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.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.Mock;
31 import org.mockito.runners.MockitoJUnitRunner;
33 import javax.servlet.http.HttpServletRequest;
34 import java.util.HashSet;
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;
42 @RunWith(MockitoJUnitRunner.class)
43 public class BaseServletTest extends DrServletTestBase {
45 private BaseServlet baseServlet;
48 private HttpServletRequest request;
51 public void setUp() throws Exception {
53 baseServlet = new BaseServlet();
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));
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));
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"));
77 .writeDeclaredStaticField(BaseServlet.class, "authorizedAddressesAndNetworks", authAddressesAndNetworks,
79 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "requireCert", false, true);
80 assertThat(baseServlet.isAuthorizedForProvisioning(request), is(nullValue()));