[DMaaP DR] JKD 11 migration
[dmaap/datarouter.git] / datarouter-node / src / test / java / org / onap / dmaap / datarouter / node / DRNodeCadiFilterTest.java
1 /*\r
2  * ============LICENSE_START=======================================================\r
3  *  Copyright (C) 2019 Nordix Foundation.\r
4  * ================================================================================\r
5  * Licensed under the Apache License, Version 2.0 (the "License");\r
6  * you may not use this file except in compliance with the License.\r
7  * You may obtain a copy of the License at\r
8  *\r
9  *      http://www.apache.org/licenses/LICENSE-2.0\r
10  *\r
11  * Unless required by applicable law or agreed to in writing, software\r
12  * distributed under the License is distributed on an "AS IS" BASIS,\r
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  * See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  *\r
17  * SPDX-License-Identifier: Apache-2.0\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.onap.dmaap.datarouter.node;\r
22 \r
23 import static org.mockito.Mockito.doThrow;\r
24 import static org.mockito.Mockito.mock;\r
25 import static org.mockito.Mockito.times;\r
26 import static org.mockito.Mockito.verify;\r
27 import static org.mockito.Mockito.when;\r
28 \r
29 import java.io.IOException;\r
30 import javax.servlet.FilterChain;\r
31 import javax.servlet.ServletException;\r
32 import javax.servlet.http.HttpServletRequest;\r
33 import javax.servlet.http.HttpServletResponse;\r
34 import org.junit.Before;\r
35 import org.junit.Test;\r
36 import org.junit.runner.RunWith;\r
37 import org.mockito.Mock;\r
38 import org.onap.aaf.cadi.PropAccess;\r
39 import org.onap.aaf.cadi.filter.CadiFilter;\r
40 import org.powermock.api.mockito.PowerMockito;\r
41 import org.powermock.api.support.membermodification.MemberMatcher;\r
42 import org.powermock.core.classloader.annotations.PowerMockIgnore;\r
43 import org.powermock.core.classloader.annotations.PrepareForTest;\r
44 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;\r
45 import org.powermock.modules.junit4.PowerMockRunner;\r
46 \r
47 @SuppressStaticInitializationFor("org.onap.dmaap.datarouter.node.NodeConfigManager")\r
48 @PrepareForTest({CadiFilter.class})\r
49 @RunWith(PowerMockRunner.class)\r
50 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "javax.management.*"})\r
51 public class DRNodeCadiFilterTest {\r
52 \r
53     @Mock\r
54     private PropAccess access;\r
55 \r
56     @Mock\r
57     private HttpServletRequest request;\r
58 \r
59     @Mock\r
60     private HttpServletResponse response;\r
61 \r
62     @Mock\r
63     private FilterChain chain;\r
64 \r
65     private DRNodeCadiFilter cadiFilter;\r
66 \r
67 \r
68     @Before\r
69     public void setUp() throws ServletException {\r
70         cadiFilter = new DRNodeCadiFilter(false, access);\r
71     }\r
72 \r
73     @Test\r
74     public void Given_doFilter_Called_And_Method_Is_GET_And_AAF_DB_Instance_Is_NULL_Then_Chain_doFilter_Called()\r
75             throws Exception {\r
76         PowerMockito.mockStatic(NodeConfigManager.class);\r
77         NodeConfigManager config = mock(NodeConfigManager.class);\r
78 \r
79         PowerMockito.when(NodeConfigManager.getInstance()).thenReturn(config);\r
80         PowerMockito.when(config.getAafInstance("/other/5")).thenReturn("legacy");\r
81         when(request.getPathInfo()).thenReturn("/publish/5");\r
82         when(request.getMethod()).thenReturn("GET");\r
83         cadiFilter.doFilter(request, response, chain);\r
84         verify(chain, times(1)).doFilter(request, response);\r
85     }\r
86 \r
87     @Test\r
88     public void Given_doFilter_Called_And_Method_Is_GET_And_Path_Includes_Internal_Then_Chain_doFilter_Called()\r
89             throws Exception {\r
90         PowerMockito.mockStatic(NodeConfigManager.class);\r
91         NodeConfigManager config = mock(NodeConfigManager.class);\r
92 \r
93         PowerMockito.when(NodeConfigManager.getInstance()).thenReturn(config);\r
94         PowerMockito.when(config.getAafInstance("/other/5")).thenReturn("legacy");\r
95         when(request.getPathInfo()).thenReturn("/internal/5");\r
96         when(request.getMethod()).thenReturn("GET");\r
97         cadiFilter.doFilter(request, response, chain);\r
98         verify(chain, times(1)).doFilter(request, response);\r
99     }\r
100 \r
101     @Test\r
102     public void Given_doFilter_Called_And_Method_Is_GET_And_AAF_DB_Is_Not_Null_Then_Super_doFilter_Called()\r
103             throws Exception {\r
104         PowerMockito.mockStatic(NodeConfigManager.class);\r
105         NodeConfigManager config = mock(NodeConfigManager.class);\r
106 \r
107         PowerMockito.when(NodeConfigManager.getInstance()).thenReturn(config);\r
108         PowerMockito.when(config.getAafInstance("5")).thenReturn("EXISTS");\r
109         when(request.getPathInfo()).thenReturn("/publish/5/fileId");\r
110         when(request.getMethod()).thenReturn("GET");\r
111         PowerMockito.suppress(MemberMatcher.methodsDeclaredIn(CadiFilter.class));\r
112         cadiFilter.doFilter(request, response, chain);\r
113         verify(chain, times(0)).doFilter(request, response);\r
114     }\r
115 \r
116     @Test\r
117     public void Given_getFileid_Called_And_SendError_Fails_Then_Throw_IOException_And_Call_chain_doFilter()\r
118             throws Exception {\r
119         PowerMockito.mockStatic(NodeConfigManager.class);\r
120         NodeConfigManager config = mock(NodeConfigManager.class);\r
121 \r
122         PowerMockito.when(NodeConfigManager.getInstance()).thenReturn(config);\r
123         when(request.getPathInfo()).thenReturn("/publish/5");\r
124         when(request.getMethod()).thenReturn("DELETE");\r
125         doThrow(new IOException()).when(response).sendError(HttpServletResponse.SC_NOT_FOUND,\r
126                 "Invalid request URI.  Expecting <feed-publishing-url>/<fileid>.  Possible missing fileid.");\r
127         cadiFilter.doFilter(request, response, chain);\r
128         verify(chain, times(1)).doFilter(request, response);\r
129     }\r
130 }\r