diff options
Diffstat (limited to 'main/src/test/java')
5 files changed, 246 insertions, 17 deletions
diff --git a/main/src/test/java/org/onap/policy/distribution/main/parameters/CommonTestData.java b/main/src/test/java/org/onap/policy/distribution/main/parameters/CommonTestData.java index 0903b070..59f551cf 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/parameters/CommonTestData.java +++ b/main/src/test/java/org/onap/policy/distribution/main/parameters/CommonTestData.java @@ -38,6 +38,10 @@ import org.onap.policy.distribution.reception.parameters.ReceptionHandlerParamet */ public class CommonTestData { + private static final String REST_SERVER_PASSWORD = "zb!XztG34"; + private static final String REST_SERVER_USER = "healthcheck"; + private static final int REST_SERVER_PORT = 6969; + private static final String REST_SERVER_HOST = "0.0.0.0"; public static final String DISTRIBUTION_GROUP_NAME = "SDCDistributionGroup"; public static final String DECODER_TYPE = "TOSCA"; public static final String DECODER_CLASS_NAME = @@ -56,6 +60,23 @@ public class CommonTestData { * Returns an instance of ReceptionHandlerParameters for test cases. * * @param isEmpty boolean value to represent that object created should be empty or not + * @return the restServerParameters object + */ + public RestServerParameters getRestServerParameters(final boolean isEmpty) { + final RestServerParameters restServerParameters; + if (!isEmpty) { + restServerParameters = new RestServerParameters(REST_SERVER_HOST, REST_SERVER_PORT, REST_SERVER_USER, + REST_SERVER_PASSWORD); + } else { + restServerParameters = new RestServerParameters(null, 0, null, null); + } + return restServerParameters; + } + + /** + * Returns an instance of ReceptionHandlerParameters for test cases. + * + * @param isEmpty boolean value to represent that object created should be empty or not * @return the receptionHandlerParameters object */ public Map<String, ReceptionHandlerParameters> getReceptionHandlerParameters(final boolean isEmpty) { @@ -66,9 +87,8 @@ public class CommonTestData { final Map<String, PolicyForwarderParameters> policyForwarders = getPolicyForwarders(isEmpty); final PSSDConfigurationParametersGroup pssdConfiguration = getPSSDConfigurationParametersGroup(isEmpty);; final PluginHandlerParameters pHParameters = new PluginHandlerParameters(policyDecoders, policyForwarders); - final ReceptionHandlerParameters rhParameters = - new ReceptionHandlerParameters(RECEPTION_HANDLER_TYPE, RECEPTION_HANDLER_CLASS_NAME, - pssdConfiguration, pHParameters); + final ReceptionHandlerParameters rhParameters = new ReceptionHandlerParameters(RECEPTION_HANDLER_TYPE, + RECEPTION_HANDLER_CLASS_NAME, pssdConfiguration, pHParameters); receptionHandlerParameters.put(SDC_RECEPTION_HANDLER_KEY, rhParameters); } return receptionHandlerParameters; @@ -88,12 +108,11 @@ public class CommonTestData { final List<String> artifactTypes = new ArrayList<>(); artifactTypes.add("TOSCA_CSAR"); pssdConfiguration = new PSSDConfigurationParametersGroup.PSSDConfigurationBuilder() - .setAsdcAddress("localhost").setMessageBusAddress(messageBusAddress) - .setUser("policy").setPassword("policy").setPollingInterval(20) - .setPollingTimeout(30).setConsumerId("policy-id").setArtifactTypes(artifactTypes) - .setConsumerGroup("policy-group").setEnvironmentName("TEST").setKeystorePath("") - .setKeystorePassword("").setActiveserverTlsAuth(false) - .setIsFilterinEmptyResources(true).setIsUseHttpsWithDmaap(false).build(); + .setAsdcAddress("localhost").setMessageBusAddress(messageBusAddress).setUser("policy") + .setPassword("policy").setPollingInterval(20).setPollingTimeout(30).setConsumerId("policy-id") + .setArtifactTypes(artifactTypes).setConsumerGroup("policy-group").setEnvironmentName("TEST") + .setKeystorePath("").setKeystorePassword("").setActiveserverTlsAuth(false) + .setIsFilterinEmptyResources(true).setIsUseHttpsWithDmaap(false).build(); } else { pssdConfiguration = new PSSDConfigurationParametersGroup.PSSDConfigurationBuilder().build(); } diff --git a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterGroup.java b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterGroup.java index 70317971..f6ed0f98 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterGroup.java +++ b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterGroup.java @@ -41,13 +41,20 @@ public class TestDistributionParameterGroup { @Test public void testDistributionParameterGroup() { + final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false); final Map<String, ReceptionHandlerParameters> receptionHandlerParameters = commonTestData.getReceptionHandlerParameters(false); - final DistributionParameterGroup distributionParameters = - new DistributionParameterGroup(CommonTestData.DISTRIBUTION_GROUP_NAME, receptionHandlerParameters); + final DistributionParameterGroup distributionParameters = new DistributionParameterGroup( + CommonTestData.DISTRIBUTION_GROUP_NAME, restServerParameters, receptionHandlerParameters); final GroupValidationResult validationResult = distributionParameters.validate(); assertTrue(validationResult.isValid()); + assertEquals(restServerParameters.getHost(), distributionParameters.getRestServerParameters().getHost()); + assertEquals(restServerParameters.getPort(), distributionParameters.getRestServerParameters().getPort()); + assertEquals(restServerParameters.getUserName(), + distributionParameters.getRestServerParameters().getUserName()); + assertEquals(restServerParameters.getPassword(), + distributionParameters.getRestServerParameters().getPassword()); assertEquals(CommonTestData.DISTRIBUTION_GROUP_NAME, distributionParameters.getName()); assertEquals(receptionHandlerParameters.get(CommonTestData.SDC_RECEPTION_HANDLER_KEY).getReceptionHandlerType(), distributionParameters.getReceptionHandlerParameters().get(CommonTestData.SDC_RECEPTION_HANDLER_KEY) @@ -64,11 +71,12 @@ public class TestDistributionParameterGroup { @Test public void testDistributionParameterGroup_NullName() { + final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false); final Map<String, ReceptionHandlerParameters> receptionHandlerParameters = commonTestData.getReceptionHandlerParameters(false); final DistributionParameterGroup distributionParameters = - new DistributionParameterGroup(null, receptionHandlerParameters); + new DistributionParameterGroup(null, restServerParameters, receptionHandlerParameters); final GroupValidationResult validationResult = distributionParameters.validate(); assertFalse(validationResult.isValid()); assertEquals(null, distributionParameters.getName()); @@ -89,11 +97,12 @@ public class TestDistributionParameterGroup { @Test public void testDistributionParameterGroup_EmptyName() { + final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false); final Map<String, ReceptionHandlerParameters> receptionHandlerParameters = commonTestData.getReceptionHandlerParameters(false); final DistributionParameterGroup distributionParameters = - new DistributionParameterGroup("", receptionHandlerParameters); + new DistributionParameterGroup("", restServerParameters, receptionHandlerParameters); final GroupValidationResult validationResult = distributionParameters.validate(); assertFalse(validationResult.isValid()); assertEquals("", distributionParameters.getName()); @@ -114,9 +123,10 @@ public class TestDistributionParameterGroup { @Test public void testDistributionParameterGroup_NullReceptionHandlerParameters() { + final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false); try { final DistributionParameterGroup distributionParameters = - new DistributionParameterGroup(CommonTestData.DISTRIBUTION_GROUP_NAME, null); + new DistributionParameterGroup(CommonTestData.DISTRIBUTION_GROUP_NAME, restServerParameters, null); distributionParameters.validate(); fail("test should throw an exception here"); } catch (final Exception e) { @@ -127,11 +137,12 @@ public class TestDistributionParameterGroup { @Test public void testDistributionParameterGroup_EmptyReceptionHandlerParameters() { + final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false); final Map<String, ReceptionHandlerParameters> receptionHandlerParameters = commonTestData.getReceptionHandlerParameters(true); try { - final DistributionParameterGroup distributionParameters = - new DistributionParameterGroup(CommonTestData.DISTRIBUTION_GROUP_NAME, receptionHandlerParameters); + final DistributionParameterGroup distributionParameters = new DistributionParameterGroup( + CommonTestData.DISTRIBUTION_GROUP_NAME, restServerParameters, receptionHandlerParameters); distributionParameters.validate(); fail("test should throw an exception here"); } catch (final Exception e) { @@ -139,4 +150,19 @@ public class TestDistributionParameterGroup { } } + + @Test + public void testDistributionParameterGroup_EmptyRestServerParameters() { + final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(true); + final Map<String, ReceptionHandlerParameters> receptionHandlerParameters = + commonTestData.getReceptionHandlerParameters(false); + + final DistributionParameterGroup distributionParameters = new DistributionParameterGroup( + CommonTestData.DISTRIBUTION_GROUP_NAME, restServerParameters, receptionHandlerParameters); + final GroupValidationResult validationResult = distributionParameters.validate(); + assertFalse(validationResult.isValid()); + assertTrue(validationResult.getResult() + .contains("\"org.onap.policy.distribution.main.parameters.RestServerParameters\" INVALID, " + + "parameter group has status INVALID")); + } } diff --git a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterHandler.java b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterHandler.java index 79b12243..69ff0f14 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterHandler.java +++ b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterHandler.java @@ -357,4 +357,72 @@ public class TestDistributionParameterHandler { assertTrue(e.getMessage().contains("policy forwarder class not found in classpath")); } } + + @Test + public void testDistributionParameterGroup_InvalidRestServerHost() throws PolicyDistributionException { + final String[] distributionConfigParameters = + { "-c", "parameters/DistributionConfigParameters_InvalidRestServerHost.json" }; + + final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); + arguments.parse(distributionConfigParameters); + + try { + new DistributionParameterHandler().getParameters(arguments); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage().contains( + "must be a non-blank string containing hostname/ipaddress of the distribution rest server")); + } + } + + @Test + public void testDistributionParameterGroup_InvalidRestServerPort() throws PolicyDistributionException { + final String[] distributionConfigParameters = + { "-c", "parameters/DistributionConfigParameters_InvalidRestServerPort.json" }; + + final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); + arguments.parse(distributionConfigParameters); + + try { + new DistributionParameterHandler().getParameters(arguments); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage() + .contains("must be a positive integer containing port of the distribution rest server")); + } + } + + @Test + public void testDistributionParameterGroup_InvalidRestServerUser() throws PolicyDistributionException { + final String[] distributionConfigParameters = + { "-c", "parameters/DistributionConfigParameters_InvalidRestServerUser.json" }; + + final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); + arguments.parse(distributionConfigParameters); + + try { + new DistributionParameterHandler().getParameters(arguments); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage().contains( + "must be a non-blank string containing userName for distribution rest server credentials")); + } + } + + @Test + public void testDistributionParameterGroup_InvalidRestServerPassword() throws PolicyDistributionException { + final String[] distributionConfigParameters = + { "-c", "parameters/DistributionConfigParameters_InvalidRestServerPassword.json" }; + + final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); + arguments.parse(distributionConfigParameters); + + try { + new DistributionParameterHandler().getParameters(arguments); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertTrue(e.getMessage().contains( + "must be a non-blank string containing password for distribution rest server credentials")); + } + } } diff --git a/main/src/test/java/org/onap/policy/distribution/main/rest/TestDistributionRestServer.java b/main/src/test/java/org/onap/policy/distribution/main/rest/TestDistributionRestServer.java new file mode 100644 index 00000000..df585574 --- /dev/null +++ b/main/src/test/java/org/onap/policy/distribution/main/rest/TestDistributionRestServer.java @@ -0,0 +1,116 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.distribution.main.rest; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MediaType; + +import org.glassfish.jersey.client.ClientConfig; +import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; +import org.junit.Test; +import org.onap.policy.common.endpoints.report.HealthCheckReport; +import org.onap.policy.distribution.main.PolicyDistributionException; +import org.onap.policy.distribution.main.parameters.CommonTestData; +import org.onap.policy.distribution.main.parameters.RestServerParameters; +import org.onap.policy.distribution.main.startstop.Main; + +/** + * Class to perform unit test of HealthCheckMonitor. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class TestDistributionRestServer { + + private static final String NOT_ALIVE = "not alive"; + private static final String ALIVE = "alive"; + private static final String SELF = "self"; + private static final String NAME = "Policy SSD"; + + @Test + public void testHealthCheckSuccess() throws PolicyDistributionException, InterruptedException { + final String reportString = "Report [name=Policy SSD, url=self, healthy=true, code=200, message=alive]"; + final Main main = startDistributionService(); + final HealthCheckReport report = performHealthCheck(); + validateReport(NAME, SELF, true, 200, ALIVE, reportString, report); + stopDistributionService(main); + } + + @Test + public void testHealthCheckFailure() throws InterruptedException { + final String reportString = "Report [name=Policy SSD, url=self, healthy=false, code=500, message=not alive]"; + final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false); + restServerParams.setName(CommonTestData.DISTRIBUTION_GROUP_NAME); + final DistributionRestServer restServer = new DistributionRestServer(restServerParams); + restServer.start(); + final HealthCheckReport report = performHealthCheck(); + validateReport(NAME, SELF, false, 500, NOT_ALIVE, reportString, report); + assertTrue(restServer.isAlive()); + assertTrue(restServer.toString().startsWith("DistributionRestServer [servers=")); + restServer.shutdown(); + } + + private Main startDistributionService() { + final String[] distributionConfigParameters = { "-c", "parameters/DistributionConfigParameters.json" }; + return new Main(distributionConfigParameters); + } + + private void stopDistributionService(final Main main) throws PolicyDistributionException { + main.shutdown(); + } + + private HealthCheckReport performHealthCheck() throws InterruptedException { + HealthCheckReport response; + final ClientConfig clientConfig = new ClientConfig(); + + final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34"); + clientConfig.register(feature); + + final Client client = ClientBuilder.newClient(clientConfig); + final WebTarget webTarget = client.target("http://localhost:6969/healthcheck"); + + final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); + try { + response = invocationBuilder.get(HealthCheckReport.class); + } catch (final Exception exp) { + // may be the server is not started yet. Wait for couple of seconds and retry. + Thread.sleep(2000); + response = invocationBuilder.get(HealthCheckReport.class); + } + + return response; + } + + private void validateReport(final String name, final String url, final boolean healthy, final int code, + final String message, final String reportString, final HealthCheckReport report) { + assertEquals(name, report.getName()); + assertEquals(url, report.getUrl()); + assertEquals(healthy, report.isHealthy()); + assertEquals(code, report.getCode()); + assertEquals(message, report.getMessage()); + assertEquals(reportString, report.toString()); + } +} diff --git a/main/src/test/java/org/onap/policy/distribution/main/startstop/TestDistributionActivator.java b/main/src/test/java/org/onap/policy/distribution/main/startstop/TestDistributionActivator.java index 6d1f83bf..25133cfe 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/startstop/TestDistributionActivator.java +++ b/main/src/test/java/org/onap/policy/distribution/main/startstop/TestDistributionActivator.java @@ -60,6 +60,6 @@ public class TestDistributionActivator { activator.getParameterGroup().getReceptionHandlerParameters() .get(CommonTestData.SDC_RECEPTION_HANDLER_KEY).getPluginHandlerParameters() .getPolicyForwarders().get(CommonTestData.PAP_ENGINE_FORWARDER_KEY).getForwarderType()); - activator.deregisterToParameterService(parGroup); + activator.terminate(); } } |