aboutsummaryrefslogtreecommitdiffstats
path: root/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/RestServerTest.java
blob: 16f1765229d9e5a89c8539fd9647e236fa41455b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.common.endpoints.http.server.test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;
import java.util.Properties;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import lombok.Getter;
import org.apache.commons.io.IOUtils;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.onap.policy.common.endpoints.http.server.HttpServletServer;
import org.onap.policy.common.endpoints.http.server.HttpServletServerFactory;
import org.onap.policy.common.endpoints.http.server.JsonExceptionMapper;
import org.onap.policy.common.endpoints.http.server.RestServer;
import org.onap.policy.common.endpoints.http.server.RestServer.Factory;
import org.onap.policy.common.endpoints.http.server.YamlExceptionMapper;
import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler;
import org.onap.policy.common.endpoints.http.server.aaf.AafAuthFilter;
import org.onap.policy.common.endpoints.parameters.RestServerParameters;
import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
import org.onap.policy.common.gson.GsonMessageBodyHandler;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.common.utils.network.NetworkUtil;
import org.powermock.reflect.Whitebox;

public class RestServerTest {
    private static final String SERVER1 = "my-server-A";
    private static final String SERVER2 = "my-server-B";
    private static final String FACTORY_FIELD = "factory";
    private static final String HOST = "my-host";
    private static final String PARAM_NAME = "my-param";
    private static final String PASS = "my-pass";
    private static final Integer PORT = 9876;
    private static final String USER = "my-user";

    private static Factory saveFactory;
    private static RestServer realRest;
    private static int realPort;
    private static RestServerParameters params;

    private RestServer rest;
    private HttpServletServer server1;
    private HttpServletServer server2;
    private Factory factory;
    private HttpServletServerFactory serverFactory;
    private String errorMsg;

    /**
     * Starts the REST server.
     * @throws Exception if an error occurs
     */
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        saveFactory = Whitebox.getInternalState(RestServer.class, FACTORY_FIELD);

        realPort = NetworkUtil.allocPort();

        initRealParams();

        realRest = new RestServer(params, null, RealProvider.class) {
            @Override
            protected Properties getServerProperties(RestServerParameters restServerParameters, String names) {
                Properties props = super.getServerProperties(restServerParameters, names);

                String svcpfx = PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
                                + restServerParameters.getName();
                props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, "false");

                return props;
            }
        };

        realRest.start();
        assertTrue(NetworkUtil.isTcpPortOpen(params.getHost(), params.getPort(), 100, 100));
    }

    /**
     * Restores the factory and stops the REST server.
     */
    @AfterClass
    public static void tearDownAfterClass() {
        Whitebox.setInternalState(RestServer.class, FACTORY_FIELD, saveFactory);

        realRest.stop();
    }

    /**
     * Initializes mocks.
     */
    @Before
    public void setUp() {
        server1 = mock(HttpServletServer.class);
        server2 = mock(HttpServletServer.class);
        factory = mock(Factory.class);
        serverFactory = mock(HttpServletServerFactory.class);

        initParams();

        when(factory.getServerFactory()).thenReturn(serverFactory);
        when(serverFactory.build(any())).thenReturn(Arrays.asList(server1, server2));

        when(server1.getName()).thenReturn(SERVER1);
        when(server2.getName()).thenReturn(SERVER2);

        Whitebox.setInternalState(RestServer.class, FACTORY_FIELD, factory);
    }

    @Test
    public void testRestServer() {
        rest = new RestServer(params, Filter.class, Provider1.class, Provider2.class);

        rest.start();
        verify(server1).start();
        verify(server2).start();

        rest.stop();
        verify(server1).stop();
        verify(server2).stop();
    }

    @Test
    public void testRestServer_NoAaf() {
        rest = new RestServer(params, Filter.class, Provider1.class, Provider2.class);
        verify(server1, never()).addFilterClass(any(), any());
        verify(server2, never()).addFilterClass(any(), any());
    }

    @Test
    public void testRestServer_OnlyOneAaf() {
        when(server2.isAaf()).thenReturn(true);

        rest = new RestServer(params, Filter.class, Provider1.class, Provider2.class);

        verify(server1, never()).addFilterClass(any(), any());
        verify(server2).addFilterClass(null, Filter.class.getName());
    }

    @Test
    public void testRestServer_BothAaf() {
        when(server1.isAaf()).thenReturn(true);
        when(server2.isAaf()).thenReturn(true);

        rest = new RestServer(params, Filter.class, Provider1.class, Provider2.class);

        verify(server1).addFilterClass(null, Filter.class.getName());
        verify(server2).addFilterClass(null, Filter.class.getName());
    }

    @Test
    public void testRestServer_BothAaf_NoFilter() {
        when(server1.isAaf()).thenReturn(true);
        when(server2.isAaf()).thenReturn(true);

        rest = new RestServer(params, null, Provider1.class, Provider2.class);

        verify(server1, never()).addFilterClass(any(), any());
        verify(server2, never()).addFilterClass(any(), any());
    }

    @Test
    public void testRestServer_MissingProviders() {
        assertThatIllegalArgumentException().isThrownBy(() -> new RestServer(params, Filter.class));
    }

    @Test
    public void testGetServerProperties_testGetProviderNames() {
        rest = new RestServer(params, Filter.class, Provider1.class, Provider2.class);

        ArgumentCaptor<Properties> cap = ArgumentCaptor.forClass(Properties.class);
        verify(serverFactory).build(cap.capture());

        Properties props = cap.getValue();
        String svcpfx = PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + PARAM_NAME;

        assertEquals(HOST, props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX));
        assertEquals(String.valueOf(PORT),
                        props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX));
        assertEquals(Provider1.class.getName() + "," + Provider2.class.getName(),
                        props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX));
        assertEquals("false", props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX));
        assertEquals("true", props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX));
        assertEquals(USER, props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX));
        assertEquals(PASS, props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX));
        assertEquals("true", props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX));
        assertEquals("true", props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX));
        assertEquals(String.join(",", GsonMessageBodyHandler.class.getName(), YamlMessageBodyHandler.class.getName(),
                        JsonExceptionMapper.class.getName(), YamlExceptionMapper.class.getName()),
                        props.getProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER));
    }

    @Test
    public void testInvalidJson() throws Exception {
        initRealParams();

        assertEquals(200, roundTrip(new StandardCoder().encode(new MyRequest())));
        assertEquals(400, roundTrip("{'bogus-json'"));
        assertThat(errorMsg).contains("Invalid request");
    }

    @Test
    public void testInvalidYaml() throws Exception {
        initRealParams();

        assertEquals(200, roundTrip(new StandardCoder().encode(new MyRequest()),
                        YamlMessageBodyHandler.APPLICATION_YAML));
        assertEquals(400, roundTrip("<bogus yaml", YamlMessageBodyHandler.APPLICATION_YAML));
        assertThat(errorMsg).contains("Invalid request");
    }

    private int roundTrip(String request) throws IOException {
        return roundTrip(request, MediaType.APPLICATION_JSON);
    }

    private int roundTrip(String request, String mediaType) throws IOException {
        URL url = new URL("http://" + params.getHost() + ":" + params.getPort() + "/request");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        String auth = params.getUserName() + ":" + params.getPassword();
        conn.setRequestProperty("Authorization", "Basic " + Base64.getEncoder().encodeToString(auth.getBytes()));
        conn.setRequestProperty("Content-type", mediaType);
        conn.setRequestProperty("Accept", mediaType);
        conn.connect();

        try (PrintWriter wtr = new PrintWriter(conn.getOutputStream())) {
            wtr.write(request);
        }

        int code = conn.getResponseCode();

        if (code == 200) {
            errorMsg = "";
        } else {
            errorMsg = IOUtils.toString(conn.getErrorStream(), StandardCharsets.UTF_8);
        }

        return code;
    }

    @Test
    public void testToString() {
        rest = new RestServer(params, Filter.class, Provider1.class, Provider2.class);
        assertNotNull(rest.toString());
    }

    @Test
    public void testFactory() {
        assertNotNull(saveFactory);
        assertNotNull(saveFactory.getServerFactory());
    }

    private static void initRealParams() {
        initParams();

        when(params.getHost()).thenReturn("localhost");
        when(params.getPort()).thenReturn(realPort);
        when(params.isHttps()).thenReturn(false);
        when(params.isAaf()).thenReturn(false);
    }

    private static void initParams() {
        params = mock(RestServerParameters.class);

        when(params.getHost()).thenReturn(HOST);
        when(params.getName()).thenReturn(PARAM_NAME);
        when(params.getPassword()).thenReturn(PASS);
        when(params.getPort()).thenReturn(PORT);
        when(params.getUserName()).thenReturn(USER);
        when(params.isAaf()).thenReturn(true);
        when(params.isHttps()).thenReturn(true);
    }

    private static class Filter extends AafAuthFilter {
        @Override
        protected String getPermissionType(HttpServletRequest request) {
            return "";
        }

        @Override
        protected String getPermissionInstance(HttpServletRequest request) {
            return "";
        }
    }

    private static class Provider1 {
        private Provider1() {
            // do nothing
        }
    }

    private static class Provider2 {
        private Provider2() {
            // do nothing
        }
    }

    @Path("/")
    @Produces({MediaType.APPLICATION_JSON, YamlMessageBodyHandler.APPLICATION_YAML})
    @Consumes({MediaType.APPLICATION_JSON, YamlMessageBodyHandler.APPLICATION_YAML})
    public static class RealProvider {
        @POST
        @Path("/request")
        public Response decision(MyRequest body) {
            return Response.status(Response.Status.OK).entity(new MyResponse()).build();
        }
    }

    @Getter
    public static class MyRequest {
        private String data;
    }

    @Getter
    public static class MyResponse {
        private String text = "hello";
    }
}