2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.apihandler.filters;
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;
46 import com.fasterxml.jackson.databind.DeserializationFeature;
47 import com.fasterxml.jackson.databind.ObjectMapper;
49 @RunWith(MockitoJUnitRunner.class)
50 public class RequestIdFilterTest {
53 private ContainerRequestContext mockContext;
56 private RequestsDbClient requestsDbClient;
59 private UriInfo uriInfo;
63 private RequestIdFilter requestIdFilter;
66 public ExpectedException thrown = ExpectedException.none();
68 private static final String REQUEST_ID = "32807a28-1a14-4b88-b7b3-2950918aa769";
69 private ObjectMapper mapper = new ObjectMapper();
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);
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);
88 // ExpectedRecord InfraActiveRequests
89 InfraActiveRequests infraActiveRequests = new InfraActiveRequests();
90 infraActiveRequests.setRequestId(REQUEST_ID);
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();
97 thrown.expect(DuplicateRequestIdException.class);
98 thrown.expectMessage("HTTP 400 Bad Request");
101 requestIdFilter.filter(mockContext);
106 public void filterTestInfraSkipRequestIdLookup() throws IOException {
107 String requestId = REQUEST_ID;
108 MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, requestId);
110 // ExpectedRecord InfraActiveRequests
111 InfraActiveRequests infraActiveRequests = new InfraActiveRequests();
112 infraActiveRequests.setRequestId(REQUEST_ID);
114 doReturn("onap/so/infra/orchestrationRequests/v7/" + REQUEST_ID).when(uriInfo).getPath();
115 doReturn(uriInfo).when(mockContext).getUriInfo();
117 verify(requestsDbClient, never()).getInfraActiveRequestbyRequestId(REQUEST_ID);
119 requestIdFilter.filter(mockContext);
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);
129 assertThat(resultingError, sameBeanAs(requestError));