Add test base parent class
[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.mockito.runners.MockitoJUnitRunner;
32
33 import javax.servlet.http.HttpServletRequest;
34 import java.util.HashSet;
35 import java.util.Set;
36
37 import static org.hamcrest.Matchers.is;
38 import static org.hamcrest.Matchers.nullValue;
39 import static org.junit.Assert.assertThat;
40 import static org.mockito.Mockito.when;
41
42 @RunWith(MockitoJUnitRunner.class)
43 public class BaseServletTest extends DrServletTestBase {
44
45     private BaseServlet baseServlet;
46
47     @Mock
48     private HttpServletRequest request;
49
50     @Before
51     public void setUp() throws Exception {
52         super.setUp();
53         baseServlet = new BaseServlet();
54     }
55
56
57     @Test
58     public void Given_Request_Path_Info_Is_Valid_Then_Id_Is_Extracted_Correctly() {
59         when(request.getPathInfo()).thenReturn("/123");
60         assertThat(baseServlet.getIdFromPath(request), is(123));
61     }
62
63     @Test
64     public void Given_Request_Path_Info_Is_Not_Valid_Then_Minus_One_Is_Returned() {
65         when(request.getPathInfo()).thenReturn("/abc");
66         assertThat(baseServlet.getIdFromPath(request), is(-1));
67         when(request.getPathInfo()).thenReturn("/");
68         assertThat(baseServlet.getIdFromPath(request), is(-1));
69     }
70
71     @Test
72     public void Given_Request_Path_Info_Is_Not_Valid_Then_Minus_One_Is() throws Exception {
73         when(request.isSecure()).thenReturn(true);
74         Set<String> authAddressesAndNetworks = new HashSet<String>();
75         authAddressesAndNetworks.add(("127.0.0.1"));
76         FieldUtils
77             .writeDeclaredStaticField(BaseServlet.class, "authorizedAddressesAndNetworks", authAddressesAndNetworks,
78                 true);
79         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "requireCert", false, true);
80         assertThat(baseServlet.isAuthorizedForProvisioning(request), is(nullValue()));
81     }
82 }