From e156fb8bf79ee361793af0aef4db5228c4c16f20 Mon Sep 17 00:00:00 2001 From: Chenfei Gao Date: Tue, 2 Apr 2019 08:55:17 -0400 Subject: Implement policy provider functions Includes: a) Implement policy provider functions b) Implement policy type provider functions c) Implement legacy guard policy provider functions d) Implement legacy operational policy provider functions e) Modify API endpoints to align with provider functions f) Hook up API statistics counter Junit tests for providers and new endpoints will be in next patch/review. Issue-ID: POLICY-1441 Change-Id: I113de95f6e0ea5f5436c072536f5e9a178988e5e Signed-off-by: Chenfei Gao --- .../exception/PolicyApiRuntimeExceptionTest.java | 43 ------------- .../policy/api/main/parameters/CommonTestData.java | 37 ++++++++++- .../api/main/parameters/TestApiParameterGroup.java | 71 ++++++++++++---------- .../main/parameters/TestApiParameterHandler.java | 3 +- .../policy/api/main/rest/TestApiRestServer.java | 25 ++------ .../provider/TestLegacyGuardPolicyProvider.java | 26 ++++++++ .../TestLegacyOperationalPolicyProvider.java | 26 ++++++++ .../api/main/rest/provider/TestPolicyProvider.java | 25 ++++++++ .../main/rest/provider/TestPolicyTypeProvider.java | 25 ++++++++ 9 files changed, 182 insertions(+), 99 deletions(-) delete mode 100644 main/src/test/java/org/onap/policy/api/main/exception/PolicyApiRuntimeExceptionTest.java create mode 100644 main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyGuardPolicyProvider.java create mode 100644 main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyOperationalPolicyProvider.java create mode 100644 main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java create mode 100644 main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyTypeProvider.java (limited to 'main/src/test/java') diff --git a/main/src/test/java/org/onap/policy/api/main/exception/PolicyApiRuntimeExceptionTest.java b/main/src/test/java/org/onap/policy/api/main/exception/PolicyApiRuntimeExceptionTest.java deleted file mode 100644 index f6770b1e..00000000 --- a/main/src/test/java/org/onap/policy/api/main/exception/PolicyApiRuntimeExceptionTest.java +++ /dev/null @@ -1,43 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 IBM. - * ================================================================================ - * 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.api.main.exception; - -import static org.junit.Assert.assertEquals; - -import org.junit.Before; -import org.junit.Test; - -public class PolicyApiRuntimeExceptionTest { - - private PolicyApiRuntimeException policyApiRuntimeException; - private String message = "test exception message"; - - @Before - public void setUp() { - policyApiRuntimeException = new PolicyApiRuntimeException(message); - } - - @Test - public void testContructor() { - policyApiRuntimeException = new PolicyApiRuntimeException(message, new Exception()); - assertEquals(message, policyApiRuntimeException.getMessage()); - } -} diff --git a/main/src/test/java/org/onap/policy/api/main/parameters/CommonTestData.java b/main/src/test/java/org/onap/policy/api/main/parameters/CommonTestData.java index 8ff2f98e..f0f971d4 100644 --- a/main/src/test/java/org/onap/policy/api/main/parameters/CommonTestData.java +++ b/main/src/test/java/org/onap/policy/api/main/parameters/CommonTestData.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= - * ONAP Policy API - * ================================================================================ + * ONAP Policy API + * ================================================================================ * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved. * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ @@ -23,19 +23,29 @@ package org.onap.policy.api.main.parameters; +import org.onap.policy.models.provider.PolicyModelsProviderParameters; + /** * Class to hold/create all parameters for test cases. * */ public class CommonTestData { + public static final String API_GROUP_NAME = "ApiGroup"; + 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"; private static final boolean REST_SERVER_HTTPS = false; private static final boolean REST_SERVER_AAF = false; - public static final String API_GROUP_NAME = "ApiGroup"; + + private static final String PROVIDER_GROUP_NAME = "PolicyProviderParameterGroup"; + private static final String PROVIDER_IMPL = "org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl"; + private static final String DATABASE_URL = "jdbc:h2:mem:testdb"; + private static final String DATABASE_USER = "policy"; + private static final String DATABASE_PASSWORD = "P01icY"; + private static final String PERSISTENCE_UNIT = "ToscaConceptTest"; /** * Returns an instance of RestServerParameters for test cases. @@ -54,4 +64,25 @@ public class CommonTestData { return restServerParameters; } + /** + * Returns an instance of PolicyModelsProviderParameters for test cases. + * + * @param isEmpty boolean value to represent that object created should be empty or not + * @return the PolicyModelsProviderParameters object + */ + public PolicyModelsProviderParameters getDatabaseProviderParameters(final boolean isEmpty) { + final PolicyModelsProviderParameters databaseProviderParameters; + if (!isEmpty) { + databaseProviderParameters = new PolicyModelsProviderParameters(); + databaseProviderParameters.setName(PROVIDER_GROUP_NAME); + databaseProviderParameters.setImplementation(PROVIDER_IMPL); + databaseProviderParameters.setDatabaseUrl(DATABASE_URL); + databaseProviderParameters.setDatabaseUser(DATABASE_USER); + databaseProviderParameters.setDatabasePassword(DATABASE_PASSWORD); + databaseProviderParameters.setPersistenceUnit(PERSISTENCE_UNIT); + } else { + databaseProviderParameters = new PolicyModelsProviderParameters(); + } + return databaseProviderParameters; + } } diff --git a/main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterGroup.java b/main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterGroup.java index 184f242b..8be52455 100644 --- a/main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterGroup.java +++ b/main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterGroup.java @@ -1,16 +1,15 @@ /*- * ============LICENSE_START======================================================= - * ONAP Policy API - * ================================================================================ + * ONAP Policy API + * ================================================================================ * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved. * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019 IBM. * ================================================================================ * 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 + * 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, @@ -30,6 +29,7 @@ import static org.junit.Assert.assertTrue; import org.junit.Test; import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.models.provider.PolicyModelsProviderParameters; /** * Class to perform unit test of ApiParameterGroup. @@ -41,14 +41,18 @@ public class TestApiParameterGroup { @Test public void testApiParameterGroup() { final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false); - final ApiParameterGroup apiParameters = new ApiParameterGroup(CommonTestData.API_GROUP_NAME, - restServerParameters); + final PolicyModelsProviderParameters databaseProviderParameters = + commonTestData.getDatabaseProviderParameters(false); + final ApiParameterGroup apiParameters = new ApiParameterGroup( + CommonTestData.API_GROUP_NAME, restServerParameters, databaseProviderParameters); final GroupValidationResult validationResult = apiParameters.validate(); assertTrue(validationResult.isValid()); assertEquals(restServerParameters.getHost(), apiParameters.getRestServerParameters().getHost()); assertEquals(restServerParameters.getPort(), apiParameters.getRestServerParameters().getPort()); - assertEquals(restServerParameters.getUserName(), apiParameters.getRestServerParameters().getUserName()); - assertEquals(restServerParameters.getPassword(), apiParameters.getRestServerParameters().getPassword()); + assertEquals(restServerParameters.getUserName(), + apiParameters.getRestServerParameters().getUserName()); + assertEquals(restServerParameters.getPassword(), + apiParameters.getRestServerParameters().getPassword()); assertEquals(restServerParameters.isHttps(), apiParameters.getRestServerParameters().isHttps()); assertEquals(restServerParameters.isAaf(), apiParameters.getRestServerParameters().isAaf()); assertEquals(CommonTestData.API_GROUP_NAME, apiParameters.getName()); @@ -57,52 +61,57 @@ public class TestApiParameterGroup { @Test public void testApiParameterGroup_NullName() { final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false); - final ApiParameterGroup apiParameters = new ApiParameterGroup(null, restServerParameters); + final PolicyModelsProviderParameters databaseProviderParameters = + commonTestData.getDatabaseProviderParameters(false); + final ApiParameterGroup apiParameters = new ApiParameterGroup(null, + restServerParameters, databaseProviderParameters); final GroupValidationResult validationResult = apiParameters.validate(); assertFalse(validationResult.isValid()); assertEquals(null, apiParameters.getName()); - assertTrue(validationResult.getResult().contains( - "field \"name\" type \"java.lang.String\" value \"null\" INVALID, " + "must be a non-blank string")); + assertTrue(validationResult.getResult() + .contains("field \"name\" type \"java.lang.String\" value \"null\" INVALID, " + + "must be a non-blank string")); } @Test public void testApiParameterGroup_EmptyName() { final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false); - - final ApiParameterGroup apiParameters = new ApiParameterGroup("", restServerParameters); + final PolicyModelsProviderParameters databaseProviderParameters = + commonTestData.getDatabaseProviderParameters(false); + final ApiParameterGroup apiParameters = new ApiParameterGroup("", + restServerParameters, databaseProviderParameters); final GroupValidationResult validationResult = apiParameters.validate(); assertFalse(validationResult.isValid()); assertEquals("", apiParameters.getName()); - assertTrue(validationResult.getResult().contains( - "field \"name\" type \"java.lang.String\" value \"\" INVALID, " + "must be a non-blank string")); + assertTrue(validationResult.getResult().contains("field \"name\" type \"java.lang.String\" value \"\" INVALID, " + + "must be a non-blank string")); } @Test public void testApiParameterGroup_EmptyRestServerParameters() { final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(true); - - final ApiParameterGroup apiParameters = new ApiParameterGroup(CommonTestData.API_GROUP_NAME, - restServerParameters); + final PolicyModelsProviderParameters databaseProviderParameters = + commonTestData.getDatabaseProviderParameters(false); + final ApiParameterGroup apiParameters = new ApiParameterGroup( + CommonTestData.API_GROUP_NAME, restServerParameters, databaseProviderParameters); final GroupValidationResult validationResult = apiParameters.validate(); assertFalse(validationResult.isValid()); assertTrue(validationResult.getResult() - .contains("\"org.onap.policy.api.main.parameters.RestServerParameters\" INVALID, " - + "parameter group has status INVALID")); + .contains("\"org.onap.policy.api.main.parameters.RestServerParameters\" INVALID, " + + "parameter group has status INVALID")); } @Test - public void testName() { + public void testApiParameterGroup_EmptyDatabaseProviderParameters() { final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false); - - final ApiParameterGroup apiParameters = new ApiParameterGroup("", restServerParameters); - apiParameters.setName("name"); - assertEquals("name", apiParameters.getName()); - } - - @Test - public void testVaildateForNullRestServiceParameters() { - final ApiParameterGroup apiParameters = new ApiParameterGroup(CommonTestData.API_GROUP_NAME, null); + final PolicyModelsProviderParameters databaseProviderParameters = + commonTestData.getDatabaseProviderParameters(true); + final ApiParameterGroup apiParameters = new ApiParameterGroup( + CommonTestData.API_GROUP_NAME, restServerParameters, databaseProviderParameters); final GroupValidationResult validationResult = apiParameters.validate(); - assertTrue(validationResult.getResult().contains("parameter group has status INVALID")); + assertFalse(validationResult.isValid()); + assertTrue(validationResult.getResult() + .contains("\"org.onap.policy.models.provider.PolicyModelsProviderParameters\" INVALID, " + + "parameter group has status INVALID")); } } diff --git a/main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterHandler.java b/main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterHandler.java index 71d8c357..480a5d8a 100644 --- a/main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterHandler.java +++ b/main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterHandler.java @@ -28,7 +28,6 @@ import static org.junit.Assert.fail; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; - import org.junit.Test; import org.onap.policy.api.main.exception.PolicyApiException; import org.onap.policy.api.main.startstop.ApiCommandLineArguments; @@ -153,7 +152,7 @@ public class TestApiParameterHandler { @Test public void testApiParameterGroup_InvalidRestServerParameters() throws PolicyApiException, IOException { - final String[] apiConfigParameters = + final String[] apiConfigParameters = { "-c", "parameters/ApiConfigParameters_InvalidRestServerParameters.json" }; final ApiCommandLineArguments arguments = new ApiCommandLineArguments(); arguments.parse(apiConfigParameters); diff --git a/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java b/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java index 3c8b251d..4fb588c2 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java @@ -140,11 +140,11 @@ public class TestApiRestServer { main = startApiService(true); Invocation.Builder invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT); StatisticsReport report = invocationBuilder.get(StatisticsReport.class); - validateStatisticsReport(report, 0, 200); + validateStatisticsReport(report, 200); updateApiStatistics(); invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT); report = invocationBuilder.get(StatisticsReport.class); - validateStatisticsReport(report, 1, 200); + validateStatisticsReport(report, 200); ApiStatisticsManager.resetAllStatistics(); } catch (final Exception exp) { LOGGER.error("testApiStatistics_200 failed", exp); @@ -161,7 +161,7 @@ public class TestApiRestServer { restServer.start(); final Invocation.Builder invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT); final StatisticsReport report = invocationBuilder.get(StatisticsReport.class); - validateStatisticsReport(report, 0, 500); + validateStatisticsReport(report, 500); ApiStatisticsManager.resetAllStatistics(); } catch (final Exception exp) { LOGGER.error("testApiStatistics_500 failed", exp); @@ -175,7 +175,7 @@ public class TestApiRestServer { main = startApiService(false); final Invocation.Builder invocationBuilder = sendHttpsRequest(STATISTICS_ENDPOINT); final StatisticsReport report = invocationBuilder.get(StatisticsReport.class); - validateStatisticsReport(report, 0, 200); + validateStatisticsReport(report, 200); } catch (final Exception exp) { LOGGER.error("testHttpsApiStatistics failed", exp); fail("Test should not throw an exception"); @@ -283,23 +283,8 @@ public class TestApiRestServer { ApiStatisticsManager.updatePolicyTypePostFailureCount(); } - private void validateStatisticsReport(final StatisticsReport report, final int count, final int code) { + private void validateStatisticsReport(final StatisticsReport report, final int code) { assertEquals(code, report.getCode()); - assertEquals(count, report.getTotalApiCallCount()); - assertEquals(count, report.getApiCallSuccessCount()); - assertEquals(count, report.getApiCallFailureCount()); - assertEquals(count, report.getTotalPolicyGetCount()); - assertEquals(count, report.getTotalPolicyPostCount()); - assertEquals(count, report.getTotalPolicyTypeGetCount()); - assertEquals(count, report.getTotalPolicyTypePostCount()); - assertEquals(count, report.getPolicyGetSuccessCount()); - assertEquals(count, report.getPolicyGetFailureCount()); - assertEquals(count, report.getPolicyPostSuccessCount()); - assertEquals(count, report.getPolicyPostFailureCount()); - assertEquals(count, report.getPolicyTypeGetSuccessCount()); - assertEquals(count, report.getPolicyTypeGetFailureCount()); - assertEquals(count, report.getPolicyTypePostSuccessCount()); - assertEquals(count, report.getPolicyTypePostFailureCount()); } private void validateHealthCheckReport(final String name, final String url, final boolean healthy, final int code, diff --git a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyGuardPolicyProvider.java b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyGuardPolicyProvider.java new file mode 100644 index 00000000..8f4f9aed --- /dev/null +++ b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyGuardPolicyProvider.java @@ -0,0 +1,26 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy API + * ================================================================================ + * 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.api.main.rest.provider; + +public class TestLegacyGuardPolicyProvider {} diff --git a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyOperationalPolicyProvider.java b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyOperationalPolicyProvider.java new file mode 100644 index 00000000..de12fca0 --- /dev/null +++ b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestLegacyOperationalPolicyProvider.java @@ -0,0 +1,26 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy API + * ================================================================================ + * 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.api.main.rest.provider; + +public class TestLegacyOperationalPolicyProvider {} diff --git a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java new file mode 100644 index 00000000..11d7e404 --- /dev/null +++ b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java @@ -0,0 +1,25 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy API + * ================================================================================ + * 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.api.main.rest.provider; + +public class TestPolicyProvider {} diff --git a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyTypeProvider.java b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyTypeProvider.java new file mode 100644 index 00000000..d60dc2e5 --- /dev/null +++ b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyTypeProvider.java @@ -0,0 +1,25 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy API + * ================================================================================ + * 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.api.main.rest.provider; + +public class TestPolicyTypeProvider {} -- cgit 1.2.3-korg