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.apache.log4j.Logger;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mock;
32 import org.mockito.runners.MockitoJUnitRunner;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletResponse;
36 import java.util.HashSet;
39 import static org.hamcrest.Matchers.is;
40 import static org.hamcrest.Matchers.nullValue;
41 import static org.junit.Assert.assertThat;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.when;
45 @RunWith(MockitoJUnitRunner.class)
46 public class BaseServletTest {
47 private BaseServlet baseServlet;
50 private HttpServletRequest request;
53 public void setUp() throws Exception {
54 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "startmsg_flag", false, true);
55 SynchronizerTask synchronizerTask = mock(SynchronizerTask.class);
56 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "synctask", synchronizerTask, true);
57 baseServlet = new BaseServlet();
62 public void Given_Request_Path_Info_Is_Valid_Then_Id_Is_Extracted_Correctly() throws Exception {
63 when(request.getPathInfo()).thenReturn("/123");
64 assertThat(baseServlet.getIdFromPath(request), is(123));
67 public void Given_Request_Path_Info_Is_Not_Valid_Then_Minus_One_Is_Returned() throws Exception {
68 when(request.getPathInfo()).thenReturn("/abc");
69 assertThat(baseServlet.getIdFromPath(request), is(-1));
70 when(request.getPathInfo()).thenReturn("/");
71 assertThat(baseServlet.getIdFromPath(request), is(-1));
75 public void Given_Request_Path_Info_Is_Not_Valid_Then_Minus_One_Is() throws Exception {
76 when(request.isSecure()).thenReturn(true);
77 Set<String> authAddressesAndNetworks = new HashSet<String>();
78 authAddressesAndNetworks.add(("127.0.0.1"));
79 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authorizedAddressesAndNetworks", authAddressesAndNetworks, true);
80 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "require_cert", false, true);
81 assertThat(baseServlet.isAuthorizedForProvisioning(request), is(nullValue()));