Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename terms #73

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions src/main/java/org/ayfaar/app/controllers/TermController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
import org.ayfaar.app.dao.CommonDao;
import org.ayfaar.app.dao.LinkDao;
import org.ayfaar.app.dao.TermDao;
import org.ayfaar.app.dao.TermMorphDao;
import org.ayfaar.app.model.*;
import org.ayfaar.app.spring.Model;
import org.ayfaar.app.utils.AliasesMap;
import org.ayfaar.app.utils.Morpher;
import org.ayfaar.app.utils.TermUtils;
import org.ayfaar.app.utils.ValueObjectUtils;
import org.ayfaar.app.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
Expand All @@ -31,6 +29,7 @@ public class TermController {

@Autowired CommonDao commonDao;
@Autowired TermDao termDao;
@Autowired TermMorphDao termMorphDao;
@Autowired LinkDao linkDao;
@Autowired AliasesMap aliasesMap;
@Autowired
Expand Down Expand Up @@ -284,4 +283,26 @@ public void remove(@PathVariable String name) {
public void reloadAliasesMap() {
aliasesMap.reload();
}


@RequestMapping(value = "rename")
@ResponseBody
public void renameTerm(@RequestParam("oldTerm") String oldName, @RequestParam("newTerm") String newName) {
Term oldTerm = aliasesMap.getTerm(oldName);
add(newName, oldTerm.getShortDescription(), oldTerm.getDescription());

Term newTerm = termDao.getByName(newName);

List<Link> links = linkDao.getAllLinks(oldTerm.getUri());
for(Link link : links) {
if(link.getUid1().getUri().equals(oldTerm.getUri())) {
link.setUid1(newTerm);
}
else if(link.getUid2().getUri().equals(oldTerm.getUri())) {
link.setUid2(newTerm);
}
linkDao.save(link);
}
remove(oldName);
}
}
68 changes: 68 additions & 0 deletions src/test/java/issues/issue66/TermControllerIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package issues.issue66;

import org.ayfaar.app.IntegrationTest;
import org.ayfaar.app.controllers.TermController;
import org.ayfaar.app.dao.LinkDao;
import org.ayfaar.app.dao.TermDao;
import org.ayfaar.app.model.Item;
import org.ayfaar.app.model.Link;
import org.ayfaar.app.model.Term;
import org.ayfaar.app.utils.AliasesMap;
import org.ayfaar.app.utils.UriGenerator;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static org.junit.Assert.*;


public class TermControllerIntegrationTest extends IntegrationTest {
@Autowired private TermController termController;
@Autowired private TermDao termDao;
@Autowired private LinkDao linkDao;
@Autowired private AliasesMap aliasesMap;

@Test
@Transactional
public void testRenameTerm1() {
String oldName = "ВСЕ-Воля-ВСЕ-Разума";
String newName = "test new name";
termController.renameTerm(oldName, newName);

Term term = aliasesMap.getTerm(newName);
List<Link> links = linkDao.getAllLinks(term.getUri());

assertEquals(UriGenerator.generate(Term.class, "Разум"), links.get(0).getUid1().getUri());
assertEquals(UriGenerator.generate(Term.class, "Test new name"), links.get(0).getUid2().getUri());
assertEquals(UriGenerator.generate(Term.class, "Test new name"), links.get(1).getUid1().getUri());
assertEquals(UriGenerator.generate(Item.class, "2.0170"), links.get(1).getUid2().getUri());
assertEquals(UriGenerator.generate(Term.class, "Test new name"), links.get(2).getUid1().getUri());
assertEquals(UriGenerator.generate(Term.class, "КРА-АГГА-АГГА"), links.get(2).getUid2().getUri());
assertEquals(UriGenerator.generate(Term.class, "Test new name"), links.get(3).getUid1().getUri());
assertEquals(UriGenerator.generate(Item.class, "2.0148"), links.get(3).getUid2().getUri());
assertNull(termDao.getByName("ВСЕ-Воля-ВСЕ-Разума"));
assertEquals("Test new name", termDao.getByName("test new name").getName());
}

@Test
@Transactional
public void testRenameTerm2() {
String oldName = "Временная Сущность";
String newName = "Новая Сущность";
termController.renameTerm(oldName, newName);

Term term = aliasesMap.getTerm(newName);
List<Link> links = linkDao.getAllLinks(term.getUri());

assertEquals(UriGenerator.generate(Term.class, "Время"), links.get(0).getUid1().getUri());
assertEquals(UriGenerator.generate(Term.class, "Новая Сущность"), links.get(0).getUid2().getUri());
assertEquals(UriGenerator.generate(Term.class, "Вселенская Временная Сущность"), links.get(1).getUid1().getUri());
assertEquals(UriGenerator.generate(Term.class, "Новая Сущность"), links.get(1).getUid2().getUri());
assertEquals(UriGenerator.generate(Term.class, "Новая Сущность"), links.get(2).getUid1().getUri());
assertEquals(UriGenerator.generate(Item.class, "1.1036"), links.get(2).getUid2().getUri());
assertNull(termDao.getByName("Временная Сущность"));
assertEquals("Новая Сущность", termDao.getByName("Новая Сущность").getName());
}
}
73 changes: 73 additions & 0 deletions src/test/java/issues/issue66/TermControllerUnitTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package issues.issue66;

import org.ayfaar.app.controllers.TermController;
import org.ayfaar.app.dao.LinkDao;
import org.ayfaar.app.dao.TermDao;
import org.ayfaar.app.model.Item;
import org.ayfaar.app.model.Link;
import org.ayfaar.app.model.Term;
import org.ayfaar.app.utils.AliasesMap;
import org.ayfaar.app.utils.UriGenerator;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.List;

import static java.util.Arrays.asList;
import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class TermControllerUnitTest {
@InjectMocks @Spy TermController termController;
@Mock TermDao termDao;
@Mock AliasesMap aliasesMap;
@Mock LinkDao linkDao;

private List<Link> links;

@Before
public void init() {
Term term1 = new Term("Разум");
term1.setUri(UriGenerator.generate(term1));
Term term2 = new Term("New name");
term2.setUri(UriGenerator.generate(term2));
Term term3 = new Term("КРА-АГГА-АГГА");
term3.setUri(UriGenerator.generate(term3));
Item item1= new Item("2.0170");
item1.setUri(UriGenerator.generate(item1));
Item item2 = new Item("2.0148");
item2.setUri(UriGenerator.generate(item2));

links = asList(new Link(term1, term2), new Link(term2, item1),
new Link(term2, term3), new Link(term2, item2));
}

@Test
public void testRenameTerm() {
String oldName = "ВСЕ-Воля-ВСЕ-Разума";
String newName = "New name";
Term oldTerm = new Term(oldName);
Term newTerm = new Term(newName);


when(aliasesMap.getTerm(oldName)).thenReturn(oldTerm);
when(termDao.getByName(newName)).thenReturn(newTerm);
when(linkDao.getAllLinks(oldTerm.getUri())).thenReturn(links);
doNothing().when(termController).remove(anyString());
when(linkDao.save((Link)anyObject())).thenReturn(new Link());

termController.renameTerm(oldName, newName);

verify(termController, times(1)).add(anyString(), anyString(), anyString());
verify(termController, times(1)).remove(oldName);
verify(aliasesMap, times(1)).getTerm(oldName);
verify(termDao, times(2)).getByName(anyString());
verify(linkDao, times(1)).getAllLinks(anyString());
verify(linkDao, times(1)).save(links.get(0));
}
}