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.*;
30 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
31 import org.onap.so.logger.ErrorCode;
32 import org.onap.so.logger.MessageEnum;
34 import java.io.IOException;
35 import java.util.LinkedList;
36 import java.util.List;
38 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
39 import static org.hamcrest.Matchers.hasProperty;
40 import static org.hamcrest.Matchers.is;
41 import static org.hamcrest.Matchers.startsWith;
44 public class ApiExceptionTest {
47 public ExpectedException thrown = ExpectedException.none();
50 public void testRecipeNotFoundException() throws ApiException {
51 thrown.expect(RecipeNotFoundException.class);
52 thrown.expectMessage("Message rewritten");
53 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_NOT_FOUND)));
54 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_BAD_PARAMETER)));
55 RecipeNotFoundException testException = new RecipeNotFoundException.Builder("Test Message", HttpStatus.SC_NOT_FOUND,ErrorNumbers.SVC_BAD_PARAMETER).message("Message rewritten").build();
61 public void testBPMNFailureException() throws ApiException {
62 List<String> testVariables = new LinkedList<>();
63 testVariables.add("hello");
64 thrown.expect(BPMNFailureException.class);
65 thrown.expectMessage(startsWith("Request Failed due to BPEL error with HTTP Status ="));
66 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_NOT_FOUND)));
67 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_BAD_PARAMETER)));
68 thrown.expect(hasProperty("variables",sameBeanAs(testVariables)));
69 BPMNFailureException testException = new BPMNFailureException.Builder("Test Message", HttpStatus.SC_NOT_FOUND,ErrorNumbers.SVC_BAD_PARAMETER).variables(testVariables).build();
75 public void testClientConnectionException() throws ApiException {
76 IOException ioException = new IOException();
77 thrown.expect(ClientConnectionException.class);
78 thrown.expectMessage("Client from test failed to connect");
79 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_NOT_FOUND)));
80 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_BAD_PARAMETER)));
81 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_NOT_FOUND)));
82 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_BAD_PARAMETER)));
83 thrown.expect(hasProperty("cause", sameBeanAs(ioException)));
84 ClientConnectionException testException = new ClientConnectionException.Builder("test", HttpStatus.SC_NOT_FOUND,ErrorNumbers.SVC_BAD_PARAMETER).cause(ioException).build();
90 public void testDuplicateRequestException() throws ApiException {
91 ErrorLoggerInfo testLog = new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ATTRIBUTE_NOT_FOUND, ErrorCode.DataError).errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
92 thrown.expect(DuplicateRequestException.class);
93 thrown.expectMessage(startsWith("Error: Locked instance"));
94 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_NOT_FOUND)));
95 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_BAD_PARAMETER)));
96 thrown.expect(hasProperty("errorLoggerInfo", sameBeanAs(testLog)));
97 DuplicateRequestException testException = new DuplicateRequestException.Builder("Test1", "Test2","Test3","Test4", HttpStatus.SC_NOT_FOUND,ErrorNumbers.SVC_BAD_PARAMETER).errorInfo(testLog).build();
103 public void testValidateException() throws ApiException {
104 thrown.expect(ValidateException.class);
105 thrown.expectMessage("Test Message");
106 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_NOT_FOUND)));
107 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR)));
109 ValidateException testException = new ValidateException.Builder("Test Message", HttpStatus.SC_NOT_FOUND,ErrorNumbers.SVC_BAD_PARAMETER).messageID(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).build();
115 public void testVfModuleNotFoundException() throws ApiException {
116 thrown.expect(VfModuleNotFoundException.class);
117 thrown.expectMessage("Test Message");
118 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_CONFLICT)));
119 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_BAD_PARAMETER)));
120 VfModuleNotFoundException testException = new VfModuleNotFoundException.Builder("Test Message", HttpStatus.SC_NOT_FOUND,ErrorNumbers.SVC_BAD_PARAMETER).httpResponseCode(HttpStatus.SC_CONFLICT).build();