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
9 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
19 package org.oransc.rappcatalogue.api;
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;
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;
33 class GeneralRappCatalogueControllerAdvisorTest {
36 void handleInvalidServiceException_shouldReturnBadRequestWithMessage() {
37 GeneralRappCatalogueControllerAdvisor advisorUnderTest = new GeneralRappCatalogueControllerAdvisor();
39 InvalidServiceException exception = new InvalidServiceException();
41 ResponseEntity<Object> response = advisorUnderTest.handleInvalidServiceException(exception);
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");
50 void handleServiceNotFoundException_shouldReturnNotFoundWithMessage() {
51 GeneralRappCatalogueControllerAdvisor advisorUnderTest = new GeneralRappCatalogueControllerAdvisor();
53 ServiceNotFoundException exception = new ServiceNotFoundException("Name");
55 ResponseEntity<Object> response = advisorUnderTest.handleServiceNotFoundException(exception);
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");
64 void handleHeaderException_shouldReturnInternalServerErrorWithMessage() {
65 GeneralRappCatalogueControllerAdvisor advisorUnderTest = new GeneralRappCatalogueControllerAdvisor();
67 String serviceName = "Service";
68 HeaderException exception = new HeaderException("Header", serviceName, new Exception("Cause"));
70 ResponseEntity<Object> response = advisorUnderTest.handleHeaderException(exception);
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");