9bf83153b3ec55f7454f477ef782bcf5874de3dc
[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 org.junit.Assert.assertEquals;
24 import static org.mockito.Mockito.doReturn;
25 import java.io.IOException;
26 import javax.ws.rs.container.ContainerRequestContext;
27 import org.apache.http.HttpStatus;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.ExpectedException;
31 import org.junit.runner.RunWith;
32 import org.mockito.InjectMocks;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.mockito.Spy;
36 import org.mockito.junit.MockitoJUnit;
37 import org.mockito.junit.MockitoJUnitRunner;
38 import org.mockito.junit.MockitoRule;
39 import org.onap.logging.ref.slf4j.ONAPLogConstants;
40 import org.onap.so.db.request.beans.InfraActiveRequests;
41 import org.onap.so.db.request.client.RequestsDbClient;
42 import org.slf4j.MDC;
43
44 @RunWith(MockitoJUnitRunner.class)
45 public class RequestIdFilterTest {
46
47     @Mock
48     ContainerRequestContext mockContext;
49
50     @Mock
51     protected RequestsDbClient requestsDbClient;
52
53     @InjectMocks
54     @Spy
55     RequestIdFilter requestIdFilter;
56
57     @Rule
58     public ExpectedException thrown = ExpectedException.none();
59
60     @Rule
61     public MockitoRule mockitoRule = MockitoJUnit.rule();
62
63     @Test
64     public void filterTest() throws IOException {
65
66         String requestId = "32807a28-1a14-4b88-b7b3-2950918aa769";
67         MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, requestId);
68
69         // ExpectedRecord InfraActiveRequests
70         InfraActiveRequests infraActiveRequests = new InfraActiveRequests();
71         infraActiveRequests.setRequestStatus("FAILED");
72         infraActiveRequests.setProgress(100L);
73         infraActiveRequests.setLastModifiedBy("APIH");
74         infraActiveRequests.setRequestScope("network");
75         infraActiveRequests.setRequestAction("deleteInstance");
76         infraActiveRequests.setRequestId("32807a28-1a14-4b88-b7b3-2950918aa769");
77
78         doReturn(infraActiveRequests).when(requestsDbClient).getInfraActiveRequestbyRequestId(requestId);
79
80         requestIdFilter.filter(mockContext);
81
82         Mockito.verify(requestIdFilter, Mockito.times(1)).filter(mockContext);
83         assertEquals(MDC.get(ONAPLogConstants.MDCs.RESPONSE_CODE), String.valueOf(HttpStatus.SC_BAD_REQUEST));
84
85     }
86 }