diff options
author | Michael Mokry <michael.mokry@att.com> | 2019-01-31 13:16:55 -0600 |
---|---|---|
committer | Michael Mokry <michael.mokry@att.com> | 2019-02-11 15:49:48 -0600 |
commit | f239a66e5dd52f4f0149a307789909c5ffc2b704 (patch) | |
tree | 55fdf3342245fcf3feff3e5ef3617424d4468173 /main/src/test/java/org | |
parent | bf593eca637e1c3cfa4ece0e0c3d13bcf613b187 (diff) |
PDPX Healthcheck/Statistic RESTful API entry point
Includes:
- Basic code structure modeled after policy distribution
- Code implementation to support Healthcheck/Statistics RESTful API entry point
- JUnits
- Fixed Checkstyles issues and added some missing statistics classes and
Junits
- Made changes per Jim's comments
- Made more changes per Jim's comments
> made gson field static
> using AssertThatThrownBy() mechanic from AssertJ
> added setup and teardown to correctly terminate activator in
Junit
- Made corrections to the statitics endpoint and junits
Change-Id: Iad40272beceff8a0f99966440e96a84fc2043b12
Issue-ID: POLICY-1436
Signed-off-by: Michael Mokry <michael.mokry@att.com>
Diffstat (limited to 'main/src/test/java/org')
8 files changed, 755 insertions, 0 deletions
diff --git a/main/src/test/java/org/onap/policy/pdpx/main/parameters/CommonTestData.java b/main/src/test/java/org/onap/policy/pdpx/main/parameters/CommonTestData.java new file mode 100644 index 00000000..f50871ec --- /dev/null +++ b/main/src/test/java/org/onap/policy/pdpx/main/parameters/CommonTestData.java @@ -0,0 +1,52 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pdpx.main.parameters; + +/** + * Class to hold/create all parameters for test cases. + * + */ +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 PDPX_GROUP_NAME = "XacmlPdpGroup"; + + /** + * Returns an instance of RestServerParameters 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; + } + +} diff --git a/main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterGroup.java b/main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterGroup.java new file mode 100644 index 00000000..4b9db99d --- /dev/null +++ b/main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterGroup.java @@ -0,0 +1,87 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + + +package org.onap.policy.pdpx.main.parameters; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.onap.policy.common.parameters.GroupValidationResult; + +/** + * Class to perform unit test of XacmlPdpParameterGroup. + * + */ +public class TestXacmlPdpParameterGroup { + CommonTestData commonTestData = new CommonTestData(); + + @Test + public void testXacmlPdpParameterGroup() { + final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false); + final XacmlPdpParameterGroup pdpxParameters = + new XacmlPdpParameterGroup(CommonTestData.PDPX_GROUP_NAME, restServerParameters); + final GroupValidationResult validationResult = pdpxParameters.validate(); + assertTrue(validationResult.isValid()); + assertEquals(restServerParameters.getHost(), pdpxParameters.getRestServerParameters().getHost()); + assertEquals(restServerParameters.getPort(), pdpxParameters.getRestServerParameters().getPort()); + assertEquals(restServerParameters.getUserName(), pdpxParameters.getRestServerParameters().getUserName()); + assertEquals(restServerParameters.getPassword(), pdpxParameters.getRestServerParameters().getPassword()); + assertEquals(CommonTestData.PDPX_GROUP_NAME, pdpxParameters.getName()); + } + + @Test + public void testXacmlPdpParameterGroup_NullName() { + final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false); + final XacmlPdpParameterGroup pdpxParameters = new XacmlPdpParameterGroup(null, restServerParameters); + final GroupValidationResult validationResult = pdpxParameters.validate(); + assertFalse(validationResult.isValid()); + assertEquals(null, pdpxParameters.getName()); + assertTrue(validationResult.getResult().contains( + "field \"name\" type \"java.lang.String\" value \"null\" INVALID, " + "must be a non-blank string")); + } + + @Test + public void testXacmlPdpParameterGroup_EmptyName() { + final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false); + + final XacmlPdpParameterGroup pdpxParameters = new XacmlPdpParameterGroup("", restServerParameters); + final GroupValidationResult validationResult = pdpxParameters.validate(); + assertFalse(validationResult.isValid()); + assertEquals("", pdpxParameters.getName()); + assertTrue(validationResult.getResult().contains( + "field \"name\" type \"java.lang.String\" value \"\" INVALID, " + "must be a non-blank string")); + } + + @Test + public void testXacmlPdpParameterGroup_EmptyRestServerParameters() { + final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(true); + + final XacmlPdpParameterGroup pdpxParameters = + new XacmlPdpParameterGroup(CommonTestData.PDPX_GROUP_NAME, restServerParameters); + final GroupValidationResult validationResult = pdpxParameters.validate(); + assertFalse(validationResult.isValid()); + assertTrue(validationResult.getResult() + .contains("\"org.onap.policy.pdpx.main.parameters.RestServerParameters\" INVALID, " + + "parameter group has status INVALID")); + } +} diff --git a/main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterHandler.java b/main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterHandler.java new file mode 100644 index 00000000..3955031a --- /dev/null +++ b/main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterHandler.java @@ -0,0 +1,179 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pdpx.main.parameters; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import org.junit.Test; +import org.onap.policy.pdpx.main.PolicyXacmlPdpException; +import org.onap.policy.pdpx.main.startstop.XacmlPdpCommandLineArguments; + +/** + * Class to perform unit test of XacmlPdpParameterHandler. + * + */ +public class TestXacmlPdpParameterHandler { + @Test + public void testParameterHandlerNoParameterFile() throws PolicyXacmlPdpException { + final String[] noArgumentString = {"-c", "parameters/NoParameterFile.json"}; + + final XacmlPdpCommandLineArguments noArguments = new XacmlPdpCommandLineArguments(); + noArguments.parse(noArgumentString); + + assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(noArguments)) + .isInstanceOf(PolicyXacmlPdpException.class); + } + + @Test + public void testParameterHandlerEmptyParameters() throws PolicyXacmlPdpException { + final String[] emptyArgumentString = {"-c", "parameters/EmptyParameters.json"}; + + final XacmlPdpCommandLineArguments emptyArguments = new XacmlPdpCommandLineArguments(); + emptyArguments.parse(emptyArgumentString); + + assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(emptyArguments)) + .hasMessage("no parameters found in \"parameters/EmptyParameters.json\""); + } + + @Test + public void testParameterHandlerBadParameters() throws PolicyXacmlPdpException { + final String[] badArgumentString = {"-c", "parameters/BadParameters.json"}; + + final XacmlPdpCommandLineArguments badArguments = new XacmlPdpCommandLineArguments(); + badArguments.parse(badArgumentString); + + assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(badArguments)) + .hasMessage("error reading parameters from \"parameters/BadParameters.json\"\n" + + "(JsonSyntaxException):java.lang.IllegalStateException: " + + "Expected a string but was BEGIN_ARRAY at line 2 column 14 path $.name"); + + } + + @Test + public void testParameterHandlerInvalidParameters() throws PolicyXacmlPdpException { + final String[] invalidArgumentString = {"-c", "parameters/InvalidParameters.json"}; + + final XacmlPdpCommandLineArguments invalidArguments = new XacmlPdpCommandLineArguments(); + invalidArguments.parse(invalidArgumentString); + + assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(invalidArguments)) + .hasMessage("error reading parameters from \"parameters/InvalidParameters.json\"\n" + + "(JsonSyntaxException):java.lang.IllegalStateException: " + + "Expected a string but was BEGIN_ARRAY at line 2 column 14 path $.name"); + } + + @Test + public void testParameterHandlerNoParameters() throws PolicyXacmlPdpException { + final String[] noArgumentString = {"-c", "parameters/NoParameters.json"}; + + final XacmlPdpCommandLineArguments noArguments = new XacmlPdpCommandLineArguments(); + noArguments.parse(noArgumentString); + + assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(noArguments)) + .hasMessage("validation error(s) on parameters from \"parameters/NoParameters.json\"\nparameter group " + + "\"null\" type \"org.onap.policy.pdpx.main.parameters.XacmlPdpParameterGroup\" INVALID, " + + "parameter group has status INVALID\n" + + " field \"name\" type \"java.lang.String\" value \"null\" " + + "INVALID, must be a non-blank string\n"); + } + + @Test + public void testParameterHandlerMinumumParameters() throws PolicyXacmlPdpException { + final String[] minArgumentString = {"-c", "parameters/MinimumParameters.json"}; + + final XacmlPdpCommandLineArguments minArguments = new XacmlPdpCommandLineArguments(); + minArguments.parse(minArgumentString); + + final XacmlPdpParameterGroup parGroup = new XacmlPdpParameterHandler().getParameters(minArguments); + assertEquals(CommonTestData.PDPX_GROUP_NAME, parGroup.getName()); + } + + @Test + public void testXacmlPdpParameterGroup() throws PolicyXacmlPdpException { + final String[] xacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters.json"}; + + final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments(); + arguments.parse(xacmlPdpConfigParameters); + + final XacmlPdpParameterGroup parGroup = new XacmlPdpParameterHandler().getParameters(arguments); + assertTrue(arguments.checkSetConfigurationFilePath()); + assertEquals(CommonTestData.PDPX_GROUP_NAME, parGroup.getName()); + } + + @Test + public void testXacmlPdpParameterGroup_InvalidName() throws PolicyXacmlPdpException { + final String[] xacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters_InvalidName.json"}; + + final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments(); + arguments.parse(xacmlPdpConfigParameters); + + assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(arguments)).hasMessageContaining( + "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string"); + } + + @Test + public void testXacmlPdpParameterGroup_InvalidRestServerParameters() throws PolicyXacmlPdpException, IOException { + final String[] xacmlPdpConfigParameters = + {"-c", "parameters/XacmlPdpConfigParameters_InvalidRestServerParameters.json"}; + + final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments(); + arguments.parse(xacmlPdpConfigParameters); + + final String expectedResult = new String(Files.readAllBytes( + Paths.get("src/test/resources/expectedValidationResults/InvalidRestServerParameters.txt"))); + + assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(arguments)) + .hasMessageContaining("validation error(s) on parameters from " + + "\"parameters/XacmlPdpConfigParameters_InvalidRestServerParameters.json\""); + } + + @Test + public void testXacmlPdpVersion() throws PolicyXacmlPdpException { + final String[] xacmlPdpConfigParameters = {"-v"}; + final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments(); + final String version = arguments.parse(xacmlPdpConfigParameters); + assertTrue(version.startsWith("ONAP Policy Framework Xacml PDP Service")); + } + + @Test + public void testXacmlPdpHelp() throws PolicyXacmlPdpException { + final String[] xacmlPdpConfigParameters = {"-h"}; + final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments(); + final String help = arguments.parse(xacmlPdpConfigParameters); + assertTrue(help.startsWith("usage:")); + } + + @Test + public void testXacmlPdpInvalidOption() throws PolicyXacmlPdpException { + final String[] xacmlPdpConfigParameters = {"-d"}; + final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments(); + try { + arguments.parse(xacmlPdpConfigParameters); + } catch (final Exception exp) { + assertTrue(exp.getMessage().startsWith("invalid command line arguments specified")); + } + } +} diff --git a/main/src/test/java/org/onap/policy/pdpx/main/rest/TestStatisticsReport.java b/main/src/test/java/org/onap/policy/pdpx/main/rest/TestStatisticsReport.java new file mode 100644 index 00000000..a116a154 --- /dev/null +++ b/main/src/test/java/org/onap/policy/pdpx/main/rest/TestStatisticsReport.java @@ -0,0 +1,46 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pdpx.main.rest; + +import com.openpojo.reflection.filters.FilterClassName; +import com.openpojo.validation.Validator; +import com.openpojo.validation.ValidatorBuilder; +import com.openpojo.validation.rule.impl.SetterMustExistRule; +import com.openpojo.validation.test.impl.GetterTester; +import com.openpojo.validation.test.impl.SetterTester; + +import org.junit.Test; +import org.onap.policy.common.utils.validation.ToStringTester; + +/** + * Class to perform unit testing of {@link StatisticsReport}. + * + */ +public class TestStatisticsReport { + + @Test + public void testStatisticsReport() { + final Validator validator = ValidatorBuilder.create().with(new ToStringTester()).with(new SetterMustExistRule()) + .with(new SetterTester()).with(new GetterTester()).build(); + validator.validate(StatisticsReport.class.getPackage().getName(), + new FilterClassName(StatisticsReport.class.getName())); + } +} diff --git a/main/src/test/java/org/onap/policy/pdpx/main/rest/TestXacmlPdpRestServer.java b/main/src/test/java/org/onap/policy/pdpx/main/rest/TestXacmlPdpRestServer.java new file mode 100644 index 00000000..ce0671a6 --- /dev/null +++ b/main/src/test/java/org/onap/policy/pdpx/main/rest/TestXacmlPdpRestServer.java @@ -0,0 +1,121 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + + +package org.onap.policy.pdpx.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.pdpx.main.PolicyXacmlPdpException; +import org.onap.policy.pdpx.main.parameters.CommonTestData; +import org.onap.policy.pdpx.main.parameters.RestServerParameters; +import org.onap.policy.pdpx.main.startstop.Main; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * Class to perform unit test of HealthCheckMonitor. + * + */ +public class TestXacmlPdpRestServer { + + private static final Logger LOGGER = LoggerFactory.getLogger(TestXacmlPdpRestServer.class); + 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 Xacml PDP"; + + @Test + public void testHealthCheckSuccess() throws PolicyXacmlPdpException, InterruptedException { + final String reportString = "Report [name=Policy Xacml PDP, url=self, healthy=true, code=200, message=alive]"; + final Main main = startXacmlPdpService(); + final HealthCheckReport report = performHealthCheck(); + validateReport(NAME, SELF, true, 200, ALIVE, reportString, report); + stopXacmlPdpService(main); + } + + @Test + public void testHealthCheckFailure() throws InterruptedException { + final String reportString = + "Report [name=Policy Xacml PDP, url=self, healthy=false, code=500, message=not alive]"; + final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false); + restServerParams.setName(CommonTestData.PDPX_GROUP_NAME); + final XacmlPdpRestServer restServer = new XacmlPdpRestServer(restServerParams); + restServer.start(); + final HealthCheckReport report = performHealthCheck(); + validateReport(NAME, SELF, false, 500, NOT_ALIVE, reportString, report); + assertTrue(restServer.isAlive()); + assertTrue(restServer.toString().startsWith("XacmlPdpRestServer [servers=")); + restServer.shutdown(); + } + + private Main startXacmlPdpService() { + final String[] xacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters.json"}; + return new Main(xacmlPdpConfigParameters); + } + + private void stopXacmlPdpService(final Main main) throws PolicyXacmlPdpException { + main.shutdown(); + } + + private HealthCheckReport performHealthCheck() throws InterruptedException { + HealthCheckReport response = null; + 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); + + final long startTime = System.currentTimeMillis(); + while (response == null && (System.currentTimeMillis() - startTime) < 120000) { + try { + response = invocationBuilder.get(HealthCheckReport.class); + } catch (final Exception exp) { + LOGGER.info("the server is not started yet. We will retry again"); + } + } + 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/pdpx/main/rest/TestXacmlPdpStatistics.java b/main/src/test/java/org/onap/policy/pdpx/main/rest/TestXacmlPdpStatistics.java new file mode 100644 index 00000000..303a3cfe --- /dev/null +++ b/main/src/test/java/org/onap/policy/pdpx/main/rest/TestXacmlPdpStatistics.java @@ -0,0 +1,129 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + + +package org.onap.policy.pdpx.main.rest; + +import static org.junit.Assert.assertEquals; + +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.pdpx.main.PolicyXacmlPdpException; +import org.onap.policy.pdpx.main.parameters.CommonTestData; +import org.onap.policy.pdpx.main.parameters.RestServerParameters; +import org.onap.policy.pdpx.main.rest.XacmlPdpStatisticsManager; +import org.onap.policy.pdpx.main.startstop.Main; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Class to perform unit test of {@link XacmlPdpRestController}. + * + */ +public class TestXacmlPdpStatistics { + + private static final Logger LOGGER = LoggerFactory.getLogger(TestXacmlPdpStatistics.class); + + + @Test + public void testXacmlPdpStatistics_200() throws PolicyXacmlPdpException, InterruptedException { + final Main main = startXacmlPdpService(); + StatisticsReport report = getXacmlPdpStatistics(); + + validateReport(report, 0, 200); + updateXacmlPdpStatistics(); + report = getXacmlPdpStatistics(); + validateReport(report, 1, 200); + stopXacmlPdpService(main); + XacmlPdpStatisticsManager.resetAllStatistics(); + } + + @Test + public void testXacmlPdpStatistics_500() throws InterruptedException { + final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false); + restServerParams.setName(CommonTestData.PDPX_GROUP_NAME); + + final XacmlPdpRestServer restServer = new XacmlPdpRestServer(restServerParams); + restServer.start(); + final StatisticsReport report = getXacmlPdpStatistics(); + + validateReport(report, 0, 500); + restServer.shutdown(); + XacmlPdpStatisticsManager.resetAllStatistics(); + } + + + private Main startXacmlPdpService() { + final String[] XacmlPdpConfigParameters = + { "-c", "parameters/XacmlPdpConfigParameters.json" }; + return new Main(XacmlPdpConfigParameters); + } + + private void stopXacmlPdpService(final Main main) throws PolicyXacmlPdpException { + main.shutdown(); + } + + private StatisticsReport getXacmlPdpStatistics() throws InterruptedException { + StatisticsReport response = null; + 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/statistics"); + + final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); + final long startTime = System.currentTimeMillis(); + while (response == null && (System.currentTimeMillis() - startTime) < 120000) { + try { + response = invocationBuilder.get(StatisticsReport.class); + } catch (final Exception exp) { + LOGGER.info("the server is not started yet. We will retry again"); + } + } + return response; + } + + private void updateXacmlPdpStatistics() { + XacmlPdpStatisticsManager.updateTotalPoliciesCount(); + XacmlPdpStatisticsManager.updatePermitDecisionsCount(); + XacmlPdpStatisticsManager.updateDenyDecisionsCount(); + XacmlPdpStatisticsManager.updateIndeterminantDecisionsCount(); + XacmlPdpStatisticsManager.updateNotApplicableDecisionsCount(); + } + + private void validateReport(final StatisticsReport report, final int count, final int code) { + assertEquals(code, report.getCode()); + assertEquals(count, report.getTotalPoliciesCount()); + assertEquals(count, report.getPermitDecisionsCount()); + assertEquals(count, report.getDenyDecisionsCount()); + assertEquals(count, report.getIndeterminantDecisionsCount()); + assertEquals(count, report.getNotApplicableDecisionsCount()); + } +} diff --git a/main/src/test/java/org/onap/policy/pdpx/main/startstop/TestMain.java b/main/src/test/java/org/onap/policy/pdpx/main/startstop/TestMain.java new file mode 100644 index 00000000..8178343c --- /dev/null +++ b/main/src/test/java/org/onap/policy/pdpx/main/startstop/TestMain.java @@ -0,0 +1,72 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pdpx.main.startstop; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.onap.policy.pdpx.main.PolicyXacmlPdpException; +import org.onap.policy.pdpx.main.parameters.CommonTestData; + +/** + * Class to perform unit test of Main. + * + */ +public class TestMain { + + @Test + public void testMain() throws PolicyXacmlPdpException { + final String[] xacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters.json"}; + final Main main = new Main(xacmlPdpConfigParameters); + assertTrue(main.getParameters().isValid()); + assertEquals(CommonTestData.PDPX_GROUP_NAME, main.getParameters().getName()); + main.shutdown(); + } + + @Test + public void testMain_NoArguments() { + final String[] xacmlPdpConfigParameters = {}; + final Main main = new Main(xacmlPdpConfigParameters); + assertNull(main.getParameters()); + } + + @Test + public void testMain_InvalidArguments() { + final String[] xacmlPdpConfigParameters = {"parameters/XacmlPdpConfigParameters.json"}; + final Main main = new Main(xacmlPdpConfigParameters); + assertNull(main.getParameters()); + } + + @Test + public void testMain_Help() { + final String[] xacmlPdpConfigParameters = {"-h"}; + Main.main(xacmlPdpConfigParameters); + } + + @Test + public void testMain_InvalidParameters() { + final String[] xacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters_InvalidName.json"}; + final Main main = new Main(xacmlPdpConfigParameters); + assertNull(main.getParameters()); + } +} diff --git a/main/src/test/java/org/onap/policy/pdpx/main/startstop/TestXacmlPdpActivator.java b/main/src/test/java/org/onap/policy/pdpx/main/startstop/TestXacmlPdpActivator.java new file mode 100644 index 00000000..7a514f70 --- /dev/null +++ b/main/src/test/java/org/onap/policy/pdpx/main/startstop/TestXacmlPdpActivator.java @@ -0,0 +1,69 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pdpx.main.startstop; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.After; +import org.junit.BeforeClass; + +import org.junit.Test; +import org.onap.policy.pdpx.main.PolicyXacmlPdpException; +import org.onap.policy.pdpx.main.parameters.CommonTestData; +import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterGroup; +import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterHandler; + + +/** + * Class to perform unit test of XacmlPdpActivator. + * + */ +public class TestXacmlPdpActivator { + private static XacmlPdpActivator activator = null; + + /** + * Setup the tests. + * @throws PolicyXacmlPdpException when Xacml PDP Exceptional condition occurs + */ + @BeforeClass + public static void setup() throws PolicyXacmlPdpException { + final String[] xacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters.json"}; + + final XacmlPdpCommandLineArguments arguments = new XacmlPdpCommandLineArguments(xacmlPdpConfigParameters); + + final XacmlPdpParameterGroup parGroup = new XacmlPdpParameterHandler().getParameters(arguments); + + activator = new XacmlPdpActivator(parGroup); + activator.initialize(); + } + + @Test + public void testXacmlPdpActivator() throws PolicyXacmlPdpException { + assertTrue(activator.getParameterGroup().isValid()); + assertEquals(CommonTestData.PDPX_GROUP_NAME, activator.getParameterGroup().getName()); + } + + @After + public void teardown() throws PolicyXacmlPdpException { + activator.terminate(); + } +} |