Commit 8435ad9b authored by Matija Obreza's avatar Matija Obreza
Browse files

Tests and refactoring for getClassPkId(String)

parent 88449b5c
......@@ -48,6 +48,6 @@ public interface ClassPKService {
* @param className
* @return
*/
Long getClassPkId(String className) throws ClassNotFoundException;
Long getClassPkId(String className);
}
......@@ -89,25 +89,23 @@ public class ClassPKServiceImpl implements ClassPKService {
}
@Override
@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRES_NEW)
public Long getClassPkId(String classname) throws ClassNotFoundException {
Long id = null;
try {
id = classPkRepository.getClassPkId(classname);
if (id == null) {
throw new Exception();
@Transactional
public Long getClassPkId(String classname) {
Long id = classPkRepository.getClassPkId(classname);
if (id == null) {
try {
Class<?> aClass = Class.forName(classname);
return getClassPk(aClass).getId();
} catch (ClassNotFoundException e) {
return null;
}
} catch (Exception e) {
Class aClass = Class.forName(classname);
ClassPK classPK = createNewClassPkForClass(aClass);
id = classPK.getId();
} else {
return id;
}
return id;
}
private ClassPK createNewClassPkForClass(Class<?> aClass) {
ClassPK classPk = new ClassPK();
classPk.setClassname(aClass.getName());
classPk.setShortName(generateShortPkName(aClass.getSimpleName().toLowerCase()));
......
......@@ -48,4 +48,34 @@ public class ClassPKServiceTest extends ServiceTest {
assertThat(cpk2.getClassname(), equalTo(Long.class.getName()));
assertThat(cpk.getId(), equalTo(cpk2.getId()));
}
@Test
public void testGetClassPkId() {
final ClassPK cpk = classPkService.getClassPk(Long.class);
assertThat(cpk.getId(), not(nullValue()));
assertThat(cpk.getClassname(), equalTo(Long.class.getName()));
final Long id = classPkService.getClassPkId(Long.class.getName());
assertThat(id, equalTo(cpk.getId()));
}
@Test
public void testCreateClassPkId() {
final Long id = classPkService.getClassPkId(Long.class.getName());
assertThat(id, not(nullValue()));
final ClassPK cpk = classPkService.getClassPk(Long.class);
assertThat(id, equalTo(cpk.getId()));
assertThat(cpk.getId(), not(nullValue()));
assertThat(cpk.getClassname(), equalTo(Long.class.getName()));
}
/**
* Test that classPk is not created for classnames that don't exist
*/
@Test
public void testNoCreateClassPkForCNFE() {
final Long id = classPkService.getClassPkId("this.class.does.not.Exist");
assertThat(id, nullValue());
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment