+ @Mock\r
+ NativeWebRequest webRequestMock;\r
+\r
+ private static final String INVALID_SERVICE_MESSAGE = "Service is missing required property: version";\r
+ private static final String SERVICE_NAME = "Service Name";\r
+ private static final String SERVICE_DESCRIPTION = "description";\r
+ private static final String SERVICE_VERSION = "1.0";\r
+ private static final String SERVICE_DISPLAY_NAME = "Display Name";\r
+\r
+ @Test\r
+ void getAddedService_shouldReturnService() {\r
+ ServicesApiDelegateImpl delegateUnderTest = new ServicesApiDelegateImpl(webRequestMock);\r
+\r
+ InputService service = new InputService();\r
+ service.setDescription(SERVICE_DESCRIPTION);\r
+ service.setVersion(SERVICE_VERSION);\r
+ service.setDisplayName(SERVICE_DISPLAY_NAME);\r
+\r
+ whenPrintResponseShouldWork();\r
+\r
+ delegateUnderTest.putIndividualService(SERVICE_NAME, service);\r
+\r
+ ResponseEntity<Service> response = delegateUnderTest.getIndividualService(SERVICE_NAME);\r
+\r
+ assertThat(response.getStatusCode()).isEqualTo(OK);\r
+ assertThat(response.getBody().getName()).isEqualTo(SERVICE_NAME);\r
+ }\r
+\r
+ @Test\r
+ void getMissingService_shouldThrowException() {\r
+ ServicesApiDelegateImpl delegateUnderTest = new ServicesApiDelegateImpl(null);\r
+\r
+ Exception exception = assertThrows(ServiceNotFoundException.class, () -> {\r
+ delegateUnderTest.getIndividualService(SERVICE_NAME);\r
+ });\r
+\r
+ String expectedMessage = "Service " + SERVICE_NAME + " not found";\r
+ String actualMessage = exception.getMessage();\r
+\r
+ assertThat(actualMessage).isEqualTo(expectedMessage);\r
+ }\r
+\r
+ @Test\r
+ void putNewValidService_shouldBeCreatedAndRegisteredAndUrlToNewServiceAddedToLocationHeaderInResponse() {\r
+ ServicesApiDelegateImpl delegateUnderTest = new ServicesApiDelegateImpl(webRequestMock);\r
+\r
+ InputService service = new InputService();\r
+ service.setDescription(SERVICE_DESCRIPTION);\r
+ service.setVersion(SERVICE_VERSION);\r
+ service.setDisplayName(SERVICE_DISPLAY_NAME);\r
+\r
+ String urlToCreatedService = "URL to created Service";\r
+ HttpServletResponse servletResponseMock = whenPrintResponseShouldWork(urlToCreatedService);\r
+\r
+ ResponseEntity<Void> putResponse = delegateUnderTest.putIndividualService(SERVICE_NAME, service);\r
+\r
+ assertThat(putResponse.getStatusCode()).isEqualTo(CREATED);\r
+ verify(servletResponseMock).addHeader("Location", urlToCreatedService);\r
+\r
+ ResponseEntity<Service> getResponse = delegateUnderTest.getIndividualService(SERVICE_NAME);\r
+\r
+ assertThat(getResponse.getStatusCode()).isEqualTo(OK);\r
+ Service body = getResponse.getBody();\r
+ assertThat(body.getName()).isEqualTo(SERVICE_NAME);\r
+ assertThat(body.getRegistrationDate()).isEqualTo(getTodaysDate());\r
+ }\r
+\r
+ @Test\r
+ void putModifiedService_shouldBeModified() {\r
+ ServicesApiDelegateImpl delegateUnderTest = new ServicesApiDelegateImpl(webRequestMock);\r
+\r
+ InputService service = new InputService();\r
+ service.setDescription(SERVICE_DESCRIPTION);\r
+ service.setVersion(SERVICE_VERSION);\r
+ service.setDisplayName(SERVICE_DISPLAY_NAME);\r
+\r
+ whenPrintResponseShouldWork();\r
+\r
+ delegateUnderTest.putIndividualService(SERVICE_NAME, service);\r
+\r
+ String newDescription = "New description";\r
+ service.setDescription(newDescription);\r
+ ResponseEntity<Void> putResponse = delegateUnderTest.putIndividualService(SERVICE_NAME, service);\r
+\r
+ assertThat(putResponse.getStatusCode()).isEqualTo(OK);\r
+\r
+ ResponseEntity<Service> getResponse = delegateUnderTest.getIndividualService(SERVICE_NAME);\r
+\r
+ assertThat(getResponse.getStatusCode()).isEqualTo(OK);\r
+ assertThat(getResponse.getBody().getDescription()).isEqualTo(newDescription);\r
+ }\r
+\r
+ @Test\r
+ void putServiceWithVersionNull_shouldThrowException() {\r
+ ServicesApiDelegateImpl delegateUnderTest = new ServicesApiDelegateImpl(null);\r
+\r
+ InputService service = new InputService();\r
+ service.setDescription(SERVICE_DESCRIPTION);\r
+ service.setDisplayName(SERVICE_DISPLAY_NAME);\r
+\r
+ Exception exception = assertThrows(InvalidServiceException.class, () -> {\r
+ delegateUnderTest.putIndividualService(SERVICE_NAME, service);\r
+ });\r
+\r
+ assertThat(exception.getMessage()).isEqualTo(INVALID_SERVICE_MESSAGE);\r
+ }\r
+\r
+ @Test\r
+ void putServiceWithBlankVersion_shouldThrowException() {\r
+ ServicesApiDelegateImpl delegateUnderTest = new ServicesApiDelegateImpl(null);\r
+\r
+ InputService service = new InputService();\r
+ service.setVersion("");\r
+ service.setDescription(SERVICE_DESCRIPTION);\r
+ service.setDisplayName(SERVICE_DISPLAY_NAME);\r
+\r
+ Exception exception = assertThrows(InvalidServiceException.class, () -> {\r
+ delegateUnderTest.putIndividualService(SERVICE_NAME, service);\r
+ });\r
+\r
+ assertThat(exception.getMessage()).isEqualTo(INVALID_SERVICE_MESSAGE);\r
+ }\r
+\r