2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.so.apihandlerinfra;
23 import org.apache.http.HttpStatus;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.onap.so.apihandler.common.ErrorNumbers;
28 import org.onap.so.apihandlerinfra.exceptions.*;
29 import org.onap.so.apihandlerinfra.logging.AlarmLoggerInfo;
30 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
31 import org.onap.so.logger.MessageEnum;
32 import org.onap.so.logger.MsoAlarmLogger;
33 import org.onap.so.logger.MsoLogger;
35 import java.io.IOException;
36 import java.util.LinkedList;
37 import java.util.List;
39 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
40 import static org.hamcrest.Matchers.hasProperty;
41 import static org.hamcrest.Matchers.is;
42 import static org.hamcrest.Matchers.startsWith;
45 public class ApiExceptionTest {
48 public ExpectedException thrown = ExpectedException.none();
51 public void testRecipeNotFoundException() throws ApiException {
52 thrown.expect(RecipeNotFoundException.class);
53 thrown.expectMessage("Message rewritten");
54 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_NOT_FOUND)));
55 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_BAD_PARAMETER)));
56 RecipeNotFoundException testException = new RecipeNotFoundException.Builder("Test Message", HttpStatus.SC_NOT_FOUND,ErrorNumbers.SVC_BAD_PARAMETER).message("Message rewritten").build();
62 public void testBPMNFailureException() throws ApiException {
63 List<String> testVariables = new LinkedList<>();
64 testVariables.add("hello");
65 thrown.expect(BPMNFailureException.class);
66 thrown.expectMessage(startsWith("Request Failed due to BPEL error with HTTP Status ="));
67 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_NOT_FOUND)));
68 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_BAD_PARAMETER)));
69 thrown.expect(hasProperty("variables",sameBeanAs(testVariables)));
70 BPMNFailureException testException = new BPMNFailureException.Builder("Test Message", HttpStatus.SC_NOT_FOUND,ErrorNumbers.SVC_BAD_PARAMETER).variables(testVariables).build();
76 public void testClientConnectionException() throws ApiException {
77 IOException ioException = new IOException();
78 thrown.expect(ClientConnectionException.class);
79 thrown.expectMessage("Client from test failed to connect");
80 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_NOT_FOUND)));
81 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_BAD_PARAMETER)));
82 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_NOT_FOUND)));
83 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_BAD_PARAMETER)));
84 thrown.expect(hasProperty("cause", sameBeanAs(ioException)));
85 ClientConnectionException testException = new ClientConnectionException.Builder("test", HttpStatus.SC_NOT_FOUND,ErrorNumbers.SVC_BAD_PARAMETER).cause(ioException).build();
91 public void testDuplicateRequestException() throws ApiException {
92 ErrorLoggerInfo testLog = new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ATTRIBUTE_NOT_FOUND, MsoLogger.ErrorCode.DataError).errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
93 thrown.expect(DuplicateRequestException.class);
94 thrown.expectMessage(startsWith("Error: Locked instance"));
95 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_NOT_FOUND)));
96 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_BAD_PARAMETER)));
97 thrown.expect(hasProperty("errorLoggerInfo", sameBeanAs(testLog)));
98 DuplicateRequestException testException = new DuplicateRequestException.Builder("Test1", "Test2","Test3","Test4", HttpStatus.SC_NOT_FOUND,ErrorNumbers.SVC_BAD_PARAMETER).errorInfo(testLog).build();
104 public void testValidateException() throws ApiException {
105 AlarmLoggerInfo testLog = new AlarmLoggerInfo.Builder("MsoConfigurationError", MsoAlarmLogger.CRITICAL,
106 Messages.errors.get(ErrorNumbers.NO_COMMUNICATION_TO_SDNC_ADAPTER)).build();
107 thrown.expect(ValidateException.class);
108 thrown.expectMessage("Test Message");
109 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_NOT_FOUND)));
110 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR)));
111 thrown.expect(hasProperty("alarmLoggerInfo", sameBeanAs(testLog)));
112 ValidateException testException = new ValidateException.Builder("Test Message", HttpStatus.SC_NOT_FOUND,ErrorNumbers.SVC_BAD_PARAMETER).messageID(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).alarmInfo(testLog).build();
118 public void testVfModuleNotFoundException() throws ApiException {
119 thrown.expect(VfModuleNotFoundException.class);
120 thrown.expectMessage("Test Message");
121 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_CONFLICT)));
122 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_BAD_PARAMETER)));
123 VfModuleNotFoundException testException = new VfModuleNotFoundException.Builder("Test Message", HttpStatus.SC_NOT_FOUND,ErrorNumbers.SVC_BAD_PARAMETER).httpResponseCode(HttpStatus.SC_CONFLICT).build();