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.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.Mock;
31 import org.onap.dmaap.datarouter.provisioning.beans.Feed;
32 import org.onap.dmaap.datarouter.provisioning.beans.FeedAuthorization;
33 import org.onap.dmaap.datarouter.provisioning.beans.Group;
34 import org.onap.dmaap.datarouter.provisioning.beans.Subscription;
35 import org.powermock.api.mockito.PowerMockito;
36 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
37 import org.powermock.modules.junit4.PowerMockRunner;
38 import javax.servlet.http.HttpServletRequest;
39 import java.util.HashSet;
41 import static org.hamcrest.Matchers.is;
42 import static org.hamcrest.Matchers.nullValue;
43 import static org.junit.Assert.assertNull;
44 import static org.junit.Assert.assertThat;
45 import static org.mockito.Matchers.anyInt;
46 import static org.mockito.Mockito.mock;
47 import static org.mockito.Mockito.when;
49 @RunWith(PowerMockRunner.class)
50 @SuppressStaticInitializationFor({"org.onap.dmaap.datarouter.provisioning.beans.Feed",
51 "org.onap.dmaap.datarouter.provisioning.beans.Subscription",
52 "org.onap.dmaap.datarouter.provisioning.beans.Group"})
53 public class BaseServletTest extends DrServletTestBase {
55 private BaseServlet baseServlet;
58 private HttpServletRequest request;
61 public void setUp() throws Exception {
63 baseServlet = new BaseServlet();
68 public void Given_Request_Path_Info_Is_Valid_Then_Id_Is_Extracted_Correctly() {
69 when(request.getPathInfo()).thenReturn("/123");
70 assertThat(baseServlet.getIdFromPath(request), is(123));
74 public void Given_Request_Path_Info_Is_Not_Valid_Then_Minus_One_Is_Returned() {
75 when(request.getPathInfo()).thenReturn("/abc");
76 assertThat(baseServlet.getIdFromPath(request), is(-1));
77 when(request.getPathInfo()).thenReturn("/");
78 assertThat(baseServlet.getIdFromPath(request), is(-1));
82 public void Given_Remote_Address_Is_Known_And_RequireCerts_Is_True() throws Exception {
83 when(request.isSecure()).thenReturn(true);
84 Set<String> authAddressesAndNetworks = new HashSet<String>();
85 authAddressesAndNetworks.add(("127.0.0.1"));
86 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authorizedAddressesAndNetworks", authAddressesAndNetworks, true);
87 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "requireCert", true, true);
88 assertNull(baseServlet.isAuthorizedForProvisioning(request));
92 public void Given_Request_Is_GetFeedOwner_And_Feed_Exists() throws Exception {
93 PowerMockito.mockStatic(Feed.class);
94 Feed feed = mock(Feed.class);
95 PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(feed);
96 when(feed.getPublisher()).thenReturn("stub_publisher");
97 assertThat(baseServlet.getFeedOwner("3"), is("stub_publisher"));
101 public void Given_Request_Is_GetFeedOwner_And_Feed_Does_Not_Exist() throws Exception {
102 PowerMockito.mockStatic(Feed.class);
103 PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(null);
104 assertThat(baseServlet.getFeedOwner("3"), is(nullValue()));
108 public void Given_Request_Is_GetFeedClassification_And_Feed_Exists() throws Exception {
109 PowerMockito.mockStatic(Feed.class);
110 Feed feed = mock(Feed.class);
111 PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(feed);
112 FeedAuthorization fAuth = mock(FeedAuthorization.class);
113 when(feed.getAuthorization()).thenReturn(fAuth);
114 when(fAuth.getClassification()).thenReturn("stub_classification");
115 assertThat(baseServlet.getFeedClassification("3"), is("stub_classification"));
119 public void Given_Request_Is_GetFeedClassification_And_Feed_Does_Not_Exist() throws Exception {
120 PowerMockito.mockStatic(Feed.class);
121 PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(null);
122 assertThat(baseServlet.getFeedClassification("3"), is(nullValue()));
126 public void Given_Request_Is_GetSubscriptionOwner_And_Subscription_Exists() throws Exception {
127 PowerMockito.mockStatic(Subscription.class);
128 Subscription subscription = mock(Subscription.class);
129 PowerMockito.when(Subscription.getSubscriptionById(anyInt())).thenReturn(subscription);
130 when(subscription.getSubscriber()).thenReturn("stub_subscriber");
131 assertThat(baseServlet.getSubscriptionOwner("3"), is("stub_subscriber"));
135 public void Given_Request_Is_GetSubscriptionOwner_And_Subscription_Does_Not_Exist() throws Exception {
136 PowerMockito.mockStatic(Subscription.class);
137 PowerMockito.when(Subscription.getSubscriptionById(anyInt())).thenReturn(null);
138 assertThat(baseServlet.getSubscriptionOwner("3"), is(nullValue()));
142 public void Given_Request_Is_GetGroupByFeedGroupId_And_User_Is_A_Member_Of_Group() throws Exception {
143 PowerMockito.mockStatic(Feed.class);
144 Feed feed = mock(Feed.class);
145 PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(feed);
146 when(feed.getGroupid()).thenReturn(3);
147 PowerMockito.mockStatic(Group.class);
148 Group group = mock(Group.class);
149 when(group.getMembers()).thenReturn("{id: stub_user}");
150 PowerMockito.when(Group.getGroupById(anyInt())).thenReturn(group);
151 when(group.getAuthid()).thenReturn("stub_authID");
152 assertThat(baseServlet.getGroupByFeedGroupId("stub_user", "3"), is("stub_authID"));
156 public void Given_Request_Is_GetGroupByFeedGroupId_And_User_Is_Not_A_Member_Of_Group() throws Exception {
157 PowerMockito.mockStatic(Feed.class);
158 Feed feed = mock(Feed.class);
159 PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(feed);
160 when(feed.getGroupid()).thenReturn(3);
161 PowerMockito.mockStatic(Group.class);
162 Group group = mock(Group.class);
163 when(group.getMembers()).thenReturn("{id: stub_otherUser}");
164 PowerMockito.when(Group.getGroupById(anyInt())).thenReturn(group);
165 when(group.getAuthid()).thenReturn("stub_authID");
166 assertThat(baseServlet.getGroupByFeedGroupId("stub_user", "3"), is(nullValue()));
170 public void Given_Request_Is_GetGroupBySubGroupId_And_User_Is_A_Member_Of_Group() throws Exception {
171 PowerMockito.mockStatic(Subscription.class);
172 Subscription subscription = mock(Subscription.class);
173 PowerMockito.when(Subscription.getSubscriptionById(anyInt())).thenReturn(subscription);
174 when(subscription.getGroupid()).thenReturn(3);
175 PowerMockito.mockStatic(Group.class);
176 Group group = mock(Group.class);
177 when(group.getMembers()).thenReturn("{id: stub_user}");
178 PowerMockito.when(Group.getGroupById(anyInt())).thenReturn(group);
179 when(group.getAuthid()).thenReturn("stub_authID");
180 assertThat(baseServlet.getGroupBySubGroupId("stub_user", "3"), is("stub_authID"));
184 public void Given_Request_Is_GetGroupBySubGroupId_And_User_Is_Not_A_Member_Of_Group() throws Exception {
185 PowerMockito.mockStatic(Subscription.class);
186 Subscription subscription = mock(Subscription.class);
187 PowerMockito.when(Subscription.getSubscriptionById(anyInt())).thenReturn(subscription);
188 when(subscription.getGroupid()).thenReturn(3);
189 PowerMockito.mockStatic(Group.class);
190 Group group = mock(Group.class);
191 when(group.getMembers()).thenReturn("{id: stub_otherUser}");
192 PowerMockito.when(Group.getGroupById(anyInt())).thenReturn(group);
193 when(group.getAuthid()).thenReturn("stub_authID");
194 assertThat(baseServlet.getGroupBySubGroupId("stub_user", "3"), is(nullValue()));