Removed MsoLogger class
[so.git] / mso-api-handlers / mso-api-handler-infra / 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.ErrorCode;
32 import org.onap.so.logger.MessageEnum;
33
34 import java.io.IOException;
35 import java.util.LinkedList;
36 import java.util.List;
37
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;
42
43
44 public class ApiExceptionTest extends BaseTest {
45
46     @Rule
47     public ExpectedException thrown = ExpectedException.none();
48
49     @Test
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();
56         throw testException;
57     }
58
59
60     @Test
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();
70         throw testException;
71     }
72
73
74     @Test
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();
85         throw testException;
86     }
87
88
89     @Test
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();
98         throw testException;
99     }
100
101
102     @Test
103     public void testValidateException() throws ApiException {
104       
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 }