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 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;
44 import com.fasterxml.jackson.databind.DeserializationFeature;
45 import com.fasterxml.jackson.databind.ObjectMapper;
47 @RunWith(MockitoJUnitRunner.class)
48 public class RequestIdFilterTest {
51 private ContainerRequestContext mockContext;
54 private RequestsDbClient requestsDbClient;
58 private RequestIdFilter requestIdFilter;
61 public ExpectedException thrown = ExpectedException.none();
63 private static final String REQUEST_ID = "32807a28-1a14-4b88-b7b3-2950918aa769";
64 private ObjectMapper mapper = new ObjectMapper();
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);
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);
84 // ExpectedRecord InfraActiveRequests
85 InfraActiveRequests infraActiveRequests = new InfraActiveRequests();
86 infraActiveRequests.setRequestId(REQUEST_ID);
88 doReturn(infraActiveRequests).when(requestsDbClient).getInfraActiveRequestbyRequestId(requestId);
89 doReturn(error).when(requestIdFilter).createRequestError(REQUEST_ID, "InfraActiveRequests");
91 thrown.expect(DuplicateRequestIdException.class);
92 thrown.expectMessage("HTTP 400 Bad Request");
93 requestIdFilter.filter(mockContext);
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);
102 assertThat(resultingError, sameBeanAs(requestError));