2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 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
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=========================================================
21 package org.onap.policy.common.endpoints.http.server.test;
23 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.mockito.Matchers.any;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.never;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
32 import java.util.Arrays;
33 import java.util.Properties;
34 import javax.servlet.http.HttpServletRequest;
35 import org.junit.AfterClass;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.mockito.ArgumentCaptor;
40 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
41 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactory;
42 import org.onap.policy.common.endpoints.http.server.RestServer;
43 import org.onap.policy.common.endpoints.http.server.RestServer.Factory;
44 import org.onap.policy.common.endpoints.http.server.aaf.AafAuthFilter;
45 import org.onap.policy.common.endpoints.parameters.RestServerParameters;
46 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
47 import org.onap.policy.common.gson.GsonMessageBodyHandler;
48 import org.powermock.reflect.Whitebox;
50 public class RestServerTest {
51 private static final String SERVER1 = "my-server-A";
52 private static final String SERVER2 = "my-server-B";
53 private static final String FACTORY_FIELD = "factory";
54 private static final String HOST = "my-host";
55 private static final String PARAM_NAME = "my-param";
56 private static final String PASS = "my-pass";
57 private static final Integer PORT = 9876;
58 private static final String USER = "my-user";
59 private static Factory saveFactory;
61 private RestServer rest;
62 private HttpServletServer server1;
63 private HttpServletServer server2;
64 private Factory factory;
65 private HttpServletServerFactory serverFactory;
66 private RestServerParameters params;
69 public static void setUpBeforeClass() {
70 saveFactory = Whitebox.getInternalState(RestServer.class, FACTORY_FIELD);
74 public static void tearDownAfterClass() {
75 Whitebox.setInternalState(RestServer.class, FACTORY_FIELD, saveFactory);
83 server1 = mock(HttpServletServer.class);
84 server2 = mock(HttpServletServer.class);
85 factory = mock(Factory.class);
86 serverFactory = mock(HttpServletServerFactory.class);
87 params = mock(RestServerParameters.class);
89 when(factory.getServerFactory()).thenReturn(serverFactory);
90 when(serverFactory.build(any())).thenReturn(Arrays.asList(server1, server2));
92 when(server1.getName()).thenReturn(SERVER1);
93 when(server2.getName()).thenReturn(SERVER2);
95 when(params.getHost()).thenReturn(HOST);
96 when(params.getName()).thenReturn(PARAM_NAME);
97 when(params.getPassword()).thenReturn(PASS);
98 when(params.getPort()).thenReturn(PORT);
99 when(params.getUserName()).thenReturn(USER);
100 when(params.isAaf()).thenReturn(true);
101 when(params.isHttps()).thenReturn(true);
103 Whitebox.setInternalState(RestServer.class, FACTORY_FIELD, factory);
107 public void testRestServer() {
108 rest = new RestServer(params, Filter.class, Provider1.class, Provider2.class);
111 verify(server1).start();
112 verify(server2).start();
115 verify(server1).stop();
116 verify(server2).stop();
120 public void testRestServer_NoAaf() {
121 rest = new RestServer(params, Filter.class, Provider1.class, Provider2.class);
122 verify(server1, never()).addFilterClass(any(), any());
123 verify(server2, never()).addFilterClass(any(), any());
127 public void testRestServer_OnlyOneAaf() {
128 when(server2.isAaf()).thenReturn(true);
130 rest = new RestServer(params, Filter.class, Provider1.class, Provider2.class);
132 verify(server1, never()).addFilterClass(any(), any());
133 verify(server2).addFilterClass(null, Filter.class.getName());
137 public void testRestServer_BothAaf() {
138 when(server1.isAaf()).thenReturn(true);
139 when(server2.isAaf()).thenReturn(true);
141 rest = new RestServer(params, Filter.class, Provider1.class, Provider2.class);
143 verify(server1).addFilterClass(null, Filter.class.getName());
144 verify(server2).addFilterClass(null, Filter.class.getName());
148 public void testRestServer_BothAaf_NoFilter() {
149 when(server1.isAaf()).thenReturn(true);
150 when(server2.isAaf()).thenReturn(true);
152 rest = new RestServer(params, null, Provider1.class, Provider2.class);
154 verify(server1, never()).addFilterClass(any(), any());
155 verify(server2, never()).addFilterClass(any(), any());
159 public void testRestServer_MissingProviders() {
160 assertThatIllegalArgumentException().isThrownBy(() -> new RestServer(params, Filter.class));
164 public void testGetServerProperties_testGetProviderNames() {
165 rest = new RestServer(params, Filter.class, Provider1.class, Provider2.class);
167 ArgumentCaptor<Properties> cap = ArgumentCaptor.forClass(Properties.class);
168 verify(serverFactory).build(cap.capture());
170 Properties props = cap.getValue();
171 String svcpfx = PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + PARAM_NAME;
173 assertEquals(HOST, props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX));
174 assertEquals(String.valueOf(PORT),
175 props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX));
176 assertEquals(Provider1.class.getName() + "," + Provider2.class.getName(),
177 props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX));
178 assertEquals("false", props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX));
179 assertEquals("true", props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX));
180 assertEquals(USER, props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX));
181 assertEquals(PASS, props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX));
182 assertEquals("true", props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX));
183 assertEquals("true", props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX));
184 assertEquals(GsonMessageBodyHandler.class.getName(),
185 props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER));
189 public void testToString() {
190 rest = new RestServer(params, Filter.class, Provider1.class, Provider2.class);
191 assertNotNull(rest.toString());
195 public void testFactory() {
196 assertNotNull(saveFactory);
197 assertNotNull(saveFactory.getServerFactory());
200 private static class Filter extends AafAuthFilter {
202 protected String getPermissionType(HttpServletRequest request) {
207 protected String getPermissionInstance(HttpServletRequest request) {
212 private static class Provider1 {
213 private Provider1() {
218 private static class Provider2 {
219 private Provider2() {