diff --git a/lib/taskana-core/src/main/java/pro/taskana/impl/ClassificationServiceImpl.java b/lib/taskana-core/src/main/java/pro/taskana/impl/ClassificationServiceImpl.java index f39df9e4d..e153bed43 100644 --- a/lib/taskana-core/src/main/java/pro/taskana/impl/ClassificationServiceImpl.java +++ b/lib/taskana-core/src/main/java/pro/taskana/impl/ClassificationServiceImpl.java @@ -70,6 +70,7 @@ public class ClassificationServiceImpl implements ClassificationService { throw new ClassificationAlreadyExistException(classification); } classificationImpl = (ClassificationImpl) classification; + this.checkClassificationId(classificationImpl); classificationImpl.setCreated(Instant.now()); classificationImpl.setModified(classificationImpl.getCreated()); this.initDefaultClassificationValues(classificationImpl); @@ -90,6 +91,12 @@ public class ClassificationServiceImpl implements ClassificationService { return classificationImpl; } + private void checkClassificationId(ClassificationImpl classificationImpl) throws InvalidArgumentException { + if (classificationImpl.getId() != null && !"".equals(classificationImpl.getId())) { + throw new InvalidArgumentException("ClassificationId should be null on creation"); + } + } + private void addClassificationToRootDomain(ClassificationImpl classificationImpl) { if (classificationImpl.getDomain() != "") { boolean doesExist = true; diff --git a/lib/taskana-core/src/test/java/pro/taskana/impl/ClassificationServiceImplTest.java b/lib/taskana-core/src/test/java/pro/taskana/impl/ClassificationServiceImplTest.java index b38939b6c..2bf00e437 100644 --- a/lib/taskana-core/src/test/java/pro/taskana/impl/ClassificationServiceImplTest.java +++ b/lib/taskana-core/src/test/java/pro/taskana/impl/ClassificationServiceImplTest.java @@ -1,5 +1,6 @@ package pro.taskana.impl; +import static junit.framework.TestCase.assertEquals; import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; @@ -87,7 +88,7 @@ public class ClassificationServiceImplTest { public void testCreateClassificationParentNotExisting() throws ClassificationAlreadyExistException, ClassificationNotFoundException, NotAuthorizedException, DomainNotFoundException, InvalidArgumentException { - Classification classification = createDummyClassification(); + Classification classification = createDummyClassification(null); classification.setParentId("NOT EXISTING ID"); doReturn(null).when(classificationMapperMock).findByKeyAndDomain(classification.getKey(), classification.getDomain()); @@ -116,7 +117,7 @@ public class ClassificationServiceImplTest { NotAuthorizedException, DomainNotFoundException, InvalidArgumentException { Instant beforeTimestamp = Instant.now(); Thread.sleep(10L); - Classification classification = createDummyClassification(); + Classification classification = createDummyClassification(null); String domain = classification.getDomain(); String key = classification.getKey(); doReturn(null).when(classificationMapperMock).findByKeyAndDomain(classification.getKey(), @@ -147,7 +148,7 @@ public class ClassificationServiceImplTest { public void testCreateClassificationInOwnDomainAndCopyInRootDomain() throws ClassificationAlreadyExistException, NotAuthorizedException, ClassificationNotFoundException, DomainNotFoundException, InvalidArgumentException { - Classification classification = createDummyClassification(); + Classification classification = createDummyClassification(""); String domain = classification.getDomain(); String key = classification.getKey(); doReturn(null).when(classificationMapperMock).findByKeyAndDomain(classification.getKey(), @@ -174,7 +175,7 @@ public class ClassificationServiceImplTest { public void testCreateClassificationIntoRootDomain() throws ClassificationAlreadyExistException, NotAuthorizedException, ClassificationNotFoundException, DomainNotFoundException, InvalidArgumentException { - ClassificationImpl classification = (ClassificationImpl) createDummyClassification(); + ClassificationImpl classification = (ClassificationImpl) createDummyClassification(null); classification.setDomain(""); doReturn(null).when(classificationMapperMock).findByKeyAndDomain(classification.getKey(), classification.getDomain()); @@ -296,13 +297,33 @@ public class ClassificationServiceImplTest { verifyNoMoreInteractions(classificationMapperMock, taskanaEngineImplMock, classificationQueryImplMock); } + @Test(expected = InvalidArgumentException.class) + public void testThrowExceptionIdIfClassificationIsCreatedWithAnExplicitId() + throws ClassificationNotFoundException, DomainNotFoundException, InvalidArgumentException, + NotAuthorizedException, ClassificationAlreadyExistException { + try { + Classification classification = createDummyClassification(); + doReturn(true).when(taskanaEngineImplMock).domainExists(any()); + cutSpy.createClassification(classification); + } catch (InvalidArgumentException e) { + assertEquals(e.getMessage(), "ClassificationId should be null on creation"); + throw e; + } + + } + private Classification createDummyClassification() { + return this.createDummyClassification("ID: 1"); + } + + private Classification createDummyClassification(String id) { + ClassificationImpl classificationImpl = new ClassificationImpl(); classificationImpl.setDescription("A DUMMY FOR TESTING A SERVICE"); classificationImpl.setName("SERVICE-DUMMY"); classificationImpl.setDomain("DOMAIN_A"); classificationImpl.setServiceLevel("P2D"); - classificationImpl.setId("ID: 1"); + classificationImpl.setId(id); classificationImpl.setKey("ABC111"); classificationImpl.setParentId(""); return classificationImpl; diff --git a/rest/taskana-rest-spring-example/src/test/java/pro/taskana/rest/ClassificationControllerIntTest.java b/rest/taskana-rest-spring-example/src/test/java/pro/taskana/rest/ClassificationControllerIntTest.java index 3cacfb5c6..e83550601 100644 --- a/rest/taskana-rest-spring-example/src/test/java/pro/taskana/rest/ClassificationControllerIntTest.java +++ b/rest/taskana-rest-spring-example/src/test/java/pro/taskana/rest/ClassificationControllerIntTest.java @@ -11,6 +11,7 @@ import java.net.HttpURLConnection; import java.net.URL; import java.util.Collections; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; @@ -43,32 +44,37 @@ import pro.taskana.rest.resource.ClassificationSummaryResource; @Import(RestConfiguration.class) public class ClassificationControllerIntTest { + String server = "http://127.0.0.1:"; + RestTemplate template; + HttpEntity request; + HttpHeaders headers = new HttpHeaders(); @LocalServerPort int port; + @Before + public void before() { + template = getRestTemplate(); + headers.add("Authorization", "Basic dGVhbWxlYWRfMTp0ZWFtbGVhZF8x"); + request = new HttpEntity(headers); + } + @Test public void testGetAllClassifications() { - RestTemplate template = getRestTemplate(); - HttpHeaders headers = new HttpHeaders(); - headers.add("Authorization", "Basic dGVhbWxlYWRfMTp0ZWFtbGVhZF8x"); - HttpEntity request = new HttpEntity(headers); ResponseEntity> response = template.exchange( - "http://127.0.0.1:" + port + "/v1/classifications", HttpMethod.GET, request, + server + port + "/v1/classifications", HttpMethod.GET, request, new ParameterizedTypeReference>() { + }); assertNotNull(response.getBody().getLink(Link.REL_SELF)); } @Test public void testGetAllClassificationsFilterByCustomAttribute() { - RestTemplate template = getRestTemplate(); - HttpHeaders headers = new HttpHeaders(); - headers.add("Authorization", "Basic dGVhbWxlYWRfMTp0ZWFtbGVhZF8x"); - HttpEntity request = new HttpEntity(headers); ResponseEntity> response = template.exchange( - "http://127.0.0.1:" + port + "/v1/classifications?domain=DOMAIN_A&custom-1-like=RVNR", HttpMethod.GET, + server + port + "/v1/classifications?domain=DOMAIN_A&custom-1-like=RVNR", HttpMethod.GET, request, new ParameterizedTypeReference>() { + }); assertNotNull(response.getBody().getLink(Link.REL_SELF)); assertEquals(13, response.getBody().getContent().size()); @@ -76,14 +82,11 @@ public class ClassificationControllerIntTest { @Test public void testGetAllClassificationsKeepingFilters() { - RestTemplate template = getRestTemplate(); - HttpHeaders headers = new HttpHeaders(); - headers.add("Authorization", "Basic dGVhbWxlYWRfMTp0ZWFtbGVhZF8x"); - HttpEntity request = new HttpEntity(headers); ResponseEntity> response = template.exchange( - "http://127.0.0.1:" + port + "/v1/classifications?domain=DOMAIN_A&sort-by=key&order=asc", HttpMethod.GET, + server + port + "/v1/classifications?domain=DOMAIN_A&sort-by=key&order=asc", HttpMethod.GET, request, new ParameterizedTypeReference>() { + }); assertNotNull(response.getBody().getLink(Link.REL_SELF)); assertTrue(response.getBody() @@ -96,15 +99,12 @@ public class ClassificationControllerIntTest { @Test public void testGetSecondPageSortedByKey() { - RestTemplate template = getRestTemplate(); - HttpHeaders headers = new HttpHeaders(); - headers.add("Authorization", "Basic dGVhbWxlYWRfMTp0ZWFtbGVhZF8x"); - HttpEntity request = new HttpEntity(headers); ResponseEntity> response = template.exchange( - "http://127.0.0.1:" + port + "/v1/classifications?domain=DOMAIN_A&sort-by=key&order=asc&page=2&page-size=5", + server + port + "/v1/classifications?domain=DOMAIN_A&sort-by=key&order=asc&page=2&page-size=5", HttpMethod.GET, request, new ParameterizedTypeReference>() { + }); assertEquals(5, response.getBody().getContent().size()); assertEquals("L1050", response.getBody().getContent().iterator().next().key); @@ -127,7 +127,7 @@ public class ClassificationControllerIntTest { @Test public void testCreateClassification() throws IOException { String newClassification = "{\"classificationId\":\"\",\"category\":\"MANUAL\",\"domain\":\"DOMAIN_A\",\"key\":\"NEW_CLASS\",\"name\":\"new classification\",\"type\":\"TASK\"}"; - URL url = new URL("http://127.0.0.1:" + port + "/v1/classifications"); + URL url = new URL(server + port + "/v1/classifications"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("POST"); con.setRequestProperty("Authorization", "Basic dGVhbWxlYWRfMTp0ZWFtbGVhZF8x"); @@ -141,7 +141,7 @@ public class ClassificationControllerIntTest { con.disconnect(); newClassification = "{\"classificationId\":\"\",\"category\":\"MANUAL\",\"domain\":\"DOMAIN_A\",\"key\":\"NEW_CLASS_2\",\"name\":\"new classification\",\"type\":\"TASK\"}"; - url = new URL("http://127.0.0.1:" + port + "/v1/classifications"); + url = new URL(server + port + "/v1/classifications"); con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("POST"); con.setRequestProperty("Authorization", "Basic dGVhbWxlYWRfMTp0ZWFtbGVhZF8x"); @@ -155,6 +155,23 @@ public class ClassificationControllerIntTest { con.disconnect(); } + @Test + public void testCreateClassificationWithClassificationIdReturnsError400() throws IOException { + String newClassification = "{\"classificationId\":\"someId\",\"category\":\"MANUAL\",\"domain\":\"DOMAIN_A\",\"key\":\"NEW_CLASS\",\"name\":\"new classification\",\"type\":\"TASK\"}"; + URL url = new URL("http://127.0.0.1:" + port + "/v1/classifications"); + HttpURLConnection con = (HttpURLConnection) url.openConnection(); + con.setRequestMethod("POST"); + con.setRequestProperty("Authorization", "Basic dGVhbWxlYWRfMTp0ZWFtbGVhZF8x"); + con.setDoOutput(true); + con.setRequestProperty("Content-Type", "application/json"); + BufferedWriter out = new BufferedWriter(new OutputStreamWriter(con.getOutputStream())); + out.write(newClassification); + out.flush(); + out.close(); + assertEquals(400, con.getResponseCode()); + con.disconnect(); + } + @Test public void testGetClassificationWithSpecialCharacter() { RestTemplate template = getRestTemplate(); @@ -166,6 +183,7 @@ public class ClassificationControllerIntTest { HttpMethod.GET, request, new ParameterizedTypeReference() { + }); assertEquals("Zustimmungserklärung", response.getBody().name); } @@ -182,6 +200,7 @@ public class ClassificationControllerIntTest { HttpMethod.DELETE, request, new ParameterizedTypeReference() { + }); assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); @@ -190,6 +209,7 @@ public class ClassificationControllerIntTest { HttpMethod.GET, request, new ParameterizedTypeReference() { + }); } @@ -207,7 +227,7 @@ public class ClassificationControllerIntTest { converter.setSupportedMediaTypes(MediaType.parseMediaTypes("application/hal+json")); converter.setObjectMapper(mapper); - RestTemplate template = new RestTemplate(Collections.> singletonList(converter)); + RestTemplate template = new RestTemplate(Collections.>singletonList(converter)); return template; }