Update for OOM integration
[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.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.Mockito;
33 import org.mockito.runners.MockitoJUnitRunner;
34 import org.onap.dmaap.datarouter.provisioning.utils.DB;
35
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse;
38 import java.util.HashSet;
39 import java.util.Properties;
40 import java.util.Set;
41
42 import static org.hamcrest.Matchers.is;
43 import static org.hamcrest.Matchers.nullValue;
44 import static org.junit.Assert.assertThat;
45 import static org.mockito.Mockito.mock;
46 import static org.mockito.Mockito.when;
47
48 @RunWith(MockitoJUnitRunner.class)
49 public class BaseServletTest {
50
51     private BaseServlet baseServlet;
52
53     @Mock
54     private HttpServletRequest request;
55
56     @Before
57     public void setUp() throws Exception {
58         Properties props = new Properties();
59         props.setProperty("org.onap.dmaap.datarouter.provserver.isaddressauthenabled", "false");
60         FieldUtils.writeDeclaredStaticField(DB.class, "props", props, true);
61         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "startmsgFlag", false, true);
62         SynchronizerTask synchronizerTask = mock(SynchronizerTask.class);
63         when(synchronizerTask.getState()).thenReturn(SynchronizerTask.UNKNOWN);
64         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "synctask", synchronizerTask, true);
65         baseServlet = new BaseServlet();
66     }
67
68
69     @Test
70     public void Given_Request_Path_Info_Is_Valid_Then_Id_Is_Extracted_Correctly() {
71         when(request.getPathInfo()).thenReturn("/123");
72         assertThat(baseServlet.getIdFromPath(request), is(123));
73     }
74
75     @Test
76     public void Given_Request_Path_Info_Is_Not_Valid_Then_Minus_One_Is_Returned() {
77         when(request.getPathInfo()).thenReturn("/abc");
78         assertThat(baseServlet.getIdFromPath(request), is(-1));
79         when(request.getPathInfo()).thenReturn("/");
80         assertThat(baseServlet.getIdFromPath(request), is(-1));
81     }
82
83     @Test
84     public void Given_Request_Path_Info_Is_Not_Valid_Then_Minus_One_Is() throws Exception {
85         when(request.isSecure()).thenReturn(true);
86         Set<String> authAddressesAndNetworks = new HashSet<String>();
87         authAddressesAndNetworks.add(("127.0.0.1"));
88         FieldUtils
89             .writeDeclaredStaticField(BaseServlet.class, "authorizedAddressesAndNetworks", authAddressesAndNetworks,
90                 true);
91         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "requireCert", false, true);
92         assertThat(baseServlet.isAuthorizedForProvisioning(request), is(nullValue()));
93     }
94 }