X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=datarouter-prov%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fprovisioning%2FBaseServletTest.java;fp=datarouter-prov%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fprovisioning%2FBaseServletTest.java;h=5966b775a32a26022a95deff3b9569efc657e108;hb=dadcc32db07e9ba1cd0198bffab39b688fa13323;hp=0000000000000000000000000000000000000000;hpb=13639e1b05d8c8b5b1e9efd543573834501aefaa;p=dmaap%2Fdatarouter.git diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/BaseServletTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/BaseServletTest.java new file mode 100644 index 00000000..5966b775 --- /dev/null +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/BaseServletTest.java @@ -0,0 +1,83 @@ +/******************************************************************************* + * ============LICENSE_START================================================== + * * org.onap.dmaap + * * =========================================================================== + * * Copyright © 2017 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==================================================== + * * + * * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * * + ******************************************************************************/ + +package org.onap.dmaap.datarouter.provisioning; + +import org.apache.commons.lang3.reflect.FieldUtils; +import org.apache.log4j.Logger; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.HashSet; +import java.util.Set; + +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class BaseServletTest { + private BaseServlet baseServlet; + + @Mock + private HttpServletRequest request; + + @Before + public void setUp() throws Exception { + FieldUtils.writeDeclaredStaticField(BaseServlet.class, "startmsg_flag", false, true); + SynchronizerTask synchronizerTask = mock(SynchronizerTask.class); + FieldUtils.writeDeclaredStaticField(BaseServlet.class, "synctask", synchronizerTask, true); + baseServlet = new BaseServlet(); + } + + + @Test + public void Given_Request_Path_Info_Is_Valid_Then_Id_Is_Extracted_Correctly() throws Exception { + when(request.getPathInfo()).thenReturn("/123"); + assertThat(baseServlet.getIdFromPath(request), is(123)); + } + @Test + public void Given_Request_Path_Info_Is_Not_Valid_Then_Minus_One_Is_Returned() throws Exception { + when(request.getPathInfo()).thenReturn("/abc"); + assertThat(baseServlet.getIdFromPath(request), is(-1)); + when(request.getPathInfo()).thenReturn("/"); + assertThat(baseServlet.getIdFromPath(request), is(-1)); + } + + @Test + public void Given_Request_Path_Info_Is_Not_Valid_Then_Minus_One_Is() throws Exception { + when(request.isSecure()).thenReturn(true); + Set authAddressesAndNetworks = new HashSet(); + authAddressesAndNetworks.add(("127.0.0.1")); + FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authorizedAddressesAndNetworks", authAddressesAndNetworks, true); + FieldUtils.writeDeclaredStaticField(BaseServlet.class, "require_cert", false, true); + assertThat(baseServlet.isAuthorizedForProvisioning(request), is(nullValue())); + } +}