diff options
Diffstat (limited to 'catalog-fe/src/test/java')
16 files changed, 575 insertions, 323 deletions
diff --git a/catalog-fe/src/test/java/org/openecomp/sdc/fe/GzipFilterTest.java b/catalog-fe/src/test/java/org/openecomp/sdc/fe/GzipFilterTest.java index 95f48a1a61..b291cdac6f 100644 --- a/catalog-fe/src/test/java/org/openecomp/sdc/fe/GzipFilterTest.java +++ b/catalog-fe/src/test/java/org/openecomp/sdc/fe/GzipFilterTest.java @@ -20,9 +20,10 @@ package org.openecomp.sdc.fe; -import javax.servlet.FilterConfig; - import org.junit.Test; +import org.openecomp.sdc.fe.filters.GzipFilter; + +import javax.servlet.FilterConfig; public class GzipFilterTest { diff --git a/catalog-fe/src/test/java/org/openecomp/sdc/fe/filters/SecurityFilterTest.java b/catalog-fe/src/test/java/org/openecomp/sdc/fe/filters/SecurityFilterTest.java new file mode 100644 index 0000000000..d750e35243 --- /dev/null +++ b/catalog-fe/src/test/java/org/openecomp/sdc/fe/filters/SecurityFilterTest.java @@ -0,0 +1,125 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2020 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========================================================= + */ + +package org.openecomp.sdc.fe.filters; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.Spy; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.portalsdk.core.onboarding.util.PortalApiProperties; + +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.when; + + +@RunWith(MockitoJUnitRunner.class) +public class SecurityFilterTest { + + private static final String excludedUrls = "/config,/configmgr,/rest/healthCheck"; + + @Mock + private HttpServletRequest request; + @Mock + private FilterChain filterChain; + @Mock + private FilterConfig filterConfig; + @Spy + private HttpServletResponse response; + + @InjectMocks + private SecurityFilter securityFilter = new SecurityFilter(); + + @Before + public void setUpClass() throws ServletException{ + when(filterConfig.getInitParameter(SecurityFilter.FILTER_EXLUDED_URLS_KEY)).thenReturn(excludedUrls); + securityFilter.init(filterConfig); + } + + @Test + public void redirectPortalRequestAsCookieIsNotFound() throws ServletException, IOException { + when(request.getServletPath()).thenReturn("/portal"); + when(request.getCookies()).thenReturn(getCookies(false)); + securityFilter.doFilter(request, response, filterChain); + Mockito.verify(response, times(1)).sendRedirect(PortalApiProperties.getProperty(SecurityFilter.PORTAL_REDIRECT_URL_KEY)); + } + + @Test + public void redirectFeProxyRequestAsCookiesIsNull() throws ServletException, IOException { + when(request.getServletPath()).thenReturn("/feProxy"); + when(request.getCookies()).thenReturn(null); + securityFilter.doFilter(request, response, filterChain); + Mockito.verify(response, times(1)).sendRedirect(PortalApiProperties.getProperty(SecurityFilter.PORTAL_REDIRECT_URL_KEY)); + } + + @Test + public void requestIsNotRedirectedAsItIsFromPortal() throws ServletException, IOException { + when(request.getServletPath()).thenReturn("/feProxy"); + when(request.getCookies()).thenReturn(getCookies(true)); + securityFilter.doFilter(request, response, filterChain); + Mockito.verify(response, times(0)).sendRedirect(PortalApiProperties.getProperty(SecurityFilter.PORTAL_REDIRECT_URL_KEY)); + } + + @Test + public void requestIsNotRedirectedAsHcUrlIsExcluded() throws ServletException, IOException { + when(request.getServletPath()).thenReturn("/rest/healthCheck"); + securityFilter.doFilter(request, response, filterChain); + Mockito.verify(response, times(0)).sendRedirect(PortalApiProperties.getProperty(SecurityFilter.PORTAL_REDIRECT_URL_KEY)); + } + + + @Test + public void requestIsNotRedirectedAsConfigUrlIsExcluded() throws ServletException, IOException { + when(request.getServletPath()).thenReturn("/config"); + securityFilter.doFilter(request, response, filterChain); + Mockito.verify(response, times(0)).sendRedirect(PortalApiProperties.getProperty(SecurityFilter.PORTAL_REDIRECT_URL_KEY)); + } + + @Test + public void requestIsNotRedirectedForConfigMngrUrlIsExcluded() throws ServletException, IOException { + when(request.getServletPath()).thenReturn("/configmgr"); + securityFilter.doFilter(request, response, filterChain); + Mockito.verify(response, times(0)).sendRedirect(PortalApiProperties.getProperty(SecurityFilter.PORTAL_REDIRECT_URL_KEY)); + } + + + private Cookie[] getCookies(boolean isFromPortal) { + Cookie[] cookies = new Cookie [1]; + if (isFromPortal) { + cookies[0] = new Cookie(PortalApiProperties.getProperty(SecurityFilter.PORTAL_COOKIE_NAME_KEY), "aaa"); + } + else { + cookies[0] = new Cookie("someName", "aaa"); + } + return cookies; + } +} diff --git a/catalog-fe/src/test/java/org/openecomp/sdc/fe/impl/AuditTest.java b/catalog-fe/src/test/java/org/openecomp/sdc/fe/impl/AuditTest.java index 83bcaab871..c65d75bdab 100644 --- a/catalog-fe/src/test/java/org/openecomp/sdc/fe/impl/AuditTest.java +++ b/catalog-fe/src/test/java/org/openecomp/sdc/fe/impl/AuditTest.java @@ -20,14 +20,6 @@ package org.openecomp.sdc.fe.impl; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.util.HashMap; -import java.util.Map; -import javax.servlet.http.HttpServletRequest; - import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -35,6 +27,14 @@ import org.mockito.junit.MockitoJUnitRunner; import org.openecomp.sdc.common.api.Constants; import org.slf4j.Logger; +import javax.servlet.http.HttpServletRequest; +import java.util.HashMap; +import java.util.Map; + +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + @RunWith(MockitoJUnitRunner.class) public class AuditTest { diff --git a/catalog-fe/src/test/java/org/openecomp/sdc/fe/impl/HealthCheckScheduledTaskTest.java b/catalog-fe/src/test/java/org/openecomp/sdc/fe/impl/HealthCheckScheduledTaskTest.java new file mode 100644 index 0000000000..362d40cb29 --- /dev/null +++ b/catalog-fe/src/test/java/org/openecomp/sdc/fe/impl/HealthCheckScheduledTaskTest.java @@ -0,0 +1,185 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2020 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========================================================= + */ + +package org.openecomp.sdc.fe.impl; + +import com.google.common.collect.Lists; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.openecomp.sdc.common.api.Constants; +import org.openecomp.sdc.common.api.HealthCheckInfo; +import org.openecomp.sdc.fe.config.Configuration; + +import java.util.Collections; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class HealthCheckScheduledTaskTest { + + private static final String PROTOCOL = "http"; + private static final String HOST = "192.115.113.25"; + private static final Integer PORT = 8090; + private static final String URI = "/healthCheck"; + private static final String HC_URL = String.format("%s://%s:%s%s", PROTOCOL, HOST, PORT, URI); + + @Mock + private Configuration.CatalogFacadeMsConfig catalogFacadeMsConfig; + @Mock + private Configuration.DcaeConfig dcaeConfig; + @Mock + private Configuration.OnboardingConfig onboardingConfig; + @Mock + private Configuration configuration; + @Mock + private HealthCheckService healthCheckService; + + @InjectMocks + private HealthCheckScheduledTask healthCheckScheduledTask; + + @Before + public void setUp() { + healthCheckScheduledTask = new HealthCheckScheduledTask(healthCheckService); + initMocks(); + } + + @Test + public void getOnboardingUrlWhenConfigurationIsNotProvided() { + when(configuration.getOnboarding()).thenReturn(null); + assertNull(healthCheckScheduledTask.getExternalComponentHcUrl(Constants.HC_COMPONENT_ON_BOARDING)); + } + + @Test + public void getUrlForUnknownComponent() { + assertNull(healthCheckScheduledTask.getExternalComponentHcUrl("test")); + } + + @Test + public void getOnboardingUrlWhenConfigurationIsProvided() { + when(configuration.getOnboarding()).thenReturn(onboardingConfig); + assertNull(HealthCheckScheduledTask.getOnboardingHcUrl()); + healthCheckScheduledTask.getExternalComponentHcUrl(Constants.HC_COMPONENT_ON_BOARDING); + assertEquals(HC_URL, HealthCheckScheduledTask.getOnboardingHcUrl()); + } + + @Test + public void getCatalogFacadeMsUrlWhenConfigurationIsProvidedAndVerifyThatItIsCalculatedOnlyOnce() { + when(configuration.getCatalogFacadeMs()).thenReturn(catalogFacadeMsConfig); + assertNull(HealthCheckScheduledTask.getCatalogFacadeMsHcUrl()); + + HealthCheckScheduledTask healthCheckScheduledTaskSpy = Mockito.spy(healthCheckScheduledTask); + + healthCheckScheduledTaskSpy.getExternalComponentHcUrl(Constants.HC_COMPONENT_CATALOG_FACADE_MS); + assertEquals(HC_URL, HealthCheckScheduledTask.getCatalogFacadeMsHcUrl()); + //try to run again and verify that assignment is not recalled + healthCheckScheduledTaskSpy.getExternalComponentHcUrl(Constants.HC_COMPONENT_CATALOG_FACADE_MS); + verify(healthCheckScheduledTaskSpy, times(1)). + buildHealthCheckUrl(any(String.class), any(String.class), any(Integer.class), any(String.class)); + } + + @Test + public void getDcaeUrlWhenConfigurationIsProvided() { + when(configuration.getDcae()).thenReturn(dcaeConfig); + assertNull(HealthCheckScheduledTask.getDcaeHcUrl()); + healthCheckScheduledTask.getExternalComponentHcUrl(Constants.HC_COMPONENT_DCAE); + assertEquals(HC_URL, HealthCheckScheduledTask.getDcaeHcUrl()); + } + + @Test + public void getExcludedComponentListWhenCatalogFacadeMsConfigExists() { + when(configuration.getCatalogFacadeMs()).thenReturn(catalogFacadeMsConfig); + when(catalogFacadeMsConfig.getPath()).thenReturn("/uicache"); + when(configuration.getHealthStatusExclude()).thenReturn(Lists.newArrayList("DMAAP", "DCAE")); + assertFalse(healthCheckScheduledTask.getExcludedComponentList().contains(Constants.HC_COMPONENT_CATALOG_FACADE_MS)); + } + + @Test + public void getExcludedComponentListWhenCatalogFacadeMsConfigDoesNotExist() { + when(configuration.getCatalogFacadeMs()).thenReturn(null); + when(configuration.getHealthStatusExclude()).thenReturn(Lists.newArrayList()); + assertTrue(healthCheckScheduledTask.getExcludedComponentList().contains(Constants.HC_COMPONENT_CATALOG_FACADE_MS)); + } + + @Test + public void getExcludedComponentListWhenCatalogFacadeMsConfigPathIsNotSet() { + when(configuration.getCatalogFacadeMs()).thenReturn(catalogFacadeMsConfig); + when(catalogFacadeMsConfig.getPath()).thenReturn(null); + when(configuration.getHealthStatusExclude()).thenReturn(Lists.newArrayList()); + assertTrue(healthCheckScheduledTask.getExcludedComponentList().contains(Constants.HC_COMPONENT_CATALOG_FACADE_MS)); + } + + @Test + public void getMergedHCListWhenFeHcIsEmptyAndMainListIsSet() { + HealthCheckInfo mainHC = new HealthCheckInfo(); + mainHC.setComponentsInfo(Collections.emptyList()); + assertEquals(0, healthCheckScheduledTask.updateSubComponentsInfoOfBeHc(mainHC, Collections.emptyList()).getComponentsInfo().size()); + } + + @Test + public void getMergedHCListWhenFeHcIsEmptyAndMainListIsNotSet() { + assertNull(healthCheckScheduledTask.updateSubComponentsInfoOfBeHc(new HealthCheckInfo(), Collections.emptyList()).getComponentsInfo()); + } + + @Test + public void getMergedHCListWhenFeHcListAndMainListAreNotEmpty() { + HealthCheckInfo mainHC = new HealthCheckInfo(); + mainHC.setComponentsInfo(Lists.newArrayList(new HealthCheckInfo())); + assertEquals(2, healthCheckScheduledTask.updateSubComponentsInfoOfBeHc(mainHC, + Collections.singletonList(new HealthCheckInfo())).getComponentsInfo().size()); + } + + @Test + public void getMergedHCListWhenFeHcListIsNotEmptyAndMainListIsEmpty() { + assertEquals(1, healthCheckScheduledTask.updateSubComponentsInfoOfBeHc(new HealthCheckInfo(), + Collections.singletonList(new HealthCheckInfo())).getComponentsInfo().size()); + } + + + private void initMocks() { + when(healthCheckService.getConfig()).thenReturn(configuration); + + when(onboardingConfig.getProtocolFe()).thenReturn(PROTOCOL); + when(onboardingConfig.getHostFe()).thenReturn(HOST); + when(onboardingConfig.getPortFe()).thenReturn(PORT); + when(onboardingConfig.getHealthCheckUriFe()).thenReturn(URI); + + when(dcaeConfig.getProtocol()).thenReturn(PROTOCOL); + when(dcaeConfig.getHost()).thenReturn(HOST); + when(dcaeConfig.getPort()).thenReturn(PORT); + when(dcaeConfig.getHealthCheckUri()).thenReturn(URI); + + when(catalogFacadeMsConfig.getProtocol()).thenReturn(PROTOCOL); + when(catalogFacadeMsConfig.getHost()).thenReturn(HOST); + when(catalogFacadeMsConfig.getPort()).thenReturn(PORT); + when(catalogFacadeMsConfig.getHealthCheckUri()).thenReturn(URI); + } +} diff --git a/catalog-fe/src/test/java/org/openecomp/sdc/fe/impl/HttpRequestInfoTest.java b/catalog-fe/src/test/java/org/openecomp/sdc/fe/impl/HttpRequestInfoTest.java index bfc45528c4..156e1624c4 100644 --- a/catalog-fe/src/test/java/org/openecomp/sdc/fe/impl/HttpRequestInfoTest.java +++ b/catalog-fe/src/test/java/org/openecomp/sdc/fe/impl/HttpRequestInfoTest.java @@ -22,12 +22,6 @@ package org.openecomp.sdc.fe.impl; -import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSetters; - -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; -import javax.servlet.http.HttpServletRequest; import org.apache.commons.io.IOUtils; import org.junit.Assert; import org.junit.Test; @@ -36,6 +30,13 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSetters; + @RunWith(MockitoJUnitRunner.class) public class HttpRequestInfoTest { diff --git a/catalog-fe/src/test/java/org/openecomp/sdc/fe/impl/ImportMetadataTest.java b/catalog-fe/src/test/java/org/openecomp/sdc/fe/impl/ImportMetadataTest.java index 2fd5b56b18..cb1beda053 100644 --- a/catalog-fe/src/test/java/org/openecomp/sdc/fe/impl/ImportMetadataTest.java +++ b/catalog-fe/src/test/java/org/openecomp/sdc/fe/impl/ImportMetadataTest.java @@ -21,12 +21,12 @@ */ package org.openecomp.sdc.fe.impl; +import org.junit.Test; + import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSetters; import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.Assert.assertThat; -import org.junit.Test; - public class ImportMetadataTest { private static final String CHECKSUM = "CHECKSUM"; diff --git a/catalog-fe/src/test/java/org/openecomp/sdc/fe/impl/MdcDataTest.java b/catalog-fe/src/test/java/org/openecomp/sdc/fe/impl/MdcDataTest.java index d07470668e..a967f46a8e 100644 --- a/catalog-fe/src/test/java/org/openecomp/sdc/fe/impl/MdcDataTest.java +++ b/catalog-fe/src/test/java/org/openecomp/sdc/fe/impl/MdcDataTest.java @@ -21,10 +21,10 @@ */ package org.openecomp.sdc.fe.impl; -import static org.junit.Assert.assertEquals; - import org.junit.Test; +import static org.junit.Assert.assertEquals; + public class MdcDataTest { private static final String INSTANCE_ID = "INSTANCE_ID"; diff --git a/catalog-fe/src/test/java/org/openecomp/sdc/fe/listen/MyObjectMapperProviderTest.java b/catalog-fe/src/test/java/org/openecomp/sdc/fe/listen/MyObjectMapperProviderTest.java index 4a390d3e0c..63e978d807 100644 --- a/catalog-fe/src/test/java/org/openecomp/sdc/fe/listen/MyObjectMapperProviderTest.java +++ b/catalog-fe/src/test/java/org/openecomp/sdc/fe/listen/MyObjectMapperProviderTest.java @@ -23,11 +23,11 @@ package org.openecomp.sdc.fe.listen; import com.fasterxml.jackson.core.JsonProcessingException; -import java.io.Serializable; +import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.Assert; import org.junit.Test; -import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.Serializable; public class MyObjectMapperProviderTest { @@ -50,7 +50,7 @@ public class MyObjectMapperProviderTest { + "}"; ObjectMapper objectMapper = new MyObjectMapperProvider().getContext(MyObjectMapperProviderTest.class); - String serialized = objectMapper.writeValueAsString(new AnyModel("Field1")); + String serialized = objectMapper.writeValueAsString(new AnyModel("Field1")).replace("\r",""); Assert.assertEquals(serialized, prettyJson); } } diff --git a/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/ConfigMgrServletTest.java b/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/ConfigMgrServletTest.java index 5406863fc0..1030b9315e 100644 --- a/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/ConfigMgrServletTest.java +++ b/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/ConfigMgrServletTest.java @@ -19,9 +19,6 @@ */ package org.openecomp.sdc.fe.servlets; -import javax.servlet.ServletContext; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpSession; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -34,6 +31,10 @@ import org.openecomp.sdc.common.rest.api.RestConfigurationInfo; import org.openecomp.sdc.fe.config.Configuration; import org.openecomp.sdc.fe.config.ConfigurationManager; +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; + @RunWith(MockitoJUnitRunner.class) public class ConfigMgrServletTest { diff --git a/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/ConfigServletTest.java b/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/ConfigServletTest.java index 29a49bc611..1bc3a06e9a 100644 --- a/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/ConfigServletTest.java +++ b/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/ConfigServletTest.java @@ -39,9 +39,7 @@ import javax.servlet.http.HttpSession; import javax.ws.rs.core.Response; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.when; import static org.mockito.MockitoAnnotations.initMocks; @@ -104,19 +102,7 @@ public class ConfigServletTest { assertEquals(response.getStatus(), HttpStatus.SC_INTERNAL_SERVER_ERROR); } - @Test - public void validateGetPluginOnlineStateReturnsCorrectState() { - - final String testPluginName = "testPlugin"; - final String pluginAvailability = "forTesting"; - prepareMocks(); - when(pluginStatusBL.getPluginAvailability(eq(testPluginName))).thenReturn(pluginAvailability); - Response response = configServlet.getPluginOnlineState(testPluginName,httpServletRequest); - - assertEquals(response.getEntity().toString(),pluginAvailability); - assertEquals(response.getStatus(), HttpStatus.SC_OK); - } @Test public void validateGetPluginOnlineStateResponsesWithServerErrorIfExceptionIsThrown() { @@ -128,18 +114,7 @@ public class ConfigServletTest { assertEquals(response.getStatus(), HttpStatus.SC_INTERNAL_SERVER_ERROR); } - @Test - public void validateGetPluginOnlineStateResponsesWithNotFoundIfThereIsNoPlugin() { - - final String testPluginName = "testPlugin"; - prepareMocks(); - when(pluginStatusBL.getPluginAvailability(any(String.class))).thenReturn(null); - Response response = configServlet.getPluginOnlineState(testPluginName, httpServletRequest); - - assertEquals(response.getStatus(), HttpStatus.SC_NOT_FOUND); - assertTrue(response.getEntity().toString().contains(testPluginName)); - } private void prepareMocks() { when(httpServletRequest.getSession()).thenReturn(httpSession); diff --git a/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/FeHealthCheckServletTest.java b/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/FeHealthCheckServletTest.java index 78fe42ff8c..616e658d3e 100644 --- a/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/FeHealthCheckServletTest.java +++ b/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/FeHealthCheckServletTest.java @@ -20,20 +20,21 @@ package org.openecomp.sdc.fe.servlets; -import static org.junit.Assert.assertEquals; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.when; -import static org.openecomp.sdc.common.api.Constants.HEALTH_CHECK_SERVICE_ATTR; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.openecomp.sdc.fe.impl.HealthCheckService; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import javax.ws.rs.core.Response; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; +import static org.openecomp.sdc.common.api.Constants.HEALTH_CHECK_SERVICE_ATTR; @RunWith(MockitoJUnitRunner.class) public class FeHealthCheckServletTest { diff --git a/catalog-fe/src/test/java/org/openecomp/sdc/servlets/FeProxyServletTest.java b/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/FeProxyServletTest.java index 4915936b1d..36f218a391 100644 --- a/catalog-fe/src/test/java/org/openecomp/sdc/servlets/FeProxyServletTest.java +++ b/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/FeProxyServletTest.java @@ -18,10 +18,11 @@ * ============LICENSE_END========================================================= */ -package org.openecomp.sdc.servlets; +package org.openecomp.sdc.fe.servlets; import org.eclipse.jetty.client.api.Request; import org.eclipse.jetty.http.HttpFields; +import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.mockito.Mockito; @@ -29,16 +30,17 @@ import org.openecomp.sdc.common.api.Constants; import org.openecomp.sdc.fe.config.Configuration; import org.openecomp.sdc.fe.config.ConfigurationManager; import org.openecomp.sdc.fe.config.PluginsConfiguration; -import org.openecomp.sdc.fe.servlets.FeProxyServlet; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; +import java.net.MalformedURLException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.when; @@ -57,6 +59,7 @@ public class FeProxyServletTest { final static ConfigurationManager configurationManager = Mockito.mock(ConfigurationManager.class); final static Configuration configuration = Mockito.mock(Configuration.class); final static Configuration.OnboardingConfig onboardingConfiguration = Mockito.mock(Configuration.OnboardingConfig.class); + private final static Configuration.CatalogFacadeMsConfig catalogFacadeMsConfig = Mockito.mock(Configuration.CatalogFacadeMsConfig.class); final static Request proxyRequest = Mockito.spy(Request.class); final static HttpFields httpFields = Mockito.mock(HttpFields.class); private static final PluginsConfiguration pluginsConfiguration = Mockito.mock(PluginsConfiguration.class); @@ -78,6 +81,12 @@ public class FeProxyServletTest { final static String HEADER_2_VAL = "Header2_Val"; final static String HEADER_3_VAL = "Header3_Val"; final static String REQUEST_ID_VAL = "4867495a-5ed7-49e4-8be2-cc8d66fdd52b"; + private final static String msProtocol = "http"; + private final static String msHealth = "/healthCheck"; + private final static String msHost = "localhost"; + private final static Integer msPort = 8080; + private final static String msPath = "/uicache"; + private final static String msUrl = String.format("%s://%s:%s", msProtocol, msHost, msPort); @BeforeClass public static void beforeClass() { @@ -93,7 +102,7 @@ public class FeProxyServletTest { when(configuration.getOnboarding().getHostBe()).thenReturn(ONBOARDING_BE_HOST); when(configuration.getOnboarding().getPortBe()).thenReturn(ONBOARDING_BE_PORT); - List<String> strList = new ArrayList<String>(); + List<String> strList = new ArrayList<>(); strList.add(HEADER_1); strList.add(HEADER_2); strList.add(HEADER_3); @@ -112,13 +121,19 @@ public class FeProxyServletTest { List<PluginsConfiguration.Plugin> pluginList = new ArrayList<PluginsConfiguration.Plugin>(); when(plugin.getPluginId()).thenReturn("WORKFLOW"); when(plugin.getPluginSourceUrl()).thenReturn(WF_PROTOCOL + "://" + WF_HOST + ":" + WF_PORT); - when(plugin.getPluginDiscoveryUrl()).thenReturn(WF_PROTOCOL + "://" + WF_HOST + ":" + WF_PORT + "/workflows"); + when(plugin.getPluginDiscoveryUrl()).thenReturn(WF_PROTOCOL + "://" + WF_HOST + ":" + WF_PORT); pluginList.add(plugin); when(configurationManager.getPluginsConfiguration()).thenReturn(pluginsConfiguration); when(pluginsConfiguration.getPluginsList()).thenReturn(pluginList); } + @Before + public void setUp() { + when(configuration.getCatalogFacadeMs()).thenReturn(catalogFacadeMsConfig); + when(servletRequest.getQueryString()).thenReturn(null); + when(catalogFacadeMsConfig.getPath()).thenReturn(null); + } @Test public void testRewriteURI_APIRequest() { when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/rest/dummyBeAPI"); @@ -180,11 +195,13 @@ public class FeProxyServletTest { assertTrue(rewriteURI.equals(expectedChangedUrl)); } + + @Test public void testRewriteURIWithWFAPIRequest() { when(servletRequest.getRequestURI()).thenReturn("/sdc1/feProxy/wf/workflows"); String requestResourceUrl = "http://localhost:8080/sdc1/feProxy/wf/workflows"; - String expectedChangedUrl = WF_PROTOCOL + "://" + WF_HOST + ":" + WF_PORT + "/workflows/wf/workflows"; + String expectedChangedUrl = WF_PROTOCOL + "://" + WF_HOST + ":" + WF_PORT + "/wf/workflows"; when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(requestResourceUrl)); when(servletRequest.getContextPath()).thenReturn("/sdc1"); @@ -195,14 +212,70 @@ public class FeProxyServletTest { assertEquals(expectedChangedUrl, rewriteURI); } - /** - * class for testing only exposes the protected method. - */ - public static class FeProxyServletForTest extends FeProxyServlet{ + @Test + public void testRedirectToMSWhenMsUrlExists() throws MalformedURLException { + final String urlParams = "x=1&y=2&z=3"; + final String url = "http//test.com:8080/uicache/v1/catalog"; + setUpConfigMocks(); + when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(url)); + when(servletRequest.getQueryString()).thenReturn(urlParams); + assertTrue(feProxy.isMsRequest(url + urlParams)); + assertEquals(msUrl + "/uicache/v1/catalog?" + urlParams, + feProxy.redirectMsRequestToMservice(servletRequest, configuration)); + } + + @Test + public void testRedirectToMSWhenMsUrlExistsWithoutParams() throws MalformedURLException { + final String uri = "/uicache/v1/home"; + final String url = String.format("http//test.com:8080%s", uri); + setUpConfigMocks(); + when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(url)); + when(servletRequest.getRequestURI()).thenReturn(uri); + assertTrue(feProxy.isMsRequest(url)); + assertEquals(msUrl + "/uicache/v1/home", feProxy.redirectMsRequestToMservice(servletRequest, configuration)); + } + @Test + public void testRedirectToBeOnToggleOff() throws MalformedURLException { + final String uri = "/uicache/v1/catalog"; + final String url = String.format("http//test.com:8080%s", uri); + when(catalogFacadeMsConfig.getPath()).thenReturn(null); + + when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(url)); + when(servletRequest.getRequestURI()).thenReturn(uri); + assertTrue(feProxy.isMsRequest(url)); + String expectedUrl = String.format("%s://%s:%s/rest/v1/screen?excludeTypes=VFCMT&excludeTypes=Configuration", + BE_PROTOCOL, BE_HOST, BE_PORT); + assertEquals(expectedUrl, feProxy.redirectMsRequestToMservice(servletRequest, configuration)); + } + @Test(expected = StringIndexOutOfBoundsException.class) + public void testRedirectToMSWhenMsUrlExistsButItIsNotCatalogRequest() throws MalformedURLException { + final String url = "http//test.com:8080/rest/v1/sc"; + final String urlParams = "x=1&y=2&z=3"; + setUpConfigMocks(); + when(servletRequest.getRequestURL()).thenReturn(new StringBuffer(url)); + when(servletRequest.getQueryString()).thenReturn(urlParams); + assertFalse(feProxy.isMsRequest(url)); + feProxy.redirectMsRequestToMservice(servletRequest, configuration); + } + private void setUpConfigMocks() { + when(catalogFacadeMsConfig.getPath()).thenReturn(msPath); + when(catalogFacadeMsConfig.getProtocol()).thenReturn(msProtocol); + when(catalogFacadeMsConfig.getHost()).thenReturn(msHost); + when(catalogFacadeMsConfig.getPort()).thenReturn(msPort); + when(catalogFacadeMsConfig.getHealthCheckUri()).thenReturn(msHealth); + } + + /* class for testing only exposes the protected method.*/ + public static class FeProxyServletForTest extends FeProxyServlet{ + private static final long serialVersionUID = 1L; @Override public String rewriteTarget(HttpServletRequest request) { return super.rewriteTarget(request); } + @Override + boolean isMsRequest(String currentUrl) { + return super.isMsRequest(currentUrl); + } } } diff --git a/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/HealthCheckServiceTest.java b/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/HealthCheckServiceTest.java deleted file mode 100644 index 31b3c90741..0000000000 --- a/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/HealthCheckServiceTest.java +++ /dev/null @@ -1,72 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2019 Samsung. 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========================================================= - */ - -package org.openecomp.sdc.fe.servlets; - -import static org.junit.Assert.assertEquals; - -import javax.servlet.ServletContext; -import javax.ws.rs.core.Response; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; - -@RunWith(MockitoJUnitRunner.class) -public class HealthCheckServiceTest { - - - @Mock - private ServletContext context; - - private final HealthCheckService healthCheckService = new HealthCheckService(context); - private final Response response = Response.status(500).entity("{}").build(); - - - @Test - public void testGetFeHealth() { - //given - Response feHealth = healthCheckService.getFeHealth(); - - //then - assertEquals(response.getEntity(), feHealth.getEntity()); - assertEquals(response.getStatus(), feHealth.getStatus()); - } - - @Test - public void testGetLastHealthStatus() { - //given - HealthCheckService.HealthStatus healthStatus = healthCheckService.getLastHealthStatus(); - - //then - assertEquals(response.getEntity(), healthStatus.getBody()); - assertEquals(response.getStatus(), healthStatus.getStatusCode()); - } - - @Test - public void testGetTask () { - //given - HealthCheckService.HealthCheckScheduledTask healthCheckScheduledTask = healthCheckService.getTask(); - HealthCheckService.HealthStatus healthStatus = healthCheckScheduledTask.checkHealth(); - - //then - assertEquals(response.getStatus(),healthStatus.getStatusCode()); - } -} diff --git a/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/KibanaServletTest.java b/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/KibanaServletTest.java deleted file mode 100644 index f946891aa9..0000000000 --- a/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/KibanaServletTest.java +++ /dev/null @@ -1,93 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2019 Samsung. 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========================================================= - */ - -package org.openecomp.sdc.fe.servlets; - -import static org.junit.Assert.assertEquals; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.when; - -import javax.servlet.ServletContext; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpSession; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; -import org.openecomp.sdc.common.api.Constants; -import org.openecomp.sdc.fe.config.Configuration; -import org.openecomp.sdc.fe.config.ConfigurationManager; - -@RunWith(MockitoJUnitRunner.class) -public class KibanaServletTest { - - private static final int KIBANA_PORT = 9898; - private static final String CONTEXT_PATH = "/context"; - private static final String SERVLET_PATH = "/sdc1/kibanaProxy"; - private static final String PATH_INFO = "/info"; - private static final String QUERY_STRING = "query=projectR"; - private static final String REQUEST_URI = "uri"; - private static final String KIBANA_PROTOCOL = "kbn"; - private static final String KIBANA_HOST = "kibana.com"; - private static final String EXPECTED = "kbn://kibana.com:9898/context/info?query=projectR"; - - private final KibanaServlet kibanaServlet = new KibanaServlet(); - - @Mock - private Configuration configuration; - - @Mock - private ConfigurationManager manager; - - @Mock - private ServletContext context; - - @Mock - private HttpSession session; - - @Mock - private HttpServletRequest request; - - @Test - public void testRewriteTarget() { - // given - when(manager.getConfiguration()).thenReturn(configuration); - when(context.getAttribute(eq(Constants.CONFIGURATION_MANAGER_ATTR))).thenReturn(manager); - when(session.getServletContext()).thenReturn(context); - when(request.getSession()).thenReturn(session); - - when(request.getContextPath()).thenReturn(CONTEXT_PATH); - when(request.getServletPath()).thenReturn(SERVLET_PATH); - when(request.getPathInfo()).thenReturn(PATH_INFO); - when(request.getQueryString()).thenReturn(QUERY_STRING); - when(request.getRequestURI()).thenReturn(REQUEST_URI); - - when(configuration.getKibanaProtocol()).thenReturn(KIBANA_PROTOCOL); - when(configuration.getKibanaHost()).thenReturn(KIBANA_HOST); - when(configuration.getKibanaPort()).thenReturn(KIBANA_PORT); - - // when - final String url = kibanaServlet.rewriteTarget(request); - - // then - assertEquals(EXPECTED, url); - } -} diff --git a/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/PluginStatusBLTest.java b/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/PluginStatusBLTest.java index 8bf4e478b4..f435e2d80e 100644 --- a/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/PluginStatusBLTest.java +++ b/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/PluginStatusBLTest.java @@ -41,90 +41,89 @@ import java.util.ArrayList; import java.util.List; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.when; public class PluginStatusBLTest { - final static CloseableHttpClient httpClient = Mockito.mock(CloseableHttpClient.class); - PluginStatusBL pluginStatusBL = new PluginStatusBL(httpClient); - private static Gson gson = new GsonBuilder().setPrettyPrinting().create(); - - final static ConfigurationManager configurationManager = Mockito.mock(ConfigurationManager.class); - final static PluginsConfiguration pluginsConfiguration = Mockito.mock(PluginsConfiguration.class); - final static Plugin offlinePlugin = new Plugin(); - final static Plugin onlinePlugin = new Plugin(); - final static CloseableHttpResponse httpResponse = Mockito.mock(CloseableHttpResponse.class); - final static StatusLine statusLine = Mockito.mock(StatusLine.class); - static List<Plugin> testPluginsList = new ArrayList<>(); - static List<Plugin> assertPluginList = new ArrayList<>(); - - final static String offlinePluginsDisplayName = "offlinePlugin"; - final static String offlinePluginDiscoveryPath = "http://192.168.10.1:1000/offline"; - - final static String onlinePluginDisplayName = "onlinePlugin"; - final static String onlinePluginDiscoveryPath = "http://192.168.10.1:2000/online"; - - @BeforeClass - public static void beforeClass() { - ConfigurationManager.setTestInstance(configurationManager); - when(configurationManager.getPluginsConfiguration()).thenReturn(pluginsConfiguration); - - offlinePlugin.setPluginId(offlinePluginsDisplayName); - offlinePlugin.setPluginDiscoveryUrl(offlinePluginDiscoveryPath); - - onlinePlugin.setPluginId(onlinePluginDisplayName); - onlinePlugin.setPluginDiscoveryUrl(onlinePluginDiscoveryPath); - } - - @Before - public void beforeTest() { - testPluginsList = new ArrayList<>(); - assertPluginList = new ArrayList<>(); - } - - @Test - public void TestPluginsConfigurationListReturnsWithWantedPlugins() { - testPluginsList.add(offlinePlugin); - testPluginsList.add(onlinePlugin); - when(pluginsConfiguration.getPluginsList()).thenReturn(testPluginsList); - - assertPluginList.add(offlinePlugin); - assertPluginList.add(onlinePlugin); - - String result = gson.toJson(assertPluginList); - String actualResult = pluginStatusBL.getPluginsList(); - - assertEquals(actualResult, result); - } - - @Test - public void TestGetPluginAvailabilityShouldReturnFalseWhenPluginIsOffline() throws ClientProtocolException, IOException { - testPluginsList.add(offlinePlugin); - when(pluginsConfiguration.getPluginsList()).thenReturn(testPluginsList); - - when(statusLine.getStatusCode()).thenReturn(404); - when(httpResponse.getStatusLine()).thenReturn(statusLine); - when(httpClient.execute(Mockito.any(HttpHead.class))).thenReturn(httpResponse); - - String result = gson.toJson(false); - String actualResult = pluginStatusBL.getPluginAvailability(offlinePlugin.getPluginId()); - - assertEquals(actualResult, result); - } - - @Test - public void TestOnlinePluginBeingReturnedWithIsOnlineValueTrue() throws ClientProtocolException, IOException { - testPluginsList.add(onlinePlugin); - when(pluginsConfiguration.getPluginsList()).thenReturn(testPluginsList); - - when(statusLine.getStatusCode()).thenReturn(200); - when(httpResponse.getStatusLine()).thenReturn(statusLine); - when(httpClient.execute(Mockito.any())).thenReturn(httpResponse); - - String result = gson.toJson(true); - String actualResult = pluginStatusBL.getPluginAvailability(onlinePlugin.getPluginId()); - - assertEquals(actualResult, result); - } + final static CloseableHttpClient httpClient = Mockito.mock(CloseableHttpClient.class); + PluginStatusBL pluginStatusBL = new PluginStatusBL(httpClient); + private static Gson gson = new GsonBuilder().setPrettyPrinting().create(); + + final static ConfigurationManager configurationManager = Mockito.mock(ConfigurationManager.class); + final static PluginsConfiguration pluginsConfiguration = Mockito.mock(PluginsConfiguration.class); + final static Plugin offlinePlugin = new Plugin(); + final static Plugin onlinePlugin = new Plugin(); + final static CloseableHttpResponse httpResponse = Mockito.mock(CloseableHttpResponse.class); + final static StatusLine statusLine = Mockito.mock(StatusLine.class); + static List<Plugin> testPluginsList = new ArrayList<>(); + static List<Plugin> assertPluginList = new ArrayList<>(); + + final static String offlinePluginsDisplayName = "offlinePlugin"; + final static String offlinePluginDiscoveryPath = "http://192.168.10.1:1000/offline"; + + final static String onlinePluginDisplayName = "onlinePlugin"; + final static String onlinePluginDiscoveryPath = "http://192.168.10.1:2000/online"; + + @BeforeClass + public static void beforeClass() { + ConfigurationManager.setTestInstance(configurationManager); + when(configurationManager.getPluginsConfiguration()).thenReturn(pluginsConfiguration); + + offlinePlugin.setPluginId(offlinePluginsDisplayName); + offlinePlugin.setPluginDiscoveryUrl(offlinePluginDiscoveryPath); + + onlinePlugin.setPluginId(onlinePluginDisplayName); + onlinePlugin.setPluginDiscoveryUrl(onlinePluginDiscoveryPath); + } + + @Before + public void beforeTest() { + testPluginsList = new ArrayList<>(); + assertPluginList = new ArrayList<>(); + } + + @Test + public void TestPluginsConfigurationListReturnsWithWantedPlugins() { + testPluginsList.add(offlinePlugin); + testPluginsList.add(onlinePlugin); + when(pluginsConfiguration.getPluginsList()).thenReturn(testPluginsList); + + assertPluginList.add(offlinePlugin); + assertPluginList.add(onlinePlugin); + + String result = gson.toJson(assertPluginList); + String actualResult = pluginStatusBL.getPluginsList(); + + assertEquals(actualResult, result); + } + + @Test + public void TestGetPluginAvailabilityShouldReturnFalseWhenPluginIsOffline() throws ClientProtocolException, IOException { + testPluginsList.add(offlinePlugin); + when(pluginsConfiguration.getPluginsList()).thenReturn(testPluginsList); + + when(statusLine.getStatusCode()).thenReturn(404); + when(httpResponse.getStatusLine()).thenReturn(statusLine); + when(httpClient.execute(Mockito.any(HttpHead.class))).thenReturn(httpResponse); + + String result = gson.toJson(false); + String actualResult = pluginStatusBL.getPluginAvailability(offlinePlugin.getPluginId()); + + assertEquals(actualResult, result); + } + + @Test + public void TestOnlinePluginBeingReturnedWithIsOnlineValueTrue() throws ClientProtocolException, IOException { + testPluginsList.add(onlinePlugin); + when(pluginsConfiguration.getPluginsList()).thenReturn(testPluginsList); + + when(statusLine.getStatusCode()).thenReturn(200); + when(httpResponse.getStatusLine()).thenReturn(statusLine); + when(httpClient.execute(Mockito.any())).thenReturn(httpResponse); + + String result = gson.toJson(true); + String actualResult = pluginStatusBL.getPluginAvailability(onlinePlugin.getPluginId()); + + assertEquals(actualResult, result); + } } diff --git a/catalog-fe/src/test/java/org/openecomp/sdc/servlets/PortalServletTest.java b/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/PortalServletTest.java index 72d279d3e5..fccb41bc99 100644 --- a/catalog-fe/src/test/java/org/openecomp/sdc/servlets/PortalServletTest.java +++ b/catalog-fe/src/test/java/org/openecomp/sdc/fe/servlets/PortalServletTest.java @@ -18,21 +18,27 @@ * ============LICENSE_END========================================================= */ -package org.openecomp.sdc.servlets; +package org.openecomp.sdc.fe.servlets; import org.glassfish.jersey.internal.inject.AbstractBinder; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.test.JerseyTest; +import org.junit.After; import org.junit.BeforeClass; import org.junit.Test; import org.mockito.Mockito; import org.mockito.stubbing.Answer; +import org.onap.sdc.security.CipherUtil; import org.openecomp.sdc.common.api.Constants; import org.openecomp.sdc.fe.config.Configuration; import org.openecomp.sdc.fe.config.ConfigurationManager; -import org.openecomp.sdc.fe.servlets.PortalServlet; -import javax.servlet.*; +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @@ -42,7 +48,12 @@ import java.util.ArrayList; import java.util.List; import static org.glassfish.jersey.test.TestProperties.CONTAINER_PORT; -import static org.mockito.Mockito.*; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + public class PortalServletTest extends JerseyTest { @@ -53,6 +64,8 @@ public class PortalServletTest extends JerseyTest { private final static Configuration configuration = Mockito.mock(Configuration.class); private final static HttpServletResponse response = Mockito.spy(HttpServletResponse.class); private final static RequestDispatcher rd = Mockito.spy(RequestDispatcher.class); + private static CipherUtil cipherUtil = Mockito.mock(CipherUtil.class); + final static Configuration.CookieConfig cookieConfiguration = Mockito.mock(Configuration.CookieConfig.class); @SuppressWarnings("serial") @BeforeClass @@ -62,6 +75,7 @@ public class PortalServletTest extends JerseyTest { when(httpSession.getServletContext()).thenReturn(servletContext); when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager); when(configurationManager.getConfiguration()).thenReturn(configuration); + when(configuration.getAuthCookie()).thenReturn(cookieConfiguration); List<List<String>> mandatoryHeaders = new ArrayList<>(); mandatoryHeaders.add(new ArrayList<String>() { { @@ -113,25 +127,58 @@ public class PortalServletTest extends JerseyTest { } + @After + public void tearDown() { + Mockito.reset(response, rd); + } + @Test public void testMissingHeadersRequest() throws IOException { when(request.getHeader(Mockito.anyString())).thenReturn(null); - target().path("/portal").request().get(); + when(request.getCookies()).thenReturn(getCookies()); + target().path("/portal").request().get(); Mockito.verify(response, times(1)).sendError(HttpServletResponse.SC_USE_PROXY, PortalServlet.MISSING_HEADERS_MSG); - Mockito.reset(response, rd); } @Test public void testSuccessfulRequest() throws IOException, ServletException { - Mockito.doAnswer((Answer<Object>) invocation -> { + ConfigurationManager.setTestInstance(configurationManager); + when(configuration.getAuthCookie().getCookieName()).thenReturn("cookieName"); + when(configuration.getAuthCookie().getPath()).thenReturn("/"); + when(configuration.getAuthCookie().getDomain()).thenReturn(""); + when(configuration.getAuthCookie().getSecurityKey()).thenReturn(""); + Mockito.doAnswer((Answer<Object>) invocation -> { Object[] args = invocation.getArguments(); return (String) args[0]; }).when(request).getHeader(Mockito.anyString()); target().path("/portal").request().get(); verify(rd).forward(Mockito.any(ServletRequest.class), Mockito.any(ServletResponse.class)); - Mockito.reset(response, rd); } + + @Test + public void testSuccessfullAddofAuthCookie() throws IOException, ServletException { + ConfigurationManager.setTestInstance(configurationManager); + when(configuration.getAuthCookie().getCookieName()).thenReturn("cookieName"); + when(configuration.getAuthCookie().getPath()).thenReturn("/"); + when(configuration.getAuthCookie().getDomain()).thenReturn(""); + when(configuration.getAuthCookie().getSecurityKey()).thenReturn("AGLDdG4D04BKm2IxIWEr8o=="); + PortalServlet pp = new PortalServlet(); + assertTrue(pp.addAuthCookie(response,"user", "test" ,"User")); + } + + @Test + public void testFailureMissingCookieConfiguration() throws IOException { + + //missing configuration mock therefore will fail + PortalServlet pp = new PortalServlet(); + pp.doGet(request,response); + assertFalse(pp.addAuthCookie(response,"user", "test" ,"User")); + + } + + + @Override protected Application configure() { // Use any available port - this allows us to run the BE tests in parallel with this one. @@ -146,6 +193,15 @@ public class PortalServletTest extends JerseyTest { } }); + + return resourceConfig; } + + private Cookie[] getCookies() { + Cookie[] cookies = new Cookie [1]; + cookies[0] = new Cookie("someName", "aaa"); + return cookies; + } + } |