87160476032008a54f225bb8139c6b562b125215
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 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.apihandler.filters;
22
23 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
24 import static org.junit.Assert.assertThat;
25 import static org.mockito.Mockito.doReturn;
26 import java.io.IOException;
27 import java.util.Collections;
28 import javax.ws.rs.container.ContainerRequestContext;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.ExpectedException;
32 import org.junit.runner.RunWith;
33 import org.mockito.InjectMocks;
34 import org.mockito.Mock;
35 import org.mockito.Spy;
36 import org.mockito.junit.MockitoJUnitRunner;
37 import org.onap.logging.ref.slf4j.ONAPLogConstants;
38 import org.onap.so.apihandlerinfra.exceptions.DuplicateRequestIdException;
39 import org.onap.so.db.request.beans.InfraActiveRequests;
40 import org.onap.so.db.request.client.RequestsDbClient;
41 import org.onap.so.serviceinstancebeans.RequestError;
42 import org.onap.so.serviceinstancebeans.ServiceException;
43 import org.slf4j.MDC;
44 import com.fasterxml.jackson.databind.DeserializationFeature;
45 import com.fasterxml.jackson.databind.ObjectMapper;
46
47 @RunWith(MockitoJUnitRunner.class)
48 public class RequestIdFilterTest {
49
50     @Mock
51     private ContainerRequestContext mockContext;
52
53     @Mock
54     private RequestsDbClient requestsDbClient;
55
56     @InjectMocks
57     @Spy
58     private RequestIdFilter requestIdFilter;
59
60     @Rule
61     public ExpectedException thrown = ExpectedException.none();
62
63     private static final String REQUEST_ID = "32807a28-1a14-4b88-b7b3-2950918aa769";
64     private ObjectMapper mapper = new ObjectMapper();
65
66     private RequestError getRequestError() throws IOException {
67         RequestError requestError = new RequestError();
68         mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
69         ServiceException serviceException = new ServiceException();
70         serviceException.setMessageId("SVC0002");
71         serviceException.setText(
72                 "RequestId: 32807a28-1a14-4b88-b7b3-2950918aa769 already exists in the RequestDB InfraActiveRequests table");
73         serviceException.setVariables(Collections.emptyList());
74         requestError.setServiceException(serviceException);
75         return requestError;
76     }
77
78     @Test
79     public void filterTestInfra() throws IOException {
80         String error = mapper.writeValueAsString(getRequestError());
81         String requestId = REQUEST_ID;
82         MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, requestId);
83
84         // ExpectedRecord InfraActiveRequests
85         InfraActiveRequests infraActiveRequests = new InfraActiveRequests();
86         infraActiveRequests.setRequestId(REQUEST_ID);
87
88         doReturn(infraActiveRequests).when(requestsDbClient).getInfraActiveRequestbyRequestId(requestId);
89         doReturn(error).when(requestIdFilter).createRequestError(REQUEST_ID, "InfraActiveRequests");
90
91         thrown.expect(DuplicateRequestIdException.class);
92         thrown.expectMessage("HTTP 400 Bad Request");
93         requestIdFilter.filter(mockContext);
94     }
95
96     @Test
97     public void createRequestErrorTest() throws IOException {
98         RequestError requestError = getRequestError();
99         String result = requestIdFilter.createRequestError(REQUEST_ID, "InfraActiveRequests");
100         RequestError resultingError = mapper.readValue(result, RequestError.class);
101
102         assertThat(resultingError, sameBeanAs(requestError));
103     }
104 }