2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2018-19 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.aai.prevalidation;
23 import com.google.gson.Gson;
24 import org.apache.http.conn.ConnectTimeoutException;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.mockito.Mockito;
29 import org.onap.aai.PayloadUtil;
30 import org.onap.aai.exceptions.AAIException;
31 import org.onap.aai.restclient.RestClient;
32 import org.springframework.boot.test.rule.OutputCapture;
33 import org.springframework.http.HttpMethod;
34 import org.springframework.http.ResponseEntity;
36 import java.io.IOException;
37 import java.net.ConnectException;
38 import java.net.SocketTimeoutException;
39 import java.util.List;
41 import static org.hamcrest.CoreMatchers.containsString;
42 import static org.hamcrest.CoreMatchers.is;
43 import static org.junit.Assert.assertNotNull;
44 import static org.junit.Assert.assertThat;
45 import static org.mockito.Matchers.any;
46 import static org.mockito.Matchers.eq;
48 public class ValidationServiceTest {
50 private RestClient restClient;
52 private ValidationService validationService;
55 public OutputCapture capture = new OutputCapture();
60 public void setUp() throws Exception {
62 restClient = Mockito.mock(RestClient.class);
63 validationService = Mockito.spy(new ValidationService(restClient, "JUNIT", "generic-vnf", null));
67 public void testNodeTypeThatIsAllowedAndItShouldReturnTrue() {
68 boolean shouldValidate = validationService.shouldValidate("generic-vnf");
69 assertThat(shouldValidate, is(true));
73 public void testNodeTypeThatIsNotAllowedAndItShouldReturnFalse() {
74 boolean shouldValidate = validationService.shouldValidate("customer");
75 assertThat(shouldValidate, is(false));
79 public void testPreValidateWithSuccessRequestAndServiceIsDownAndShouldErrorWithConnectionRefused() throws IOException, AAIException {
81 String pserverRequest = PayloadUtil.getResourcePayload("prevalidation/success-request-with-no-violations.json");
86 eq(ValidationService.VALIDATION_ENDPOINT),
91 ).thenThrow(new RuntimeException(new ConnectException("Connection refused")));
93 validationService.preValidate(pserverRequest);
95 assertThat(capture.toString(), containsString("Connection refused to the validation microservice due to service unreachable"));
99 public void testPreValidateWithSuccessRequestAndServiceIsUnreachableAndShouldErrorWithConnectionTimeout() throws IOException, AAIException {
101 String pserverRequest = PayloadUtil.getResourcePayload("prevalidation/success-request-with-no-violations.json");
106 eq(ValidationService.VALIDATION_ENDPOINT),
111 ).thenThrow(new RuntimeException(new ConnectTimeoutException("Connection timed out")));
113 validationService.preValidate(pserverRequest);
115 assertThat(capture.toString(), containsString("Connection timeout to the validation microservice as this could indicate the server is unable to reach port"));
119 public void testPreValidateWithSuccessRequestAndRespondSuccessfullyWithinAllowedTime() throws IOException, AAIException {
121 String pserverRequest = PayloadUtil.getResourcePayload("prevalidation/success-request-with-no-violations.json");
122 String validationResponse = PayloadUtil.getResourcePayload("prevalidation/success-response-with-empty-violations.json");
124 ResponseEntity responseEntity = Mockito.mock(ResponseEntity.class, Mockito.RETURNS_DEEP_STUBS);
129 eq(ValidationService.VALIDATION_ENDPOINT),
134 ).thenReturn(responseEntity);
136 Mockito.when(responseEntity.getStatusCodeValue()).thenReturn(200);
137 Mockito.when(responseEntity.getBody()).thenReturn(validationResponse);
139 Mockito.doReturn(true).when(validationService).isSuccess(responseEntity);
141 List<String> errorMessages = validationService.preValidate(pserverRequest);
142 assertNotNull("Expected the error messages to be not null", errorMessages);
143 assertThat(errorMessages.size(), is(0));
147 public void testPreValidateWithSuccessRequestAndServiceIsAvailableAndRequestIsTakingTooLongAndClientShouldTimeout() throws IOException, AAIException {
149 String pserverRequest = PayloadUtil.getResourcePayload("prevalidation/success-request-with-no-violations.json");
154 eq(ValidationService.VALIDATION_ENDPOINT),
159 ).thenThrow(new RuntimeException(new SocketTimeoutException("Request timed out due to taking longer than client expected")));
161 validationService.preValidate(pserverRequest);
163 assertThat(capture.toString(), containsString("Request to validation service took longer than the currently set timeout"));
167 public void testExtractViolationsReturnsSuccessfullyAListWhenViolationsAreFound() throws IOException {
169 String genericVnfRequest = PayloadUtil.getResourcePayload("prevalidation/failed-response-with-violations.json");
171 Validation validation = gson.fromJson(genericVnfRequest, Validation.class);
172 List<String> errorMessages = validationService.extractViolations(validation);
173 assertNotNull("Expected the error messages to be not null", errorMessages);
174 assertThat(errorMessages.size(), is(1));
175 assertThat(errorMessages.get(0), is("Invalid nf values, check nf-type, nf-role, nf-function, and nf-naming-code"));
179 public void testErrorMessagesAreEmptyListWhenViolationsReturnEmptyList() throws IOException {
181 String genericVnfRequest = PayloadUtil.getResourcePayload("prevalidation/success-response-with-empty-violations.json");
183 Validation validation = gson.fromJson(genericVnfRequest, Validation.class);
184 List<String> errorMessages = validationService.extractViolations(validation);
185 assertNotNull("Expected the error messages to be not null", errorMessages);
186 assertThat(errorMessages.size(), is(0));
190 public void testErrorMessagesAreEmptyListWhenViolationsIsNotFoundInJson() throws IOException {
192 String genericVnfRequest = PayloadUtil.getResourcePayload("prevalidation/success-response-with-exclude-violations.json");
194 Validation validation = gson.fromJson(genericVnfRequest, Validation.class);
195 List<String> errorMessages = validationService.extractViolations(validation);
196 assertNotNull("Expected the error messages to be not null", errorMessages);
197 assertThat(errorMessages.size(), is(0));