Merge "Fixed Sonar issues"
[dmaap/datarouter.git] / datarouter-prov / src / test / java / org / onap / dmaap / datarouter / provisioning / BaseServletTest.java
1 /*******************************************************************************
2  * ============LICENSE_START==================================================
3  * * org.onap.dmaap
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
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  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  * *
22  ******************************************************************************/
23
24 package org.onap.dmaap.datarouter.provisioning;
25
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;
40 import java.util.Set;
41 import static org.hamcrest.Matchers.is;
42 import static org.hamcrest.Matchers.nullValue;
43 import static org.junit.Assert.assertThat;
44 import static org.mockito.Matchers.anyInt;
45 import static org.mockito.Mockito.mock;
46 import static org.mockito.Mockito.when;
47
48 @RunWith(PowerMockRunner.class)
49 @SuppressStaticInitializationFor({"org.onap.dmaap.datarouter.provisioning.beans.Feed",
50         "org.onap.dmaap.datarouter.provisioning.beans.Subscription",
51         "org.onap.dmaap.datarouter.provisioning.beans.Group"})
52 public class BaseServletTest extends DrServletTestBase {
53
54     private BaseServlet baseServlet;
55
56     @Mock
57     private HttpServletRequest request;
58
59     @Before
60     public void setUp() throws Exception {
61         super.setUp();
62         baseServlet = new BaseServlet();
63     }
64
65
66     @Test
67     public void Given_Request_Path_Info_Is_Valid_Then_Id_Is_Extracted_Correctly() {
68         when(request.getPathInfo()).thenReturn("/123");
69         assertThat(baseServlet.getIdFromPath(request), is(123));
70     }
71
72     @Test
73     public void Given_Request_Path_Info_Is_Not_Valid_Then_Minus_One_Is_Returned() {
74         when(request.getPathInfo()).thenReturn("/abc");
75         assertThat(baseServlet.getIdFromPath(request), is(-1));
76         when(request.getPathInfo()).thenReturn("/");
77         assertThat(baseServlet.getIdFromPath(request), is(-1));
78     }
79
80     @Test
81     public void Given_Remote_Address_Is_Known_And_RequireCerts_Is_True() throws Exception {
82         when(request.isSecure()).thenReturn(true);
83         Set<String> authAddressesAndNetworks = new HashSet<String>();
84         authAddressesAndNetworks.add(("127.0.0.1"));
85         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authorizedAddressesAndNetworks", authAddressesAndNetworks, true);
86         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "requireCert", true, true);
87         assertThat(baseServlet.isAuthorizedForProvisioning(request), is("Client certificate is missing."));
88     }
89
90     @Test
91     public void Given_Request_Is_GetFeedOwner_And_Feed_Exists() throws Exception {
92         PowerMockito.mockStatic(Feed.class);
93         Feed feed = mock(Feed.class);
94         PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(feed);
95         when(feed.getPublisher()).thenReturn("stub_publisher");
96         assertThat(baseServlet.getFeedOwner("3"), is("stub_publisher"));
97     }
98
99     @Test
100     public void Given_Request_Is_GetFeedOwner_And_Feed_Does_Not_Exist() throws Exception {
101         PowerMockito.mockStatic(Feed.class);
102         PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(null);
103         assertThat(baseServlet.getFeedOwner("3"), is(nullValue()));
104     }
105
106     @Test
107     public void Given_Request_Is_GetFeedClassification_And_Feed_Exists() throws Exception {
108         PowerMockito.mockStatic(Feed.class);
109         Feed feed = mock(Feed.class);
110         PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(feed);
111         FeedAuthorization fAuth = mock(FeedAuthorization.class);
112         when(feed.getAuthorization()).thenReturn(fAuth);
113         when(fAuth.getClassification()).thenReturn("stub_classification");
114         assertThat(baseServlet.getFeedClassification("3"), is("stub_classification"));
115     }
116
117     @Test
118     public void Given_Request_Is_GetFeedClassification_And_Feed_Does_Not_Exist() throws Exception {
119         PowerMockito.mockStatic(Feed.class);
120         PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(null);
121         assertThat(baseServlet.getFeedClassification("3"), is(nullValue()));
122     }
123
124     @Test
125     public void Given_Request_Is_GetSubscriptionOwner_And_Subscription_Exists() throws Exception {
126         PowerMockito.mockStatic(Subscription.class);
127         Subscription subscription = mock(Subscription.class);
128         PowerMockito.when(Subscription.getSubscriptionById(anyInt())).thenReturn(subscription);
129         when(subscription.getSubscriber()).thenReturn("stub_subscriber");
130         assertThat(baseServlet.getSubscriptionOwner("3"), is("stub_subscriber"));
131     }
132
133     @Test
134     public void Given_Request_Is_GetSubscriptionOwner_And_Subscription_Does_Not_Exist() throws Exception {
135         PowerMockito.mockStatic(Subscription.class);
136         PowerMockito.when(Subscription.getSubscriptionById(anyInt())).thenReturn(null);
137         assertThat(baseServlet.getSubscriptionOwner("3"), is(nullValue()));
138     }
139
140     @Test
141     public void Given_Request_Is_GetGroupByFeedGroupId_And_User_Is_A_Member_Of_Group() throws Exception {
142         PowerMockito.mockStatic(Feed.class);
143         Feed feed = mock(Feed.class);
144         PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(feed);
145         when(feed.getGroupid()).thenReturn(3);
146         PowerMockito.mockStatic(Group.class);
147         Group group = mock(Group.class);
148         when(group.getMembers()).thenReturn("{id: stub_user}");
149         PowerMockito.when(Group.getGroupById(anyInt())).thenReturn(group);
150         when(group.getAuthid()).thenReturn("stub_authID");
151         assertThat(baseServlet.getGroupByFeedGroupId("stub_user", "3"), is("stub_authID"));
152     }
153
154     @Test
155     public void Given_Request_Is_GetGroupByFeedGroupId_And_User_Is_Not_A_Member_Of_Group() throws Exception {
156         PowerMockito.mockStatic(Feed.class);
157         Feed feed = mock(Feed.class);
158         PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(feed);
159         when(feed.getGroupid()).thenReturn(3);
160         PowerMockito.mockStatic(Group.class);
161         Group group = mock(Group.class);
162         when(group.getMembers()).thenReturn("{id: stub_otherUser}");
163         PowerMockito.when(Group.getGroupById(anyInt())).thenReturn(group);
164         when(group.getAuthid()).thenReturn("stub_authID");
165         assertThat(baseServlet.getGroupByFeedGroupId("stub_user", "3"), is(nullValue()));
166     }
167
168     @Test
169     public void Given_Request_Is_GetGroupBySubGroupId_And_User_Is_A_Member_Of_Group() throws Exception {
170         PowerMockito.mockStatic(Subscription.class);
171         Subscription subscription = mock(Subscription.class);
172         PowerMockito.when(Subscription.getSubscriptionById(anyInt())).thenReturn(subscription);
173         when(subscription.getGroupid()).thenReturn(3);
174         PowerMockito.mockStatic(Group.class);
175         Group group = mock(Group.class);
176         when(group.getMembers()).thenReturn("{id: stub_user}");
177         PowerMockito.when(Group.getGroupById(anyInt())).thenReturn(group);
178         when(group.getAuthid()).thenReturn("stub_authID");
179         assertThat(baseServlet.getGroupBySubGroupId("stub_user", "3"), is("stub_authID"));
180     }
181
182     @Test
183     public void Given_Request_Is_GetGroupBySubGroupId_And_User_Is_Not_A_Member_Of_Group() throws Exception {
184         PowerMockito.mockStatic(Subscription.class);
185         Subscription subscription = mock(Subscription.class);
186         PowerMockito.when(Subscription.getSubscriptionById(anyInt())).thenReturn(subscription);
187         when(subscription.getGroupid()).thenReturn(3);
188         PowerMockito.mockStatic(Group.class);
189         Group group = mock(Group.class);
190         when(group.getMembers()).thenReturn("{id: stub_otherUser}");
191         PowerMockito.when(Group.getGroupById(anyInt())).thenReturn(group);
192         when(group.getAuthid()).thenReturn("stub_authID");
193         assertThat(baseServlet.getGroupBySubGroupId("stub_user", "3"), is(nullValue()));
194     }
195 }