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;
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;
48 import com.fasterxml.jackson.databind.DeserializationFeature;
49 import com.fasterxml.jackson.databind.ObjectMapper;
51 @RunWith(MockitoJUnitRunner.class)
52 public class RequestIdFilterTest {
55 private ContainerRequestContext mockContext;
58 private RequestsDbClient requestsDbClient;
61 private UriInfo uriInfo;
65 private RequestIdFilter requestIdFilter;
68 public ExpectedException thrown = ExpectedException.none();
70 private static final String REQUEST_ID = "32807a28-1a14-4b88-b7b3-2950918aa769";
71 private ObjectMapper mapper = new ObjectMapper();
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);
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);
91 // ExpectedRecord InfraActiveRequests
92 InfraActiveRequests infraActiveRequests = new InfraActiveRequests();
93 infraActiveRequests.setRequestId(REQUEST_ID);
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();
100 thrown.expect(DuplicateRequestIdException.class);
101 thrown.expectMessage("HTTP 400 Bad Request");
104 requestIdFilter.filter(mockContext);
109 public void filterTestInfraSkipRequestIdLookup() throws IOException {
110 String requestId = REQUEST_ID;
111 MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, requestId);
113 // ExpectedRecord InfraActiveRequests
114 InfraActiveRequests infraActiveRequests = new InfraActiveRequests();
115 infraActiveRequests.setRequestId(REQUEST_ID);
117 doReturn("onap/so/infra/orchestrationRequests/v7/" + REQUEST_ID).when(uriInfo).getPath();
118 doReturn(uriInfo).when(mockContext).getUriInfo();
120 verify(requestsDbClient, never()).getInfraActiveRequestbyRequestId(REQUEST_ID);
122 requestIdFilter.filter(mockContext);
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);
132 assertThat(resultingError, sameBeanAs(requestError));