421136d402b6b2b949fb3ac8376f2b6fe88ca995
[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 static org.mockito.Mockito.never;
27 import static org.mockito.Mockito.verify;
28 import java.io.IOException;
29 import java.net.URI;
30 import java.util.Collections;
31 import javax.ws.rs.container.ContainerRequestContext;
32 import javax.ws.rs.core.UriInfo;
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.rules.ExpectedException;
36 import org.junit.runner.RunWith;
37 import org.mockito.InjectMocks;
38 import org.mockito.Mock;
39 import org.mockito.Spy;
40 import org.mockito.junit.MockitoJUnitRunner;
41 import org.onap.logging.ref.slf4j.ONAPLogConstants;
42 import org.onap.so.apihandlerinfra.exceptions.DuplicateRequestIdException;
43 import org.onap.so.db.request.beans.InfraActiveRequests;
44 import org.onap.so.db.request.client.RequestsDbClient;
45 import org.onap.so.serviceinstancebeans.RequestError;
46 import org.onap.so.serviceinstancebeans.ServiceException;
47 import org.slf4j.MDC;
48 import com.fasterxml.jackson.databind.DeserializationFeature;
49 import com.fasterxml.jackson.databind.ObjectMapper;
50
51 @RunWith(MockitoJUnitRunner.class)
52 public class RequestIdFilterTest {
53
54     @Mock
55     private ContainerRequestContext mockContext;
56
57     @Mock
58     private RequestsDbClient requestsDbClient;
59
60     @Mock
61     private UriInfo uriInfo;
62
63     @InjectMocks
64     @Spy
65     private RequestIdFilter requestIdFilter;
66
67     @Rule
68     public ExpectedException thrown = ExpectedException.none();
69
70     private static final String REQUEST_ID = "32807a28-1a14-4b88-b7b3-2950918aa769";
71     private ObjectMapper mapper = new ObjectMapper();
72
73     private RequestError getRequestError() throws IOException {
74         RequestError requestError = new RequestError();
75         mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
76         ServiceException serviceException = new ServiceException();
77         serviceException.setMessageId("SVC0002");
78         serviceException.setText(
79                 "RequestId: 32807a28-1a14-4b88-b7b3-2950918aa769 already exists in the RequestDB InfraActiveRequests table");
80         serviceException.setVariables(Collections.emptyList());
81         requestError.setServiceException(serviceException);
82         return requestError;
83     }
84
85     @Test
86     public void filterTestInfra() throws IOException {
87         String error = mapper.writeValueAsString(getRequestError());
88         String requestId = REQUEST_ID;
89         MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, requestId);
90
91         // ExpectedRecord InfraActiveRequests
92         InfraActiveRequests infraActiveRequests = new InfraActiveRequests();
93         infraActiveRequests.setRequestId(REQUEST_ID);
94
95         doReturn(infraActiveRequests).when(requestsDbClient).getInfraActiveRequestbyRequestId(requestId);
96         doReturn(error).when(requestIdFilter).createRequestError(REQUEST_ID, "InfraActiveRequests");
97         doReturn("/onap/so/infra/serviceInstantiation/v7/serviceInstances").when(uriInfo).getPath();
98         doReturn(uriInfo).when(mockContext).getUriInfo();
99
100         thrown.expect(DuplicateRequestIdException.class);
101         thrown.expectMessage("HTTP 400 Bad Request");
102
103
104         requestIdFilter.filter(mockContext);
105     }
106
107
108     @Test
109     public void filterTestInfraSkipRequestIdLookup() throws IOException {
110         String requestId = REQUEST_ID;
111         MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, requestId);
112
113         // ExpectedRecord InfraActiveRequests
114         InfraActiveRequests infraActiveRequests = new InfraActiveRequests();
115         infraActiveRequests.setRequestId(REQUEST_ID);
116
117         doReturn("onap/so/infra/orchestrationRequests/v7/" + REQUEST_ID).when(uriInfo).getPath();
118         doReturn(uriInfo).when(mockContext).getUriInfo();
119
120         verify(requestsDbClient, never()).getInfraActiveRequestbyRequestId(REQUEST_ID);
121
122         requestIdFilter.filter(mockContext);
123     }
124
125
126     @Test
127     public void createRequestErrorTest() throws IOException {
128         RequestError requestError = getRequestError();
129         String result = requestIdFilter.createRequestError(REQUEST_ID, "InfraActiveRequests");
130         RequestError resultingError = mapper.readValue(result, RequestError.class);
131
132         assertThat(resultingError, sameBeanAs(requestError));
133     }
134 }