aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/test/java/org
diff options
context:
space:
mode:
authorRashmi Pujar <rashmi.pujar1@bell.ca>2022-02-04 21:49:30 -0500
committerRashmi Pujar <rashmi.pujar1@bell.ca>2022-02-10 01:33:25 -0500
commit5f919673b13d8fd40a6a90b97f4e662ade068304 (patch)
treec24a727c3c60b228b28cd68df3d37ca56acd379e /main/src/test/java/org
parent2d4db3c6375ca761b25700226addea6060432b0d (diff)
Migrate policy api startup & config, controller to springboot
Issue-ID: POLICY-3753 Signed-off-by: Rashmi Pujar <rashmi.pujar1@bell.ca> Change-Id: I4537ed49b7fc57683bfc487f0d212642b7e69f17
Diffstat (limited to 'main/src/test/java/org')
-rw-r--r--main/src/test/java/org/onap/policy/api/main/parameters/CommonTestData.java119
-rw-r--r--main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterGroup.java152
-rw-r--r--main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterHandler.java175
-rw-r--r--main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java99
-rw-r--r--main/src/test/java/org/onap/policy/api/main/rest/TestApiStatisticsManager.java18
-rw-r--r--main/src/test/java/org/onap/policy/api/main/rest/TestCommonRestController.java13
-rw-r--r--main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java78
-rw-r--r--main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyTypeProvider.java72
-rw-r--r--main/src/test/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializerTest.java65
-rw-r--r--main/src/test/java/org/onap/policy/api/main/startstop/TestApiActivator.java54
-rw-r--r--main/src/test/java/org/onap/policy/api/main/startstop/TestApiCommandLineArguments.java68
-rw-r--r--main/src/test/java/org/onap/policy/api/main/startstop/TestMain.java80
12 files changed, 128 insertions, 865 deletions
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
deleted file mode 100644
index 32c44602..00000000
--- a/main/src/test/java/org/onap/policy/api/main/parameters/CommonTestData.java
+++ /dev/null
@@ -1,119 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * ONAP Policy API
- * ================================================================================
- * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
- * 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.parameters;
-
-import java.io.File;
-import java.io.IOException;
-import java.nio.charset.StandardCharsets;
-import java.nio.file.Files;
-import org.onap.policy.common.endpoints.parameters.RestServerParameters;
-import org.onap.policy.common.utils.coder.Coder;
-import org.onap.policy.common.utils.coder.CoderException;
-import org.onap.policy.common.utils.coder.StandardCoder;
-import org.onap.policy.common.utils.resources.TextFileUtils;
-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";
-
- /**
- * Server port, as it appears within the config files.
- */
- private static final String REST_SERVER_PORT = "6969";
-
- 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_DRIVER = "org.h2.Driver";
- 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";
-
- private Coder coder = new StandardCoder();
-
- /**
- * Returns an instance of RestServerParameters for test cases.
- *
- * @param isEmpty boolean value to represent that object created should be empty or not
- * @param port server port
- * @return the RestServerParameters object
- */
- public RestServerParameters getRestServerParameters(final boolean isEmpty, int port) {
- String fileName = "src/test/resources/parameters/"
- + (isEmpty ? "RestServerParametersEmpty" : "RestServerParameters") + ".json";
- try {
- String text = new String(Files.readAllBytes(new File(fileName).toPath()), StandardCharsets.UTF_8);
- text = text.replace("6969", String.valueOf(port));
- return coder.decode(text, RestServerParameters.class);
- } catch (CoderException | IOException e) {
- throw new RuntimeException("cannot read/decode " + fileName, e);
- }
- }
-
- /**
- * 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.setDatabaseDriver(DATABASE_DRIVER);
- databaseProviderParameters.setDatabaseUrl(DATABASE_URL);
- databaseProviderParameters.setDatabaseUser(DATABASE_USER);
- databaseProviderParameters.setDatabasePassword(DATABASE_PASSWORD);
- databaseProviderParameters.setPersistenceUnit(PERSISTENCE_UNIT);
- } else {
- databaseProviderParameters = new PolicyModelsProviderParameters();
- }
- return databaseProviderParameters;
- }
-
- /**
- * Copies a source file to a target file, replacing occurrances of
- * {@link #REST_SERVER_PORT} with the given port number.
- *
- * @param source source file name
- * @param target target file name
- * @param port port to be substituted
- * @throws IOException if an error occurs
- */
- public void makeParameters(String source, String target, int port) throws IOException {
- String text = TextFileUtils.getTextFileAsString(source);
- text = text.replace(REST_SERVER_PORT, String.valueOf(port));
-
- File file = new File(target);
- file.deleteOnExit();
- TextFileUtils.putStringAsFile(text, file);
- }
-}
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
deleted file mode 100644
index 28982489..00000000
--- a/main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterGroup.java
+++ /dev/null
@@ -1,152 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * ONAP Policy API
- * ================================================================================
- * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
- * Copyright (C) 2019, 2021 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.parameters;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-
-import java.util.Collections;
-import org.junit.Test;
-import org.onap.policy.common.endpoints.parameters.RestServerParameters;
-import org.onap.policy.common.parameters.ValidationResult;
-import org.onap.policy.models.provider.PolicyModelsProviderParameters;
-
-/**
- * Class to perform unit test of ApiParameterGroup.
- *
- */
-public class TestApiParameterGroup {
- private static final int PORT = 6969;
- private CommonTestData commonTestData = new CommonTestData();
-
- @Test
- public void testApiParameterGroup() {
- final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false, PORT);
- final PolicyModelsProviderParameters databaseProviderParameters =
- commonTestData.getDatabaseProviderParameters(false);
- final ApiParameterGroup apiParameters = new ApiParameterGroup(CommonTestData.API_GROUP_NAME,
- restServerParameters, databaseProviderParameters, Collections.emptyList(), Collections.emptyList());
- final ValidationResult 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.isHttps(), apiParameters.getRestServerParameters().isHttps());
- assertEquals(restServerParameters.isAaf(), apiParameters.getRestServerParameters().isAaf());
- assertEquals(CommonTestData.API_GROUP_NAME, apiParameters.getName());
- assertTrue(apiParameters.getPreloadPolicyTypes().isEmpty());
- }
-
- @Test
- public void testApiParameterGroup_NullName() {
- final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false, PORT);
- final PolicyModelsProviderParameters databaseProviderParameters =
- commonTestData.getDatabaseProviderParameters(false);
- final ApiParameterGroup apiParameters = new ApiParameterGroup(null, restServerParameters,
- databaseProviderParameters, Collections.emptyList(), Collections.emptyList());
- final ValidationResult validationResult = apiParameters.validate();
- assertFalse(validationResult.isValid());
- assertEquals(null, apiParameters.getName());
- assertThat(validationResult.getResult()).contains("\"name\" value \"null\" INVALID, is null");
- }
-
- @Test
- public void testApiParameterGroup_EmptyName() {
- final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false, PORT);
- final PolicyModelsProviderParameters databaseProviderParameters =
- commonTestData.getDatabaseProviderParameters(false);
- final ApiParameterGroup apiParameters = new ApiParameterGroup("", restServerParameters,
- databaseProviderParameters, Collections.emptyList(), Collections.emptyList());
- final ValidationResult validationResult = apiParameters.validate();
- assertFalse(validationResult.isValid());
- assertEquals("", apiParameters.getName());
- assertThat(validationResult.getResult()).contains("\"name\" value \"\" INVALID, is blank");
- }
-
- @Test
- public void testApiParameterGroup_EmptyRestServerParameters() {
- final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(true, PORT);
- final PolicyModelsProviderParameters databaseProviderParameters =
- commonTestData.getDatabaseProviderParameters(false);
- final ApiParameterGroup apiParameters = new ApiParameterGroup(CommonTestData.API_GROUP_NAME,
- restServerParameters, databaseProviderParameters, Collections.emptyList(), Collections.emptyList());
- final ValidationResult validationResult = apiParameters.validate();
- assertFalse(validationResult.isValid());
- assertThat(validationResult.getResult())
- .contains("\"RestServerParameters\" INVALID, item has status INVALID");
- }
-
- @Test
- public void testApiParameterGroup_NullRestServerParameters() {
- final RestServerParameters restServerParameters = null;
- final PolicyModelsProviderParameters databaseProviderParameters =
- commonTestData.getDatabaseProviderParameters(false);
- final ApiParameterGroup apiParameters = new ApiParameterGroup(CommonTestData.API_GROUP_NAME,
- restServerParameters, databaseProviderParameters, Collections.emptyList(), Collections.emptyList());
- final ValidationResult validationResult = apiParameters.validate();
- assertFalse(validationResult.isValid());
- assertThat(validationResult.getResult())
- .contains("item \"restServerParameters\" value \"null\" INVALID, is null");
- }
-
-
- @Test
- public void testApiParameterGroup_EmptyDatabaseProviderParameters() {
- final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false, PORT);
- final PolicyModelsProviderParameters databaseProviderParameters =
- commonTestData.getDatabaseProviderParameters(true);
- final ApiParameterGroup apiParameters = new ApiParameterGroup(CommonTestData.API_GROUP_NAME,
- restServerParameters, databaseProviderParameters, Collections.emptyList(), Collections.emptyList());
- final ValidationResult validationResult = apiParameters.validate();
- assertFalse(validationResult.isValid());
- assertThat(validationResult.getResult())
- .contains("\"PolicyModelsProviderParameters\" INVALID, item has status INVALID");
- }
-
- @Test
- public void testApiParameterGroup_NullDatabaseProviderParameters() {
- final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false, PORT);
- final PolicyModelsProviderParameters databaseProviderParameters = null;
- final ApiParameterGroup apiParameters = new ApiParameterGroup(CommonTestData.API_GROUP_NAME,
- restServerParameters, databaseProviderParameters, Collections.emptyList(), Collections.emptyList());
- final ValidationResult validationResult = apiParameters.validate();
- assertFalse(validationResult.isValid());
- assertThat(validationResult.getResult())
- .contains("item \"databaseProviderParameters\" value \"null\" INVALID, is null");
- }
-
- @Test
- public void testApiParameterGroup_SetName() {
- final ApiParameterGroup apiParameters = new ApiParameterGroup(CommonTestData.API_GROUP_NAME,
- null, null, Collections.emptyList(), Collections.emptyList());
- assertEquals(CommonTestData.API_GROUP_NAME, apiParameters.getName());
- apiParameters.setName("SampleName");
- assertEquals("SampleName", apiParameters.getName());
- }
-}
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
deleted file mode 100644
index 26ecd026..00000000
--- a/main/src/test/java/org/onap/policy/api/main/parameters/TestApiParameterHandler.java
+++ /dev/null
@@ -1,175 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
- * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2022 Nordix Foundation.
- * ================================================================================
- * 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.parameters;
-
-import static org.assertj.core.api.Assertions.assertThatThrownBy;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
-
-import org.junit.Test;
-import org.onap.policy.api.main.exception.PolicyApiException;
-import org.onap.policy.api.main.startstop.ApiCommandLineArguments;
-import org.onap.policy.common.utils.cmd.CommandLineException;
-
-/**
- * Class to perform unit test of ApiParameterHandler.
- *
- */
-public class TestApiParameterHandler {
- @Test
- public void testParameterHandlerNoParameterFile() throws PolicyApiException, CommandLineException {
- final String[] noArgumentString = {"-c", "parameters/NoParameterFile.json"};
- final ApiCommandLineArguments noArguments = new ApiCommandLineArguments();
- noArguments.parse(noArgumentString);
-
- try {
- new ApiParameterHandler().getParameters(noArguments);
- fail("test should throw an exception here");
- } catch (final Exception e) {
- assertTrue(e.getMessage().contains("FileNotFoundException"));
- }
- }
-
- @Test
- public void testParameterHandlerEmptyParameters() throws PolicyApiException, CommandLineException {
- final String[] emptyArgumentString = {"-c", "parameters/EmptyParameters.json"};
- final ApiCommandLineArguments emptyArguments = new ApiCommandLineArguments();
- emptyArguments.parse(emptyArgumentString);
-
- try {
- new ApiParameterHandler().getParameters(emptyArguments);
- fail("test should throw an exception here");
- } catch (final Exception e) {
- assertEquals("no parameters found in \"parameters/EmptyParameters.json\"", e.getMessage());
- }
- }
-
- @Test
- public void testParameterHandlerBadParameters() throws PolicyApiException, CommandLineException {
- final String[] badArgumentString = {"-c", "parameters/BadParameters.json"};
- final ApiCommandLineArguments badArguments = new ApiCommandLineArguments();
- badArguments.parse(badArgumentString);
-
- try {
- new ApiParameterHandler().getParameters(badArguments);
- fail("test should throw an exception here");
- } catch (final Exception e) {
- assertEquals("error reading parameters from \"parameters/BadParameters.json\"\n"
- + "(JsonSyntaxException):java.lang.IllegalStateException: "
- + "Expected a string but was BEGIN_ARRAY at line 2 column 15 path $.name", e.getMessage());
- }
- }
-
- @Test
- public void testParameterHandlerInvalidParameters() throws PolicyApiException, CommandLineException {
- final String[] invalidArgumentString = {"-c", "parameters/InvalidParameters.json"};
- final ApiCommandLineArguments invalidArguments = new ApiCommandLineArguments();
- invalidArguments.parse(invalidArgumentString);
-
- try {
- new ApiParameterHandler().getParameters(invalidArguments);
- fail("test should throw an exception here");
- } catch (final Exception e) {
- assertEquals("error reading parameters from \"parameters/InvalidParameters.json\"\n"
- + "(JsonSyntaxException):java.lang.IllegalStateException: "
- + "Expected a string but was BEGIN_ARRAY at line 2 column 15 path $.name", e.getMessage());
- }
- }
-
- @Test
- public void testParameterHandlerNoParameters() throws PolicyApiException, CommandLineException {
- final String[] noArgumentString = {"-c", "parameters/NoParameters.json"};
- final ApiCommandLineArguments noArguments = new ApiCommandLineArguments();
- noArguments.parse(noArgumentString);
-
- assertThatThrownBy(() -> new ApiParameterHandler().getParameters(noArguments))
- .hasMessageContaining("\"name\"", "is null");
- }
-
- @Test
- public void testParameterHandlerMinumumParameters() throws PolicyApiException, CommandLineException {
- final String[] minArgumentString = {"-c", "parameters/MinimumParameters.json"};
- final ApiCommandLineArguments minArguments = new ApiCommandLineArguments();
- minArguments.parse(minArgumentString);
- final ApiParameterGroup parGroup = new ApiParameterHandler().getParameters(minArguments);
- assertEquals(CommonTestData.API_GROUP_NAME, parGroup.getName());
- }
-
- @Test
- public void testApiParameterGroup() throws PolicyApiException, CommandLineException {
- final String[] apiConfigParameters = {"-c", "parameters/ApiConfigParameters_Https.json"};
- final ApiCommandLineArguments arguments = new ApiCommandLineArguments();
- arguments.parse(apiConfigParameters);
- final ApiParameterGroup parGroup = new ApiParameterHandler().getParameters(arguments);
- assertTrue(arguments.checkSetConfigurationFilePath());
- assertEquals(CommonTestData.API_GROUP_NAME, parGroup.getName());
- }
-
- @Test
- public void testApiParameterGroup_Postgres() throws PolicyApiException, CommandLineException {
- final String[] apiConfigParameters = {"-c", "parameters/ApiConfigParameters_Postgres.json"};
- final ApiCommandLineArguments arguments = new ApiCommandLineArguments();
- arguments.parse(apiConfigParameters);
- final ApiParameterGroup parGroup = new ApiParameterHandler().getParameters(arguments);
- assertTrue(arguments.checkSetConfigurationFilePath());
- assertEquals(CommonTestData.API_GROUP_NAME, parGroup.getName());
- }
-
- @Test
- public void testApiParameterGroup_Invalid() throws PolicyApiException, CommandLineException {
- final String[] apiConfigParameters = {"-c", "parameters/ApiConfigParameters_InvalidName.json"};
- final ApiCommandLineArguments arguments = new ApiCommandLineArguments();
- arguments.parse(apiConfigParameters);
-
- assertThatThrownBy(() -> new ApiParameterHandler().getParameters(arguments)).hasMessageContaining("\"name\"",
- "is null");
- }
-
- @Test
- public void testApiVersion() throws PolicyApiException, CommandLineException {
- final String[] apiConfigParameters = {"-v"};
- final ApiCommandLineArguments arguments = new ApiCommandLineArguments();
- final String version = arguments.parse(apiConfigParameters);
- assertTrue(version.startsWith("ONAP Policy Framework Api Service"));
- }
-
- @Test
- public void testApiHelp() throws PolicyApiException, CommandLineException {
- final String[] apiConfigParameters = {"-h"};
- final ApiCommandLineArguments arguments = new ApiCommandLineArguments();
- final String help = arguments.parse(apiConfigParameters);
- assertTrue(help.startsWith("usage:"));
- }
-
- @Test
- public void testApiInvalidOption() throws PolicyApiException {
- final String[] apiConfigParameters = {"-d"};
- final ApiCommandLineArguments arguments = new ApiCommandLineArguments();
- try {
- arguments.parse(apiConfigParameters);
- } catch (final Exception exp) {
- assertTrue(exp.getMessage().startsWith("invalid command line arguments specified"));
- }
- }
-}
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 9f22ff23..ae654c6c 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
@@ -3,7 +3,7 @@
* Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
* Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2019-2020 Nordix Foundation.
- * Modifications Copyright (C) 2020 Bell Canada.
+ * Modifications Copyright (C) 2020-2022 Bell Canada. 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.
@@ -32,10 +32,8 @@ import static org.junit.Assert.assertNull;
import java.io.File;
import java.io.IOException;
import java.security.SecureRandom;
-import java.util.Collections;
import java.util.List;
import java.util.Map;
-import java.util.Properties;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.ws.rs.client.Client;
@@ -47,34 +45,40 @@ import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
-import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
-import org.onap.policy.api.main.parameters.ApiParameterGroup;
-import org.onap.policy.api.main.parameters.CommonTestData;
-import org.onap.policy.api.main.rest.provider.PolicyProvider;
-import org.onap.policy.api.main.rest.provider.PolicyTypeProvider;
-import org.onap.policy.api.main.startstop.Main;
+import org.junit.runner.RunWith;
+import org.onap.policy.api.main.PolicyApiApplication;
import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler;
import org.onap.policy.common.endpoints.report.HealthCheckReport;
import org.onap.policy.common.gson.GsonMessageBodyHandler;
-import org.onap.policy.common.parameters.ParameterService;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.common.utils.coder.StandardYamlCoder;
import org.onap.policy.common.utils.network.NetworkUtil;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.common.utils.resources.TextFileUtils;
import org.onap.policy.common.utils.security.SelfSignedKeyStore;
-import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.errors.concepts.ErrorResponse;
-import org.onap.policy.models.provider.PolicyModelsProviderParameters;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.web.server.LocalServerPort;
+import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.annotation.DirtiesContext.ClassMode;
+import org.springframework.test.context.ActiveProfiles;
+import org.springframework.test.context.DynamicPropertyRegistry;
+import org.springframework.test.context.DynamicPropertySource;
+import org.springframework.test.context.junit4.SpringRunner;
/**
* Class to perform unit test of {@link ApiRestController}.
*
* @author Chenfei Gao (cgao@research.att.com)
*/
+@RunWith(SpringRunner.class)
+@SpringBootTest(classes = PolicyApiApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+@ActiveProfiles("test")
+@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
public class TestApiRestServer {
private static final String ALIVE = "alive";
@@ -156,70 +160,37 @@ public class TestApiRestServer {
"policies/vCPE.policy.operational.input.tosca.yaml";
private static final String POLICIES_VCPE_VERSION1 = "policies/onap.restart.tca/versions/1.0.0";
-
- private static PolicyModelsProviderParameters providerParams;
- private static ApiParameterGroup apiParamGroup;
- private static PolicyProvider policyProvider;
- private static PolicyTypeProvider policyTypeProvider;
-
// @formatter:on
private static final StandardCoder standardCoder = new StandardCoder();
+ private static StandardYamlCoder standardYamlCoder = new StandardYamlCoder();
+ private static SelfSignedKeyStore keystore;
- private static int apiPort;
- private static Main apiMain;
+ @LocalServerPort
+ private int apiPort;
- private static StandardYamlCoder standardYamlCoder = new StandardYamlCoder();
+ @Autowired
+ private ApiStatisticsManager mgr;
/**
* Initializes parameters and set up test environment.
*
- * @throws PfModelException the PfModel parsing exception
* @throws IOException on I/O exceptions
* @throws InterruptedException if interrupted
*/
@BeforeClass
- public static void setupParameters() throws PfModelException, IOException, InterruptedException {
- providerParams = new PolicyModelsProviderParameters();
- // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
- providerParams.setDatabaseDriver("org.h2.Driver");
- providerParams.setDatabaseUrl("jdbc:h2:mem:testdb");
- providerParams.setDatabaseUser("policy");
- providerParams.setDatabasePassword("P01icY");
- providerParams.setPersistenceUnit("ToscaConceptTest");
- apiParamGroup = new ApiParameterGroup("ApiGroup", null, providerParams, Collections.emptyList(),
- Collections.emptyList());
- ParameterService.register(apiParamGroup, true);
-
- policyTypeProvider = new PolicyTypeProvider();
- policyProvider = new PolicyProvider();
-
- apiPort = NetworkUtil.allocPort();
-
- final String[] apiConfigParameters = new String[2];
- final Properties systemProps = System.getProperties();
- systemProps.put("javax.net.ssl.keyStore", new SelfSignedKeyStore().getKeystoreName());
- systemProps.put("javax.net.ssl.keyStorePassword", SelfSignedKeyStore.KEYSTORE_PASSWORD);
- System.setProperties(systemProps);
- new CommonTestData().makeParameters("src/test/resources/parameters/ApiConfigParameters_Https.json",
- "src/test/resources/parameters/ApiConfigParameters_HttpsXXX.json", apiPort);
- apiConfigParameters[0] = "-c";
- apiConfigParameters[1] = "src/test/resources/parameters/ApiConfigParameters_HttpsXXX.json";
-
- apiMain = new Main(apiConfigParameters);
+ public static void setupParameters() throws IOException, InterruptedException {
+ keystore = new SelfSignedKeyStore();
}
- /**
- * Method for cleanup after each test.
- */
- @AfterClass
- public static void teardown() throws Exception {
- policyTypeProvider.close();
- policyProvider.close();
-
- if (apiMain != null) {
- apiMain.shutdown();
- }
+ @DynamicPropertySource
+ static void registerPgProperties(DynamicPropertyRegistry registry) {
+ registry.add("server.ssl.enabled", () -> "true");
+ registry.add("server.ssl.key-store", () -> keystore.getKeystoreName());
+ registry.add("server.ssl.key-store-password", () -> SelfSignedKeyStore.KEYSTORE_PASSWORD);
+ registry.add("server.ssl.key-store-type", () -> "PKCS12");
+ registry.add("server.ssl.key-alias", () -> "policy@policy.onap.org");
+ registry.add("server.ssl.key-password", () -> SelfSignedKeyStore.PRIVATE_KEY_PASSWORD);
}
@Test
@@ -378,7 +349,7 @@ public class TestApiRestServer {
invocationBuilder = sendHttpsRequest(STATISTICS_ENDPOINT, mediaType);
report = invocationBuilder.get(StatisticsReport.class);
validateStatisticsReport(report, 200);
- ApiStatisticsManager.resetAllStatistics();
+ // ApiStatisticsManager.resetAllStatistics();
}
@Test
@@ -798,8 +769,6 @@ public class TestApiRestServer {
}
private void updateApiStatistics() {
- var mgr = ApiStatisticsManager.getInstance();
-
mgr.updateTotalApiCallCount();
mgr.updateApiCallSuccessCount();
mgr.updateApiCallFailureCount();
@@ -831,4 +800,4 @@ public class TestApiRestServer {
assertEquals(code, report.getCode());
assertEquals(message, report.getMessage());
}
-}
+} \ No newline at end of file
diff --git a/main/src/test/java/org/onap/policy/api/main/rest/TestApiStatisticsManager.java b/main/src/test/java/org/onap/policy/api/main/rest/TestApiStatisticsManager.java
index 5f5c586d..bc67d21c 100644
--- a/main/src/test/java/org/onap/policy/api/main/rest/TestApiStatisticsManager.java
+++ b/main/src/test/java/org/onap/policy/api/main/rest/TestApiStatisticsManager.java
@@ -3,7 +3,7 @@
* ONAP Policy API
* ================================================================================
* Copyright (C) 2019 IBM.
- * Modifications Copyright (C) 2020 Bell Canada.
+ * Modifications Copyright (C) 2020-2022 Bell Canada.
* Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -27,13 +27,23 @@ package org.onap.policy.api.main.rest;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.onap.policy.api.main.PolicyApiApplication;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.ActiveProfiles;
+import org.springframework.test.context.junit4.SpringRunner;
+@RunWith(SpringRunner.class)
+@SpringBootTest(classes = PolicyApiApplication.class)
+@ActiveProfiles("test")
public class TestApiStatisticsManager {
+ @Autowired
+ private ApiStatisticsManager mgr = new ApiStatisticsManager();
+
@Test
public void testUpdateMethods() {
- var mgr = new ApiStatisticsManager();
-
assertEquals(1, mgr.updateTotalApiCallCount());
assertEquals(1, mgr.updateApiCallSuccessCount());
assertEquals(1, mgr.updateApiCallFailureCount());
@@ -56,4 +66,4 @@ public class TestApiStatisticsManager {
assertEquals(1, mgr.updatePolicyTypeDeleteSuccessCount());
assertEquals(1, mgr.updatePolicyTypeDeleteFailureCount());
}
-}
+} \ No newline at end of file
diff --git a/main/src/test/java/org/onap/policy/api/main/rest/TestCommonRestController.java b/main/src/test/java/org/onap/policy/api/main/rest/TestCommonRestController.java
index dc921488..2213bbf9 100644
--- a/main/src/test/java/org/onap/policy/api/main/rest/TestCommonRestController.java
+++ b/main/src/test/java/org/onap/policy/api/main/rest/TestCommonRestController.java
@@ -4,6 +4,7 @@
* ================================================================================
* Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2019 Nordix Foundation.
+ * Modifications Copyright (C) 2022 Bell Canada. 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.
@@ -23,13 +24,12 @@
package org.onap.policy.api.main.rest;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertSame;
import java.util.UUID;
-import javax.ws.rs.core.Response;
-import javax.ws.rs.core.Response.ResponseBuilder;
import org.junit.Test;
+import org.springframework.http.ResponseEntity;
/**
* Class to perform unit testing of CommonRestController.
@@ -40,9 +40,8 @@ public class TestCommonRestController {
@Test
public void testAddLoggingHeaders() {
UUID requestId = UUID.randomUUID();
- ResponseBuilder rb =
- crc.addLoggingHeaders(crc.addVersionControlHeaders(Response.status(Response.Status.OK)), requestId);
- assertSame(rb, rb.header("X-ONAP-RequestID", requestId));
+ ResponseEntity<Void> rb = crc.makeOkResponse(requestId, null);
+ assertEquals(requestId.toString(), rb.getHeaders().getFirst("X-ONAP-RequestID"));
}
/*
@@ -52,4 +51,4 @@ public class TestCommonRestController {
public void testToJsonNull() {
assertNull(crc.toJson(null));
}
-}
+} \ No newline at end of file
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
index e47a4ce6..ad394e33 100644
--- 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
@@ -4,7 +4,7 @@
* ================================================================================
* Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2019-2021 Nordix Foundation.
- * Modifications Copyright (C) 2020 Bell Canada.
+ * Modifications Copyright (C) 2020,2022 Bell Canada.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -33,13 +33,11 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
-import org.junit.After;
-import org.junit.Before;
+import org.junit.Ignore;
import org.junit.Test;
-import org.onap.policy.api.main.parameters.ApiParameterGroup;
-import org.onap.policy.common.parameters.ParameterService;
+import org.junit.runner.RunWith;
+import org.onap.policy.api.main.PolicyApiApplication;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.common.utils.coder.StandardYamlCoder;
@@ -52,24 +50,30 @@ import org.onap.policy.models.pdp.concepts.PdpSubGroup;
import org.onap.policy.models.pdp.enums.PdpHealthStatus;
import org.onap.policy.models.pdp.enums.PdpState;
import org.onap.policy.models.provider.PolicyModelsProvider;
-import org.onap.policy.models.provider.PolicyModelsProviderFactory;
-import org.onap.policy.models.provider.PolicyModelsProviderParameters;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.annotation.DirtiesContext.ClassMode;
+import org.springframework.test.context.ActiveProfiles;
+import org.springframework.test.context.junit4.SpringRunner;
/**
* This class performs unit test of {@link PolicyProvider}.
*
* @author Chenfei Gao (cgao@research.att.com)
*/
+// Provider classes will be obsolete upon migration to Hibernate
+@Ignore
+@RunWith(SpringRunner.class)
+@SpringBootTest(classes = PolicyApiApplication.class, properties = {"database.initialize=false"})
+@ActiveProfiles("test")
+@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
public class TestPolicyProvider {
- private static PolicyProvider policyProvider;
- private static PolicyTypeProvider policyTypeProvider;
- private static PolicyModelsProviderParameters providerParams;
- private static ApiParameterGroup apiParamGroup;
- private static StandardCoder standardCoder;
- private static StandardYamlCoder standardYamlCoder;
+ private static StandardCoder standardCoder = new StandardCoder();
+ private static StandardYamlCoder standardYamlCoder = new StandardYamlCoder();
private static final String POLICY_RESOURCE = "policies/vCPE.policy.monitoring.input.tosca.json";
private static final String POLICY_TYPE_RESOURCE = "policytypes/onap.policies.monitoring.tcagen2.yaml";
@@ -88,41 +92,12 @@ public class TestPolicyProvider {
private static final String POLICY_RESOURCE_OPERATIONAL = "policies/vCPE.policy.operational.input.tosca.json";
private static final String POLICY_TYPE_OPERATIONAL_DROOLS = "onap.policies.controlloop.operational.common.Drools";
- /**
- * Initializes parameters.
- *
- * @throws PfModelException the PfModel parsing exception
- */
- @Before
- public void setupParameters() throws PfModelException {
-
- standardCoder = new StandardCoder();
- standardYamlCoder = new StandardYamlCoder();
- providerParams = new PolicyModelsProviderParameters();
- providerParams.setDatabaseDriver("org.h2.Driver");
- providerParams.setDatabaseUrl("jdbc:h2:mem:testdb");
- providerParams.setDatabaseUser("policy");
- providerParams.setDatabasePassword("P01icY");
- providerParams.setPersistenceUnit("ToscaConceptTest");
- apiParamGroup = new ApiParameterGroup("ApiGroup", null, providerParams, Collections.emptyList(),
- Collections.emptyList());
- ParameterService.register(apiParamGroup, true);
- policyTypeProvider = new PolicyTypeProvider();
- policyProvider = new PolicyProvider();
- }
-
- /**
- * Closes up DB connections and deregisters API parameter group.
- *
- * @throws PfModelException the PfModel parsing exception
- */
- @After
- public void tearDown() throws PfModelException {
-
- policyTypeProvider.close();
- policyProvider.close();
- ParameterService.deregister(apiParamGroup);
- }
+ @Autowired
+ private PolicyProvider policyProvider;
+ @Autowired
+ private PolicyTypeProvider policyTypeProvider;
+ @Autowired
+ private PolicyModelsProvider databaseProvider;
@Test
public void testFetchPolicies() {
@@ -159,8 +134,7 @@ public class TestPolicyProvider {
String policyTypeVersion = "1.0.0";
String policyTypeId = "onap.policies.monitoring.cdap.tca.hi.lo.app";
- try (PolicyModelsProvider databaseProvider =
- new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParams)) {
+ try {
assertEquals(0, databaseProvider.getPdpGroups("name").size());
assertEquals(0, databaseProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).size());
@@ -511,4 +485,4 @@ public class TestPolicyProvider {
assertThatThrownBy(() -> policyProvider.deletePolicy(null, null, "onap.restart.tca.unavailable", "1.0.0"))
.hasMessageContaining("not found");
}
-}
+} \ No newline at end of file
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
index 355f3d45..6b7630c7 100644
--- 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
@@ -4,7 +4,7 @@
* ================================================================================
* Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2019-2021 Nordix Foundation.
- * Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
+ * Modifications Copyright (C) 2020-2022 Bell Canada. 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.
@@ -29,32 +29,43 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
-import java.util.Collections;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
+import org.junit.Ignore;
import org.junit.Test;
-import org.onap.policy.api.main.parameters.ApiParameterGroup;
-import org.onap.policy.common.parameters.ParameterService;
+import org.junit.runner.RunWith;
+import org.onap.policy.api.main.PolicyApiApplication;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardYamlCoder;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.models.base.PfModelException;
-import org.onap.policy.models.provider.PolicyModelsProviderParameters;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.annotation.DirtiesContext.ClassMode;
+import org.springframework.test.context.ActiveProfiles;
+import org.springframework.test.context.junit4.SpringRunner;
/**
* This class performs unit test of {@link PolicyTypeProvider}.
*
* @author Chenfei Gao (cgao@research.att.com)
*/
+// Provider classes will be obsolete upon migration to Hibernate
+@Ignore
+@RunWith(SpringRunner.class)
+@SpringBootTest(classes = PolicyApiApplication.class, properties = {"database.initialize=false"})
+@ActiveProfiles("test")
+@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
public class TestPolicyTypeProvider {
- private static PolicyTypeProvider policyTypeProvider;
- private static PolicyProvider policyProvider;
- private static PolicyModelsProviderParameters providerParams;
- private static ApiParameterGroup apiParamGroup;
- private static StandardYamlCoder standardYamlCoder;
+ private static StandardYamlCoder standardYamlCoder = new StandardYamlCoder();
+
+ @Autowired
+ private PolicyProvider policyProvider;
+
+ @Autowired
+ private PolicyTypeProvider policyTypeProvider;
private static final String POLICY_TYPE_VERSION = "1.0.0";
@@ -74,41 +85,6 @@ public class TestPolicyTypeProvider {
private static final String POLICY_TYPE_OPERATIONAL_APEX = "onap.policies.controlloop.operational.common.Apex";
private static final String POLICY_TYPE_OPERATIONAL_DROOLS = "onap.policies.controlloop.operational.common.Drools";
- /**
- * Initializes parameters.
- *
- * @throws PfModelException the PfModel parsing exception
- */
- @BeforeClass
- public static void setupParameters() throws PfModelException {
-
- standardYamlCoder = new StandardYamlCoder();
- providerParams = new PolicyModelsProviderParameters();
- providerParams.setDatabaseDriver("org.h2.Driver");
- providerParams.setDatabaseUrl("jdbc:h2:mem:testdb");
- providerParams.setDatabaseUser("policy");
- providerParams.setDatabasePassword("P01icY");
- providerParams.setPersistenceUnit("ToscaConceptTest");
- apiParamGroup = new ApiParameterGroup("ApiGroup", null, providerParams, Collections.emptyList(),
- Collections.emptyList());
- ParameterService.register(apiParamGroup, true);
- policyTypeProvider = new PolicyTypeProvider();
- policyProvider = new PolicyProvider();
- }
-
- /**
- * Closes up DB connections and deregisters API parameter group.
- *
- * @throws PfModelException the PfModel parsing exception
- */
- @AfterClass
- public static void tearDown() throws PfModelException {
-
- policyTypeProvider.close();
- policyProvider.close();
- ParameterService.deregister(apiParamGroup);
- }
-
@Test
public void testFetchPolicyTypes() throws Exception {
@@ -220,4 +196,4 @@ public class TestPolicyTypeProvider {
policyTypeProvider.deletePolicyType("onap.policies.monitoring.tcagen2", "1.0.0");
}).hasMessage("policy type onap.policies.monitoring.tcagen2:1.0.0 not found");
}
-}
+} \ No newline at end of file
diff --git a/main/src/test/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializerTest.java b/main/src/test/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializerTest.java
index f7e43b8d..aa86d739 100644
--- a/main/src/test/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializerTest.java
+++ b/main/src/test/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializerTest.java
@@ -3,6 +3,7 @@
* ONAP
* ================================================================================
* Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2022 Bell Canada. 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.
@@ -21,54 +22,36 @@
package org.onap.policy.api.main.startstop;
import static org.assertj.core.api.Assertions.assertThatCode;
-import static org.junit.Assert.fail;
-import java.io.File;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
import org.junit.Test;
-import org.onap.policy.api.main.exception.PolicyApiException;
-import org.onap.policy.api.main.parameters.ApiParameterGroup;
-import org.onap.policy.api.main.parameters.CommonTestData;
-import org.onap.policy.common.parameters.ValidationResult;
-import org.onap.policy.common.utils.coder.StandardCoder;
-import org.onap.policy.models.provider.PolicyModelsProvider;
-import org.onap.policy.models.provider.PolicyModelsProviderFactory;
-
+import org.junit.runner.RunWith;
+import org.onap.policy.api.main.PolicyApiApplication;
+import org.onap.policy.api.main.config.PolicyPreloadConfig;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.context.ActiveProfiles;
+import org.springframework.test.context.junit4.SpringRunner;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest(classes = PolicyApiApplication.class)
+@ActiveProfiles("test")
+@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class ApiDatabaseInitializerTest {
- private static final String PARAM_FILE = "src/test/resources/parameters/ApiConfigParameters_Https.json";
- private static final CommonTestData COMMON_TEST_DATA = new CommonTestData();
- private static ApiParameterGroup params;
- private static PolicyModelsProvider provider;
-
- /**
- * Creates the DB and keeps it open.
- */
- @BeforeClass
- public static void setUpBeforeClass() throws Exception {
- COMMON_TEST_DATA.makeParameters(PARAM_FILE, "src/test/resources/parameters/ApiConfigParametersXXX.json", 6969);
- params = new StandardCoder().decode(new File(PARAM_FILE), ApiParameterGroup.class);
- ValidationResult result = params.validate();
- if (!result.isValid()) {
- fail(result.getResult());
- }
+ @Autowired
+ private PolicyPreloadConfig params;
- // keep the DB open until the test completes
- provider = new PolicyModelsProviderFactory().createPolicyModelsProvider(params.getDatabaseProviderParameters());
- }
-
- @AfterClass
- public static void tearDownAfterClass() throws Exception {
- provider.close();
- }
+ @Autowired
+ private ApiDatabaseInitializer adi;
@Test
- public void testInitializeApiDatabase() throws PolicyApiException {
- ApiDatabaseInitializer adi = new ApiDatabaseInitializer();
- assertThatCode(() -> adi.initializeApiDatabase(params)).doesNotThrowAnyException();
+ public void testInitializeApiDatabase() {
+ assertThatCode(() -> adi.initializeApiDatabase(params.getPolicyTypes(),
+ params.getPolicies())).doesNotThrowAnyException();
// invoke it again - should still be OK
- assertThatCode(() -> adi.initializeApiDatabase(params)).doesNotThrowAnyException();
+ assertThatCode(() -> adi.initializeApiDatabase(params.getPolicyTypes(),
+ params.getPolicies())).doesNotThrowAnyException();
}
-}
+} \ No newline at end of file
diff --git a/main/src/test/java/org/onap/policy/api/main/startstop/TestApiActivator.java b/main/src/test/java/org/onap/policy/api/main/startstop/TestApiActivator.java
deleted file mode 100644
index ccd0f14d..00000000
--- a/main/src/test/java/org/onap/policy/api/main/startstop/TestApiActivator.java
+++ /dev/null
@@ -1,54 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
- * Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2021 Bell Canada. 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.startstop;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-
-import org.junit.Test;
-import org.onap.policy.api.main.parameters.ApiParameterGroup;
-import org.onap.policy.api.main.parameters.ApiParameterHandler;
-import org.onap.policy.api.main.parameters.CommonTestData;
-import org.onap.policy.common.utils.network.NetworkUtil;
-
-/**
- * Class to perform unit test of ApiActivator.
- *
- */
-public class TestApiActivator {
- private static final CommonTestData COMMON_TEST_DATA = new CommonTestData();
-
- @Test
- public void testApiActivator() throws Exception {
- COMMON_TEST_DATA.makeParameters("src/test/resources/parameters/ApiConfigParameters_Https.json",
- "src/test/resources/parameters/ApiConfigParametersXXX.json", NetworkUtil.allocPort());
- final String[] apiConfigParameters = {"-c", "src/test/resources/parameters/ApiConfigParametersXXX.json"};
- final ApiCommandLineArguments arguments = new ApiCommandLineArguments(apiConfigParameters);
- final ApiParameterGroup parGroup = new ApiParameterHandler().getParameters(arguments);
- final ApiActivator activator = new ApiActivator(parGroup);
- activator.initialize();
- assertTrue(activator.getParameterGroup().isValid());
- assertEquals(CommonTestData.API_GROUP_NAME, activator.getParameterGroup().getName());
- activator.terminate();
- }
-}
diff --git a/main/src/test/java/org/onap/policy/api/main/startstop/TestApiCommandLineArguments.java b/main/src/test/java/org/onap/policy/api/main/startstop/TestApiCommandLineArguments.java
deleted file mode 100644
index 8d3c42c9..00000000
--- a/main/src/test/java/org/onap/policy/api/main/startstop/TestApiCommandLineArguments.java
+++ /dev/null
@@ -1,68 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * ONAP Policy API
- * ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2021 Nordix Foundation.
- * ================================================================================
- * 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.startstop;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatThrownBy;
-
-import org.junit.Test;
-import org.onap.policy.api.main.exception.PolicyApiRuntimeException;
-
-public class TestApiCommandLineArguments {
- private ApiCommandLineArguments apiCmdArgs = new ApiCommandLineArguments();
-
- @Test(expected = PolicyApiRuntimeException.class)
- public void testApiCommandLineArgumentsStringArray() {
- String[] args = {"---d"};
- new ApiCommandLineArguments(args);
- }
-
- @Test
- public void testNonExistentFileValidateReadableFile() {
- apiCmdArgs.setConfigurationFilePath("src/test/resources/filetest/nonexist.json ");
- assertThatThrownBy(apiCmdArgs::validate)
- .hasMessageContaining("file \"src/test/resources/filetest/nonexist.json \" does not exist");
- }
-
- @Test
- public void testEmptyFileNameValidateReadableFile() {
- apiCmdArgs.setConfigurationFilePath("");
- assertThatThrownBy(apiCmdArgs::validate)
- .hasMessageContaining("policy-api configuration file was not specified as an argument");
- }
-
- @Test
- public void testInvalidUrlValidateReadableFile() {
- apiCmdArgs.setConfigurationFilePath("src/test\\resources/filetest\\n");
- assertThatThrownBy(apiCmdArgs::validate).hasMessageContaining(
- "policy-api configuration file \"src/test\\resources/filetest\\n\" does not exist");
- }
-
- @Test
- public void testVersion() {
- String[] testArgs = {"-v"};
- ApiCommandLineArguments cmdArgs = new ApiCommandLineArguments(testArgs);
- assertThat(cmdArgs.version()).startsWith("ONAP Policy Framework Api Service");
- }
-}
diff --git a/main/src/test/java/org/onap/policy/api/main/startstop/TestMain.java b/main/src/test/java/org/onap/policy/api/main/startstop/TestMain.java
deleted file mode 100644
index 6c8c514d..00000000
--- a/main/src/test/java/org/onap/policy/api/main/startstop/TestMain.java
+++ /dev/null
@@ -1,80 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
- * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2020-2021 Bell Canada. 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.startstop;
-
-import static org.assertj.core.api.Assertions.assertThatCode;
-import static org.assertj.core.api.Assertions.assertThatThrownBy;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-
-import org.junit.Test;
-import org.onap.policy.api.main.exception.PolicyApiRuntimeException;
-import org.onap.policy.api.main.parameters.CommonTestData;
-import org.onap.policy.common.utils.network.NetworkUtil;
-import org.onap.policy.common.utils.resources.MessageConstants;
-
-/**
- * Class to perform unit test of Main.
- *
- */
-public class TestMain {
- private static final CommonTestData COMMON_TEST_DATA = new CommonTestData();
-
- @Test
- public void testMain() throws Exception {
- COMMON_TEST_DATA.makeParameters("src/test/resources/parameters/ApiConfigParameters_Https.json",
- "src/test/resources/parameters/ApiConfigParametersXXX.json", NetworkUtil.allocPort());
- final String[] apiConfigParameters = {"-c", "src/test/resources/parameters/ApiConfigParametersXXX.json"};
- final Main main = new Main(apiConfigParameters);
- assertTrue(main.getParameterGroup().isValid());
- assertEquals(CommonTestData.API_GROUP_NAME, main.getParameterGroup().getName());
- main.shutdown();
- }
-
- @Test
- public void testMain_NoArguments() {
- final String[] apiConfigParameters = {};
- assertThatThrownBy(() -> new Main(apiConfigParameters)).isInstanceOf(PolicyApiRuntimeException.class)
- .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_API));
- }
-
- @Test
- public void testMain_InvalidArguments() {
- final String[] apiConfigParameters = {"parameters/ApiConfigParameters.json"};
- assertThatThrownBy(() -> new Main(apiConfigParameters)).isInstanceOf(PolicyApiRuntimeException.class)
- .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_API));
- }
-
- @Test
- public void testMain_Help() {
- final String[] apiConfigParameters = {"-h"};
- assertThatCode(() -> Main.main(apiConfigParameters)).doesNotThrowAnyException();
- }
-
- @Test
- public void testMain_InvalidParameters() {
- final String[] apiConfigParameters = {"-c", "parameters/ApiConfigParameters_InvalidName.json"};
- assertThatThrownBy(() -> new Main(apiConfigParameters)).isInstanceOf(PolicyApiRuntimeException.class)
- .hasMessage(String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_API));
- }
-}