1be521952ac19e640b54bf91d59552c0871c1528
[so.git] / mso-api-handlers / mso-api-handler-common / src / test / java / org / onap / so / apihandlerinfra / ApiExceptionTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.apihandlerinfra;
22
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
30 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
31 import org.onap.so.logger.MessageEnum;
32
33 import org.onap.so.logger.MsoLogger;
34
35 import java.io.IOException;
36 import java.util.LinkedList;
37 import java.util.List;
38
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;
43
44
45 public class ApiExceptionTest {
46
47     @Rule
48     public ExpectedException thrown = ExpectedException.none();
49
50     @Test
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();
57         throw testException;
58     }
59
60
61     @Test
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();
71         throw testException;
72     }
73
74
75     @Test
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();
86         throw testException;
87     }
88
89
90     @Test
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();
99         throw testException;
100     }
101
102
103     @Test
104     public void testValidateException() throws ApiException {       
105         thrown.expect(ValidateException.class);
106         thrown.expectMessage("Test Message");
107         thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_NOT_FOUND)));
108         thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR)));
109
110         ValidateException testException = new ValidateException.Builder("Test Message", HttpStatus.SC_NOT_FOUND,ErrorNumbers.SVC_BAD_PARAMETER).messageID(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).build();
111         throw testException;
112     }
113
114
115     @Test
116     public void testVfModuleNotFoundException() throws ApiException {
117         thrown.expect(VfModuleNotFoundException.class);
118         thrown.expectMessage("Test Message");
119         thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_CONFLICT)));
120         thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_BAD_PARAMETER)));
121         VfModuleNotFoundException testException = new VfModuleNotFoundException.Builder("Test Message", HttpStatus.SC_NOT_FOUND,ErrorNumbers.SVC_BAD_PARAMETER).httpResponseCode(HttpStatus.SC_CONFLICT).build();
122         throw testException;
123     }
124
125
126 }