bc867503f2f7a5c20b47a1cc0825ea1e3464008c
[so.git] /
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 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
30 import org.onap.so.logger.ErrorCode;
31 import org.onap.so.logger.MessageEnum;
32 import java.io.IOException;
33 import java.util.LinkedList;
34 import java.util.List;
35 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
36 import static org.hamcrest.Matchers.hasProperty;
37 import static org.hamcrest.Matchers.is;
38 import static org.hamcrest.Matchers.startsWith;
39
40
41 public class ApiExceptionTest extends BaseTest {
42
43     @Rule
44     public ExpectedException thrown = ExpectedException.none();
45
46     @Test
47     public void testRecipeNotFoundException() throws ApiException {
48         thrown.expect(RecipeNotFoundException.class);
49         thrown.expectMessage("Message rewritten");
50         thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_NOT_FOUND)));
51         thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_BAD_PARAMETER)));
52         RecipeNotFoundException testException = new RecipeNotFoundException.Builder("Test Message",
53                 HttpStatus.SC_NOT_FOUND, ErrorNumbers.SVC_BAD_PARAMETER).message("Message rewritten").build();
54         throw testException;
55     }
56
57
58     @Test
59     public void testBPMNFailureException() throws ApiException {
60         List<String> testVariables = new LinkedList<>();
61         testVariables.add("hello");
62         thrown.expect(BPMNFailureException.class);
63         thrown.expectMessage(startsWith("Request Failed due to BPEL error with HTTP Status ="));
64         thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_NOT_FOUND)));
65         thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_BAD_PARAMETER)));
66         thrown.expect(hasProperty("variables", sameBeanAs(testVariables)));
67         BPMNFailureException testException = new BPMNFailureException.Builder("Test Message", HttpStatus.SC_NOT_FOUND,
68                 ErrorNumbers.SVC_BAD_PARAMETER).variables(testVariables).build();
69         throw testException;
70     }
71
72
73     @Test
74     public void testClientConnectionException() throws ApiException {
75         IOException ioException = new IOException();
76         thrown.expect(ClientConnectionException.class);
77         thrown.expectMessage("Client from test failed to connect");
78         thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_NOT_FOUND)));
79         thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_BAD_PARAMETER)));
80         thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_NOT_FOUND)));
81         thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_BAD_PARAMETER)));
82         thrown.expect(hasProperty("cause", sameBeanAs(ioException)));
83         ClientConnectionException testException =
84                 new ClientConnectionException.Builder("test", HttpStatus.SC_NOT_FOUND, ErrorNumbers.SVC_BAD_PARAMETER)
85                         .cause(ioException).build();
86         throw testException;
87     }
88
89
90     @Test
91     public void testDuplicateRequestException() throws ApiException {
92         ErrorLoggerInfo testLog =
93                 new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ATTRIBUTE_NOT_FOUND, ErrorCode.DataError)
94                         .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
95         thrown.expect(DuplicateRequestException.class);
96         thrown.expectMessage(startsWith("Error: Locked instance"));
97         thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_NOT_FOUND)));
98         thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_BAD_PARAMETER)));
99         thrown.expect(hasProperty("errorLoggerInfo", sameBeanAs(testLog)));
100         DuplicateRequestException testException = new DuplicateRequestException.Builder("Test1", "Test2", "Test3",
101                 "Test4", HttpStatus.SC_NOT_FOUND, ErrorNumbers.SVC_BAD_PARAMETER).errorInfo(testLog).build();
102         throw testException;
103     }
104
105
106     @Test
107     public void testValidateException() throws ApiException {
108
109         thrown.expect(ValidateException.class);
110         thrown.expectMessage("Test Message");
111         thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_NOT_FOUND)));
112         thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR)));
113
114         ValidateException testException =
115                 new ValidateException.Builder("Test Message", HttpStatus.SC_NOT_FOUND, ErrorNumbers.SVC_BAD_PARAMETER)
116                         .messageID(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).build();
117         throw testException;
118     }
119
120
121     @Test
122     public void testVfModuleNotFoundException() throws ApiException {
123         thrown.expect(VfModuleNotFoundException.class);
124         thrown.expectMessage("Test Message");
125         thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_CONFLICT)));
126         thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_BAD_PARAMETER)));
127         VfModuleNotFoundException testException =
128                 new VfModuleNotFoundException.Builder("Test Message", HttpStatus.SC_NOT_FOUND,
129                         ErrorNumbers.SVC_BAD_PARAMETER).httpResponseCode(HttpStatus.SC_CONFLICT).build();
130         throw testException;
131     }
132
133
134 }