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