1 /*******************************************************************************
2 * ============LICENSE_START==================================================
4 * * ===========================================================================
5 * * Copyright © 2017 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====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
24 package org.onap.dmaap.datarouter.provisioning;
26 import org.apache.commons.lang3.reflect.FieldUtils;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mock;
32 import org.onap.dmaap.datarouter.provisioning.beans.Feed;
33 import org.onap.dmaap.datarouter.provisioning.beans.FeedAuthorization;
34 import org.onap.dmaap.datarouter.provisioning.beans.Group;
35 import org.onap.dmaap.datarouter.provisioning.beans.Subscription;
36 import org.powermock.api.mockito.PowerMockito;
37 import org.powermock.core.classloader.annotations.PrepareForTest;
38 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
39 import org.powermock.modules.junit4.PowerMockRunner;
42 import javax.servlet.http.HttpServletRequest;
43 import java.util.HashSet;
45 import java.util.UUID;
47 import static org.hamcrest.Matchers.is;
48 import static org.hamcrest.Matchers.nullValue;
49 import static org.junit.Assert.assertNull;
50 import static org.junit.Assert.assertThat;
51 import static org.mockito.Matchers.anyInt;
52 import static org.mockito.Mockito.mock;
53 import static org.mockito.Mockito.when;
54 import static org.powermock.api.mockito.PowerMockito.mockStatic;
56 @RunWith(PowerMockRunner.class)
57 @SuppressStaticInitializationFor({"org.onap.dmaap.datarouter.provisioning.beans.Feed",
58 "org.onap.dmaap.datarouter.provisioning.beans.Subscription",
59 "org.onap.dmaap.datarouter.provisioning.beans.Group",
60 "org.onap.dmaap.datarouter.provisioning.BaseServlet"})
61 @PrepareForTest({ UUID.class})
62 public class BaseServletTest extends DrServletTestBase {
64 private BaseServlet baseServlet;
67 private HttpServletRequest request;
70 public void setUp() throws Exception {
72 baseServlet = new BaseServlet();
77 public void Given_Request_Path_Info_Is_Valid_Then_Id_Is_Extracted_Correctly() {
78 when(request.getPathInfo()).thenReturn("/123");
79 assertThat(baseServlet.getIdFromPath(request), is(123));
83 public void Given_Request_Path_Info_Is_Not_Valid_Then_Minus_One_Is_Returned() {
84 when(request.getPathInfo()).thenReturn("/abc");
85 assertThat(baseServlet.getIdFromPath(request), is(-1));
86 when(request.getPathInfo()).thenReturn("/");
87 assertThat(baseServlet.getIdFromPath(request), is(-1));
91 public void Given_Remote_Address_Is_Known_And_RequireCerts_Is_True() throws Exception {
92 when(request.isSecure()).thenReturn(true);
93 Set<String> authAddressesAndNetworks = new HashSet<String>();
94 authAddressesAndNetworks.add(("127.0.0.1"));
95 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authorizedAddressesAndNetworks", authAddressesAndNetworks, true);
96 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "requireCert", true, true);
97 assertNull(baseServlet.isAuthorizedForProvisioning(request));
101 public void Given_Request_Is_GetFeedOwner_And_Feed_Exists() throws Exception {
102 PowerMockito.mockStatic(Feed.class);
103 Feed feed = mock(Feed.class);
104 PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(feed);
105 when(feed.getPublisher()).thenReturn("stub_publisher");
106 assertThat(baseServlet.getFeedOwner("3"), is("stub_publisher"));
110 public void Given_Request_Is_GetFeedOwner_And_Feed_Does_Not_Exist() throws Exception {
111 PowerMockito.mockStatic(Feed.class);
112 PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(null);
113 assertThat(baseServlet.getFeedOwner("3"), is(nullValue()));
117 public void Given_Request_Is_GetFeedClassification_And_Feed_Exists() throws Exception {
118 PowerMockito.mockStatic(Feed.class);
119 Feed feed = mock(Feed.class);
120 PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(feed);
121 FeedAuthorization fAuth = mock(FeedAuthorization.class);
122 when(feed.getAuthorization()).thenReturn(fAuth);
123 when(fAuth.getClassification()).thenReturn("stub_classification");
124 assertThat(baseServlet.getFeedClassification("3"), is("stub_classification"));
128 public void Given_Request_Is_GetFeedClassification_And_Feed_Does_Not_Exist() throws Exception {
129 PowerMockito.mockStatic(Feed.class);
130 PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(null);
131 assertThat(baseServlet.getFeedClassification("3"), is(nullValue()));
135 public void Given_Request_Is_GetSubscriptionOwner_And_Subscription_Exists() throws Exception {
136 PowerMockito.mockStatic(Subscription.class);
137 Subscription subscription = mock(Subscription.class);
138 PowerMockito.when(Subscription.getSubscriptionById(anyInt())).thenReturn(subscription);
139 when(subscription.getSubscriber()).thenReturn("stub_subscriber");
140 assertThat(baseServlet.getSubscriptionOwner("3"), is("stub_subscriber"));
144 public void Given_Request_Is_GetSubscriptionOwner_And_Subscription_Does_Not_Exist() throws Exception {
145 PowerMockito.mockStatic(Subscription.class);
146 PowerMockito.when(Subscription.getSubscriptionById(anyInt())).thenReturn(null);
147 assertThat(baseServlet.getSubscriptionOwner("3"), is(nullValue()));
151 public void Given_Request_Is_GetGroupByFeedGroupId_And_User_Is_A_Member_Of_Group() throws Exception {
152 PowerMockito.mockStatic(Feed.class);
153 Feed feed = mock(Feed.class);
154 PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(feed);
155 when(feed.getGroupid()).thenReturn(3);
156 PowerMockito.mockStatic(Group.class);
157 Group group = mock(Group.class);
158 when(group.getMembers()).thenReturn("{id: stub_user}");
159 PowerMockito.when(Group.getGroupById(anyInt())).thenReturn(group);
160 when(group.getAuthid()).thenReturn("stub_authID");
161 assertThat(baseServlet.getGroupByFeedGroupId("stub_user", "3"), is("stub_authID"));
165 public void Given_Request_Is_GetGroupByFeedGroupId_And_User_Is_Not_A_Member_Of_Group() throws Exception {
166 PowerMockito.mockStatic(Feed.class);
167 Feed feed = mock(Feed.class);
168 PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(feed);
169 when(feed.getGroupid()).thenReturn(3);
170 PowerMockito.mockStatic(Group.class);
171 Group group = mock(Group.class);
172 when(group.getMembers()).thenReturn("{id: stub_otherUser}");
173 PowerMockito.when(Group.getGroupById(anyInt())).thenReturn(group);
174 when(group.getAuthid()).thenReturn("stub_authID");
175 assertThat(baseServlet.getGroupByFeedGroupId("stub_user", "3"), is(nullValue()));
179 public void Given_Request_Is_GetGroupBySubGroupId_And_User_Is_A_Member_Of_Group() throws Exception {
180 PowerMockito.mockStatic(Subscription.class);
181 Subscription subscription = mock(Subscription.class);
182 PowerMockito.when(Subscription.getSubscriptionById(anyInt())).thenReturn(subscription);
183 when(subscription.getGroupid()).thenReturn(3);
184 PowerMockito.mockStatic(Group.class);
185 Group group = mock(Group.class);
186 when(group.getMembers()).thenReturn("{id: stub_user}");
187 PowerMockito.when(Group.getGroupById(anyInt())).thenReturn(group);
188 when(group.getAuthid()).thenReturn("stub_authID");
189 assertThat(baseServlet.getGroupBySubGroupId("stub_user", "3"), is("stub_authID"));
193 public void Given_Request_Is_GetGroupBySubGroupId_And_User_Is_Not_A_Member_Of_Group() throws Exception {
194 PowerMockito.mockStatic(Subscription.class);
195 Subscription subscription = mock(Subscription.class);
196 PowerMockito.when(Subscription.getSubscriptionById(anyInt())).thenReturn(subscription);
197 when(subscription.getGroupid()).thenReturn(3);
198 PowerMockito.mockStatic(Group.class);
199 Group group = mock(Group.class);
200 when(group.getMembers()).thenReturn("{id: stub_otherUser}");
201 PowerMockito.when(Group.getGroupById(anyInt())).thenReturn(group);
202 when(group.getAuthid()).thenReturn("stub_authID");
203 assertThat(baseServlet.getGroupBySubGroupId("stub_user", "3"), is(nullValue()));
207 public void Given_Request_Has_Empty_RequestId_And_InvocationId_Headers_Generate_MDC_Values() {
208 when(request.getHeader("X-ONAP-RequestID")).thenReturn("");
209 when(request.getHeader("X-InvocationID")).thenReturn("");
210 mockStatic(UUID.class);
211 when(UUID.randomUUID().toString()).thenReturn("123", "456");
212 baseServlet.setIpFqdnRequestIDandInvocationIDForEelf("doDelete", request);
213 Assert.assertEquals("123", MDC.get("RequestId"));
214 Assert.assertEquals("456", MDC.get("InvocationId"));
218 public void Given_Request_Has_RequestId_And_InvocationId_Headers_Set_MDC_Values() {
219 when(request.getHeader("X-ONAP-RequestID")).thenReturn("123");
220 when(request.getHeader("X-InvocationID")).thenReturn("456");
221 baseServlet.setIpFqdnRequestIDandInvocationIDForEelf("doDelete", request);
222 Assert.assertEquals("123", MDC.get("RequestId"));
223 Assert.assertEquals("456", MDC.get("InvocationId"));