Merge "Add docker-compose file for odu-app"
[nonrtric.git] / r-app-catalogue / src / test / java / org / oransc / rappcatalogue / api / GeneralRappCatalogueControllerAdvisorTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * Copyright (C) 2020 Nordix Foundation. All rights reserved.
4  * ======================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ========================LICENSE_END===================================
17  */
18
19 package org.oransc.rappcatalogue.api;
20
21 import static org.assertj.core.api.Assertions.assertThat;
22 import static org.springframework.http.HttpStatus.BAD_REQUEST;
23 import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
24 import static org.springframework.http.HttpStatus.NOT_FOUND;
25
26 import org.junit.jupiter.api.Test;
27 import org.oransc.rappcatalogue.exception.HeaderException;
28 import org.oransc.rappcatalogue.exception.InvalidServiceException;
29 import org.oransc.rappcatalogue.exception.ServiceNotFoundException;
30 import org.oransc.rappcatalogue.model.ErrorInformation;
31 import org.springframework.http.ResponseEntity;
32
33 class GeneralRappCatalogueControllerAdvisorTest {
34
35     @Test
36     void handleInvalidServiceException_shouldReturnBadRequestWithMessage() {
37         GeneralRappCatalogueControllerAdvisor advisorUnderTest = new GeneralRappCatalogueControllerAdvisor();
38
39         InvalidServiceException exception = new InvalidServiceException();
40
41         ResponseEntity<Object> response = advisorUnderTest.handleInvalidServiceException(exception);
42
43         assertThat(response.getStatusCode()).isEqualTo(BAD_REQUEST);
44         ErrorInformation body = (ErrorInformation) response.getBody();
45         assertThat(body.getStatus()).isEqualTo(BAD_REQUEST.value());
46         assertThat(body.getDetail()).isEqualTo("Service is missing required property: version");
47     }
48
49     @Test
50     void handleServiceNotFoundException_shouldReturnNotFoundWithMessage() {
51         GeneralRappCatalogueControllerAdvisor advisorUnderTest = new GeneralRappCatalogueControllerAdvisor();
52
53         ServiceNotFoundException exception = new ServiceNotFoundException("Name");
54
55         ResponseEntity<Object> response = advisorUnderTest.handleServiceNotFoundException(exception);
56
57         assertThat(response.getStatusCode()).isEqualTo(NOT_FOUND);
58         ErrorInformation body = (ErrorInformation) response.getBody();
59         assertThat(body.getStatus()).isEqualTo(NOT_FOUND.value());
60         assertThat(body.getDetail()).isEqualTo("Service Name not found");
61     }
62
63     @Test
64     void handleHeaderException_shouldReturnInternalServerErrorWithMessage() {
65         GeneralRappCatalogueControllerAdvisor advisorUnderTest = new GeneralRappCatalogueControllerAdvisor();
66
67         String serviceName = "Service";
68         HeaderException exception = new HeaderException("Header", serviceName, new Exception("Cause"));
69
70         ResponseEntity<Object> response = advisorUnderTest.handleHeaderException(exception);
71
72         assertThat(response.getStatusCode()).isEqualTo(INTERNAL_SERVER_ERROR);
73         ErrorInformation body = (ErrorInformation) response.getBody();
74         assertThat(body.getStatus()).isEqualTo(INTERNAL_SERVER_ERROR.value());
75         assertThat(body.getDetail())
76             .isEqualTo("Unable to set header Header in put response for service " + serviceName + ". Cause: Cause");
77     }
78 }