2 package org.oransc.rappcatalogue.api;
4 import static org.assertj.core.api.Assertions.assertThat;
5 import static org.junit.jupiter.api.Assertions.assertThrows;
6 import static org.mockito.Mockito.mock;
7 import static org.mockito.Mockito.verify;
8 import static org.mockito.Mockito.when;
9 import static org.springframework.http.HttpStatus.CREATED;
10 import static org.springframework.http.HttpStatus.NO_CONTENT;
11 import static org.springframework.http.HttpStatus.OK;
13 import java.io.IOException;
14 import java.io.PrintWriter;
16 import java.util.Arrays;
17 import java.util.List;
19 import javax.servlet.http.HttpServletRequest;
20 import javax.servlet.http.HttpServletResponse;
22 import org.junit.jupiter.api.Test;
23 import org.junit.jupiter.api.extension.ExtendWith;
24 import org.mockito.Mock;
25 import org.mockito.junit.jupiter.MockitoExtension;
26 import org.oransc.rappcatalogue.exception.HeaderException;
27 import org.oransc.rappcatalogue.exception.InvalidServiceException;
28 import org.oransc.rappcatalogue.exception.ServiceNotFoundException;
29 import org.oransc.rappcatalogue.model.InputService;
30 import org.oransc.rappcatalogue.model.Service;
31 import org.springframework.http.ResponseEntity;
32 import org.springframework.web.context.request.NativeWebRequest;
34 @ExtendWith(MockitoExtension.class)
35 class ServicesApiDelegateImplTest {
38 NativeWebRequest webRequestMock;
40 private static final String INVALID_SERVICE_MESSAGE = "Service is missing required property: version";
41 private static final String SERVICE_NAME = "Service Name";
42 private static final String SERVICE_DESCRIPTION = "description";
43 private static final String SERVICE_VERSION = "1.0";
44 private static final String SERVICE_DISPLAY_NAME = "Display Name";
47 void getAddedService_shouldReturnService() throws Exception {
48 ServicesApiDelegateImpl delegateUnderTest = new ServicesApiDelegateImpl(webRequestMock);
50 InputService service = new InputService();
51 service.setDescription(SERVICE_DESCRIPTION);
52 service.setVersion(SERVICE_VERSION);
53 service.setDisplayName(SERVICE_DISPLAY_NAME);
55 whenPrintResponseShouldWork();
57 delegateUnderTest.putIndividualService(SERVICE_NAME, service);
59 ResponseEntity<Service> response = delegateUnderTest.getIndividualService(SERVICE_NAME);
61 assertThat(response.getStatusCode()).isEqualTo(OK);
62 assertThat(response.getBody().getName()).isEqualTo(SERVICE_NAME);
66 void getMissingService_shouldThrowException() {
67 ServicesApiDelegateImpl delegateUnderTest = new ServicesApiDelegateImpl(null);
69 Exception exception = assertThrows(ServiceNotFoundException.class, () -> {
70 delegateUnderTest.getIndividualService(SERVICE_NAME);
73 String expectedMessage = "Service " + SERVICE_NAME + " not found";
74 String actualMessage = exception.getMessage();
76 assertThat(actualMessage).isEqualTo(expectedMessage);
80 void putNewValidService_shouldBeCreatedAndRegisteredAndUrlToNewServiceAddedToLocationHeaderInResponse()
82 ServicesApiDelegateImpl delegateUnderTest = new ServicesApiDelegateImpl(webRequestMock);
84 InputService service = new InputService();
85 service.setDescription(SERVICE_DESCRIPTION);
86 service.setVersion(SERVICE_VERSION);
87 service.setDisplayName(SERVICE_DISPLAY_NAME);
89 String urlToCreatedService = "URL to created Service";
90 HttpServletResponse servletResponseMock = whenPrintResponseShouldWork(urlToCreatedService);
92 ResponseEntity<Void> putResponse = delegateUnderTest.putIndividualService(SERVICE_NAME, service);
94 assertThat(putResponse.getStatusCode()).isEqualTo(CREATED);
95 verify(servletResponseMock).addHeader("Location", urlToCreatedService);
97 ResponseEntity<Service> getResponse = delegateUnderTest.getIndividualService(SERVICE_NAME);
99 assertThat(getResponse.getStatusCode()).isEqualTo(OK);
100 Service body = getResponse.getBody();
101 assertThat(body.getName()).isEqualTo(SERVICE_NAME);
102 assertThat(body.getRegistrationDate()).isEqualTo(getTodaysDate());
106 void putModifiedService_shouldBeModified() throws Exception {
107 ServicesApiDelegateImpl delegateUnderTest = new ServicesApiDelegateImpl(webRequestMock);
109 InputService service = new InputService();
110 service.setDescription(SERVICE_DESCRIPTION);
111 service.setVersion(SERVICE_VERSION);
112 service.setDisplayName(SERVICE_DISPLAY_NAME);
114 whenPrintResponseShouldWork();
116 delegateUnderTest.putIndividualService(SERVICE_NAME, service);
118 String newDescription = "New description";
119 service.setDescription(newDescription);
120 ResponseEntity<Void> putResponse = delegateUnderTest.putIndividualService(SERVICE_NAME, service);
122 assertThat(putResponse.getStatusCode()).isEqualTo(OK);
124 ResponseEntity<Service> getResponse = delegateUnderTest.getIndividualService(SERVICE_NAME);
126 assertThat(getResponse.getStatusCode()).isEqualTo(OK);
127 assertThat(getResponse.getBody().getDescription()).isEqualTo(newDescription);
131 void putServiceWithVersionNull_shouldThrowException() {
132 ServicesApiDelegateImpl delegateUnderTest = new ServicesApiDelegateImpl(null);
134 InputService service = new InputService();
135 service.setDescription(SERVICE_DESCRIPTION);
136 service.setDisplayName(SERVICE_DISPLAY_NAME);
138 Exception exception = assertThrows(InvalidServiceException.class, () -> {
139 delegateUnderTest.putIndividualService(SERVICE_NAME, service);
142 assertThat(exception.getMessage()).isEqualTo(INVALID_SERVICE_MESSAGE);
146 void putServiceWithBlankVersion_shouldThrowException() {
147 ServicesApiDelegateImpl delegateUnderTest = new ServicesApiDelegateImpl(null);
149 InputService service = new InputService();
150 service.setVersion("");
151 service.setDescription(SERVICE_DESCRIPTION);
152 service.setDisplayName(SERVICE_DISPLAY_NAME);
154 Exception exception = assertThrows(InvalidServiceException.class, () -> {
155 delegateUnderTest.putIndividualService(SERVICE_NAME, service);
158 assertThat(exception.getMessage()).isEqualTo(INVALID_SERVICE_MESSAGE);
162 void putServiceWhenIoExceptionAddingHeader_shouldThrowExceptionAndNoServiceCreated() throws Exception {
163 ServicesApiDelegateImpl delegateUnderTest = new ServicesApiDelegateImpl(webRequestMock);
165 whenGetRequestUrlThenReturnUrl();
166 HttpServletResponse servletResponseMock = mock(HttpServletResponse.class);
167 when(webRequestMock.getNativeResponse(HttpServletResponse.class)).thenReturn(servletResponseMock);
168 when(servletResponseMock.getWriter()).thenThrow(new IOException("Error"));
170 InputService service = new InputService();
171 service.setVersion("1.0");
172 service.setDescription(SERVICE_DESCRIPTION);
173 service.setDisplayName(SERVICE_DISPLAY_NAME);
175 Exception exception = assertThrows(HeaderException.class, () -> {
176 delegateUnderTest.putIndividualService(SERVICE_NAME, service);
179 assertThat(exception.getMessage())
180 .isEqualTo("Unable to set header Location in put response for service " + SERVICE_NAME + ". Cause: Error");
182 ResponseEntity<List<Service>> response = delegateUnderTest.getServices();
183 assertThat(response.getBody()).isEmpty();
187 void getServices_shouldProvideArrayOfAddedServiceNames() throws Exception {
188 ServicesApiDelegateImpl delegateUnderTest = new ServicesApiDelegateImpl(webRequestMock);
190 InputService service1 = new InputService();
191 service1.setDescription("description 1");
192 service1.setVersion(SERVICE_VERSION);
193 service1.setDisplayName("Display Name 1");
195 InputService service2 = new InputService();
196 service2.setDescription("description 2");
197 service2.setVersion(SERVICE_VERSION);
198 service2.setDisplayName("Display Name 2");
200 whenPrintResponseShouldWork();
202 String serviceName1 = "Service Name 1";
203 delegateUnderTest.putIndividualService(serviceName1, service1);
204 String serviceName2 = "Service Name 2";
205 delegateUnderTest.putIndividualService(serviceName2, service2);
207 ResponseEntity<List<Service>> response = delegateUnderTest.getServices();
209 assertThat(response.getStatusCode()).isEqualTo(OK);
210 List<Service> services = response.getBody();
211 assertThat(services).hasSize(2);
212 List<String> expectedServiceNames = Arrays.asList(serviceName1, serviceName2);
213 assertThat(expectedServiceNames).contains(services.get(0).getName()) //
214 .contains(services.get(1).getName());
218 void deleteService_shouldBeOk() throws Exception {
219 ServicesApiDelegateImpl delegateUnderTest = new ServicesApiDelegateImpl(webRequestMock);
221 InputService service = new InputService();
222 service.setDescription(SERVICE_DESCRIPTION);
223 service.setVersion(SERVICE_VERSION);
224 service.setDisplayName(SERVICE_DISPLAY_NAME);
226 whenPrintResponseShouldWork();
228 delegateUnderTest.putIndividualService(SERVICE_NAME, service);
230 ResponseEntity<List<Service>> servicesResponse = delegateUnderTest.getServices();
232 assertThat(servicesResponse.getBody()).hasSize(1);
234 ResponseEntity<Void> deleteResponse = delegateUnderTest.deleteIndividualService(SERVICE_NAME);
236 assertThat(deleteResponse.getStatusCode()).isEqualTo(NO_CONTENT);
238 servicesResponse = delegateUnderTest.getServices();
240 assertThat(servicesResponse.getBody()).isEmpty();
243 private void whenGetRequestUrlThenReturnUrl() {
244 whenGetRequestUrlThenReturnUrl("URL");
247 private void whenGetRequestUrlThenReturnUrl(String url) {
248 HttpServletRequest servletRequestMock = mock(HttpServletRequest.class);
249 when(webRequestMock.getNativeRequest(HttpServletRequest.class)).thenReturn(servletRequestMock);
250 when(servletRequestMock.getRequestURL()).thenReturn(new StringBuffer(url));
253 private HttpServletResponse whenPrintResponseShouldWork() {
254 return whenPrintResponseShouldWork("URL");
257 private HttpServletResponse whenPrintResponseShouldWork(String url) {
258 whenGetRequestUrlThenReturnUrl(url);
259 HttpServletResponse servletResponseMock = mock(HttpServletResponse.class);
260 when(webRequestMock.getNativeResponse(HttpServletResponse.class)).thenReturn(servletResponseMock);
261 PrintWriter printWriterMock = mock(PrintWriter.class);
263 when(servletResponseMock.getWriter()).thenReturn(printWriterMock);
264 } catch (IOException e) {
267 return servletResponseMock;
270 private String getTodaysDate() {
271 long millis = System.currentTimeMillis();
272 Date date = new Date(millis);
273 return date.toString();