diff options
author | liamfallon <liam.fallon@est.tech> | 2019-03-27 22:27:14 +0000 |
---|---|---|
committer | liamfallon <liam.fallon@est.tech> | 2019-03-27 22:27:14 +0000 |
commit | 6ccec5265d3431a1ca3265876b3df7bb422d9b62 (patch) | |
tree | c639987c73c76c533cddb526bb4d75a48df07ceb /models-tosca/src/test/java | |
parent | 2141dc0263940c94bf4265c6ca0072d9130a8ead (diff) |
Add support for legacy guard policies
Support for legacy guard policies added.
Support for translation of all legacy policies to
TOSCA format now complete.
Fix merge problems with dummy provider implementation.
Issue-ID: POLICY-1095
Change-Id: I3dd1775b78d39078a884e1834502b832ff40be18
Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'models-tosca/src/test/java')
-rw-r--r-- | models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/mapping/PlainToscaServiceTemplateMapperTest.java | 18 | ||||
-rw-r--r-- | models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyTest.java | 12 | ||||
-rw-r--r-- | models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/TestPojos.java | 52 | ||||
-rw-r--r-- | models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java | 295 | ||||
-rw-r--r-- | models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyOperationalTest.java (renamed from models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProviderTest.java) | 168 | ||||
-rw-r--r-- | models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/serialization/LegacyOperationalPolicySerializationTest.java | 15 | ||||
-rw-r--r-- | models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java | 5 |
7 files changed, 428 insertions, 137 deletions
diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/mapping/PlainToscaServiceTemplateMapperTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/mapping/PlainToscaServiceTemplateMapperTest.java index e9223b350..74b88e790 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/mapping/PlainToscaServiceTemplateMapperTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/mapping/PlainToscaServiceTemplateMapperTest.java @@ -25,11 +25,9 @@ package org.onap.policy.models.tosca.authorative.mapping; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import com.google.gson.Gson; -import com.google.gson.JsonSyntaxException; -import java.io.IOException; import org.junit.Before; import org.junit.Test; +import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.models.base.PfValidationResult; import org.onap.policy.models.tosca.authorative.concepts.PlainToscaServiceTemplate; @@ -43,21 +41,21 @@ import org.yaml.snakeyaml.Yaml; */ public class PlainToscaServiceTemplateMapperTest { - private Gson defaultGson; + private StandardCoder standardCoder; private PlainToscaServiceTemplateMapper mapper; @Before public void setUp() { - defaultGson = new Gson(); + standardCoder = new StandardCoder(); mapper = new PlainToscaServiceTemplateMapper(); } @Test - public void testPlainToscaPolicies() throws JsonSyntaxException, IOException { + public void testPlainToscaPolicies() throws Exception { try { String inputJson = ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"); - PlainToscaServiceTemplate plainPolicies = defaultGson.fromJson(inputJson, PlainToscaServiceTemplate.class); + PlainToscaServiceTemplate plainPolicies = standardCoder.decode(inputJson, PlainToscaServiceTemplate.class); ToscaServiceTemplate internalPolicies = mapper.toToscaServiceTemplate(plainPolicies); assertTrue(internalPolicies.validate(new PfValidationResult()).isValid()); PlainToscaServiceTemplate plainPolicies2 = mapper.fromToscaServiceTemplate(internalPolicies); @@ -69,15 +67,15 @@ public class PlainToscaServiceTemplateMapperTest { } @Test - public void testPlainToscaPolicyTypes() throws JsonSyntaxException, IOException { + public void testPlainToscaPolicyTypes() throws Exception { try { Yaml yaml = new Yaml(); String inputYaml = ResourceUtils.getResourceAsString( "policytypes/onap.policy.monitoring.cdap.tca.hi.lo.app.yaml"); Object yamlObject = yaml.load(inputYaml); - String yamlAsJsonString = defaultGson.toJson(yamlObject); + String yamlAsJsonString = standardCoder.encode(yamlObject); - PlainToscaServiceTemplate plainPolicyTypes = defaultGson.fromJson(yamlAsJsonString, + PlainToscaServiceTemplate plainPolicyTypes = standardCoder.decode(yamlAsJsonString, PlainToscaServiceTemplate.class); ToscaServiceTemplate internalPolicyTypes = mapper.toToscaServiceTemplate(plainPolicyTypes); assertTrue(internalPolicyTypes.validate(new PfValidationResult()).isValid()); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyTest.java index 764ce063f..5720b5594 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyTest.java @@ -27,22 +27,24 @@ import java.util.HashMap; import java.util.Map; import org.junit.Test; -import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicy; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; public class LegacyGuardPolicyTest { @Test public void test() { - LegacyGuardPolicy guard = new LegacyGuardPolicy(); + LegacyGuardPolicyInput guard = new LegacyGuardPolicyInput(); assertNotNull(guard); guard.setPolicyId("guard.frequency"); assertEquals("guard.frequency", guard.getPolicyId()); guard.setPolicyVersion("1"); assertEquals("1", guard.getPolicyVersion()); - Map<String, String> content = new HashMap<>(); - content.put("actor", "SO"); + Map<String, String> body = new HashMap<>(); + body.put("actor", "SO"); + LegacyGuardPolicyContent content = new LegacyGuardPolicyContent(); + content.setActor("SO"); guard.setContent(content); - assertEquals(1, guard.getContent().size()); + assertEquals("SO", guard.getContent().getActor()); } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/TestPojos.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/TestPojos.java new file mode 100644 index 000000000..f35a4e668 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/TestPojos.java @@ -0,0 +1,52 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Model + * ================================================================================ + * 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.models.tosca.legacy.concepts; + +import com.openpojo.reflection.filters.FilterPackageInfo; +import com.openpojo.validation.Validator; +import com.openpojo.validation.ValidatorBuilder; +import com.openpojo.validation.rule.impl.GetterMustExistRule; +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 tests of all pojos. + * + * @author Chenfei Gao (cgao@research.att.com) + * + */ +public class TestPojos { + + private static final String POJO_PACKAGE = "org.onap.policy.models.tosca.legacy.concepts"; + + @Test + public void testPojos() { + final Validator validator = ValidatorBuilder.create().with(new ToStringTester()) + .with(new SetterMustExistRule()).with(new GetterMustExistRule()).with(new SetterTester()) + .with(new GetterTester()).build(); + validator.validate(POJO_PACKAGE, new FilterPackageInfo()); + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java new file mode 100644 index 000000000..2fdc3aae7 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyGuardTest.java @@ -0,0 +1,295 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 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.models.tosca.legacy.provider; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.util.Map; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.models.dao.DaoParameters; +import org.onap.policy.models.dao.PfDao; +import org.onap.policy.models.dao.PfDaoFactory; +import org.onap.policy.models.dao.impl.DefaultPfDao; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyContent; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput; + +/** + * Test the {@link LegacyProvider} class for legacy guard policies. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class LegacyProvider4LegacyGuardTest { + private Connection connection; + private PfDao pfDao; + private StandardCoder standardCoder; + + + /** + * Set up the DAO towards the database. + * + * @throws Exception on database errors + */ + @Before + public void setupDao() throws Exception { + // Use the JDBC UI "jdbc:h2:mem:testdb" to test towards the h2 database + // Use the JDBC UI "jdbc:mariadb://localhost:3306/policy" to test towards a locally installed mariadb instance + connection = DriverManager.getConnection("jdbc:h2:mem:testdb", "policy", "P01icY"); + + final DaoParameters daoParameters = new DaoParameters(); + daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName()); + + // Use the persistence unit ToscaConceptTest to test towards the h2 database + // Use the persistence unit ToscaConceptMariaDBTest to test towards a locally installed mariadb instance + daoParameters.setPersistenceUnit("ToscaConceptTest"); + + pfDao = new PfDaoFactory().createPfDao(daoParameters); + pfDao.init(daoParameters); + } + + /** + * Set up standard coder. + */ + @Before + public void setupStandardCoder() { + standardCoder = new StandardCoder(); + } + + @After + public void teardown() throws Exception { + pfDao.close(); + connection.close(); + } + + @Test + public void testPoliciesGet() throws Exception { + assertThatThrownBy(() -> { + new LegacyProvider().getGuardPolicy(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new LegacyProvider().getGuardPolicy(null, ""); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new LegacyProvider().getGuardPolicy(pfDao, null); + }).hasMessage("policyId is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new LegacyProvider().getGuardPolicy(pfDao, "I Dont Exist"); + }).hasMessage("no policy found for policy ID: I Dont Exist"); + + LegacyGuardPolicyInput originalGip = standardCoder.decode( + ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"), + LegacyGuardPolicyInput.class); + + assertNotNull(originalGip); + + Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip); + + assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next()); + assertEquals(originalGip.getContent(), + createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); + + Map<String, LegacyGuardPolicyOutput> gotGopm = + new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId()); + + assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next()); + assertEquals(originalGip.getContent(), + gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); + + String expectedJsonOutput = + ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.output.json"); + String actualJsonOutput = standardCoder.encode(gotGopm); + + assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", "")); + } + + @Test + public void testPolicyCreate() throws Exception { + assertThatThrownBy(() -> { + new LegacyProvider().createGuardPolicy(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new LegacyProvider().createGuardPolicy(null, new LegacyGuardPolicyInput()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new LegacyProvider().createGuardPolicy(pfDao, null); + }).hasMessage("legacyGuardPolicy is marked @NonNull but is null"); + + LegacyGuardPolicyInput originalGip = standardCoder.decode( + ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"), + LegacyGuardPolicyInput.class); + + assertNotNull(originalGip); + + Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip); + + assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next()); + assertEquals(originalGip.getContent(), + createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); + + Map<String, LegacyGuardPolicyOutput> gotGopm = + new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId()); + + assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next()); + assertEquals(originalGip.getContent(), + gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); + + String expectedJsonOutput = + ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.output.json"); + String actualJsonOutput = standardCoder.encode(gotGopm); + + assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", "")); + } + + + @Test + public void testPolicyUpdate() throws Exception { + assertThatThrownBy(() -> { + new LegacyProvider().updateGuardPolicy(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new LegacyProvider().updateGuardPolicy(null, new LegacyGuardPolicyInput()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new LegacyProvider().updateGuardPolicy(pfDao, null); + }).hasMessage("legacyGuardPolicy is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new LegacyProvider().updateGuardPolicy(pfDao, new LegacyGuardPolicyInput()); + }).hasMessage("policy type for guard policy \"null\" unknown"); + + LegacyGuardPolicyInput originalGip = standardCoder.decode( + ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"), + LegacyGuardPolicyInput.class); + + assertNotNull(originalGip); + + Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip); + assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next()); + assertEquals(originalGip.getContent(), + createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); + + Map<String, LegacyGuardPolicyOutput> gotGopm = + new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId()); + + assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next()); + assertEquals(originalGip.getContent(), + gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); + + originalGip.getContent().setRecipe("Roast Turkey"); + Map<String, LegacyGuardPolicyOutput> updatedGp = new LegacyProvider().updateGuardPolicy(pfDao, originalGip); + assertEquals(originalGip.getPolicyId(), updatedGp.keySet().iterator().next()); + assertEquals(originalGip.getContent(), + updatedGp.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); + + Map<String, LegacyGuardPolicyOutput> gotUpdatedGopm = + new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId()); + assertEquals(originalGip.getPolicyId(), gotUpdatedGopm.keySet().iterator().next()); + assertEquals(originalGip.getContent(), + gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); + assertEquals("Roast Turkey", + gotUpdatedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next().getRecipe()); + } + + + @Test + public void testPoliciesDelete() throws Exception { + assertThatThrownBy(() -> { + new LegacyProvider().deleteGuardPolicy(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new LegacyProvider().deleteGuardPolicy(null, ""); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new LegacyProvider().deleteGuardPolicy(pfDao, null); + }).hasMessage("policyId is marked @NonNull but is null"); + + + assertThatThrownBy(() -> { + new LegacyProvider().deleteGuardPolicy(pfDao, "I Dont Exist"); + }).hasMessage("no policy found for policy ID: I Dont Exist"); + + LegacyGuardPolicyInput originalGip = standardCoder.decode( + ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.input.json"), + LegacyGuardPolicyInput.class); + + assertNotNull(originalGip); + + Map<String, LegacyGuardPolicyOutput> createdGopm = new LegacyProvider().createGuardPolicy(pfDao, originalGip); + assertEquals(originalGip.getPolicyId(), createdGopm.keySet().iterator().next()); + assertEquals(originalGip.getContent(), + createdGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); + + Map<String, LegacyGuardPolicyOutput> gotGopm = + new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId()); + + assertEquals(originalGip.getPolicyId(), gotGopm.keySet().iterator().next()); + assertEquals(originalGip.getContent(), + gotGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); + + String expectedJsonOutput = + ResourceUtils.getResourceAsString("policies/vDNS.policy.guard.frequency.output.json"); + String actualJsonOutput = standardCoder.encode(gotGopm); + + assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", "")); + + Map<String, LegacyGuardPolicyOutput> deletedGopm = + new LegacyProvider().deleteGuardPolicy(pfDao, originalGip.getPolicyId()); + assertEquals(originalGip.getPolicyId(), deletedGopm.keySet().iterator().next()); + assertEquals(originalGip.getContent(), + deletedGopm.get(originalGip.getPolicyId()).getProperties().values().iterator().next()); + + assertThatThrownBy(() -> { + new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId()); + }).hasMessage("no policy found for policy ID: guard.frequency.scaleout"); + + LegacyGuardPolicyInput otherGip = new LegacyGuardPolicyInput(); + otherGip.setPolicyId("guard.blacklist"); + otherGip.setPolicyVersion("1"); + otherGip.setContent(new LegacyGuardPolicyContent()); + + Map<String, LegacyGuardPolicyOutput> createdOtherGopm = new LegacyProvider().createGuardPolicy(pfDao, otherGip); + assertEquals(otherGip.getPolicyId(), createdOtherGopm.keySet().iterator().next()); + assertEquals(otherGip.getContent(), + createdOtherGopm.get(otherGip.getPolicyId()).getProperties().values().iterator().next()); + + assertThatThrownBy(() -> { + new LegacyProvider().getGuardPolicy(pfDao, originalGip.getPolicyId()); + }).hasMessage("no policy found for policy ID: guard.frequency.scaleout"); + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProviderTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyOperationalTest.java index 271e019d9..d198bd4f5 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProviderTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider4LegacyOperationalTest.java @@ -20,11 +20,9 @@ package org.onap.policy.models.tosca.legacy.provider; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; - -import com.google.gson.Gson; import java.sql.Connection; import java.sql.DriverManager; @@ -32,8 +30,8 @@ import java.sql.DriverManager; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.resources.ResourceUtils; -import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.dao.DaoParameters; import org.onap.policy.models.dao.PfDao; import org.onap.policy.models.dao.PfDaoFactory; @@ -41,15 +39,14 @@ import org.onap.policy.models.dao.impl.DefaultPfDao; import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; /** - * Test the {@link LegacyProvider} class. + * Test the {@link LegacyProvider} class for legacy operational policies. * * @author Liam Fallon (liam.fallon@est.tech) */ -public class LegacyProviderTest { +public class LegacyProvider4LegacyOperationalTest { private Connection connection; private PfDao pfDao; - private Gson gson; - + private StandardCoder standardCoder; /** * Set up the DAO towards the database. @@ -74,11 +71,11 @@ public class LegacyProviderTest { } /** - * Set up GSON. + * Set up standard coder. */ @Before - public void setupGson() { - gson = new Gson(); + public void setupStandardCoder() { + standardCoder = new StandardCoder(); } @After @@ -88,37 +85,25 @@ public class LegacyProviderTest { } @Test - public void testPoliciesGet() throws PfModelException { - try { + public void testPoliciesGet() throws Exception { + assertThatThrownBy(() -> { new LegacyProvider().getOperationalPolicy(null, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("dao is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new LegacyProvider().getOperationalPolicy(null, ""); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("dao is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new LegacyProvider().getOperationalPolicy(pfDao, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("policyId is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("policyId is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new LegacyProvider().getOperationalPolicy(pfDao, "I Dont Exist"); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("no policy found for policy ID: I Dont Exist", exc.getMessage()); - } + }).hasMessage("no policy found for policy ID: I Dont Exist"); LegacyOperationalPolicy originalLop = - gson.fromJson(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"), + standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"), LegacyOperationalPolicy.class); assertNotNull(originalLop); @@ -132,9 +117,9 @@ public class LegacyProviderTest { assertEquals(gotLop, originalLop); String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json"); - String actualJsonOutput = gson.toJson(gotLop); + String actualJsonOutput = standardCoder.encode(gotLop); - assertEquals(actualJsonOutput.replaceAll("\\s+", ""), expectedJsonOutput.replaceAll("\\s+", "")); + assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", "")); LegacyOperationalPolicy createdLopV2 = new LegacyProvider().createOperationalPolicy(pfDao, originalLop); LegacyOperationalPolicy gotLopV2 = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId()); @@ -142,30 +127,21 @@ public class LegacyProviderTest { } @Test - public void testPolicyCreate() throws PfModelException { - try { + public void testPolicyCreate() throws Exception { + assertThatThrownBy(() -> { new LegacyProvider().createOperationalPolicy(null, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("dao is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new LegacyProvider().createOperationalPolicy(null, new LegacyOperationalPolicy()); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("dao is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new LegacyProvider().createOperationalPolicy(pfDao, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("legacyOperationalPolicy is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null"); LegacyOperationalPolicy originalLop = - gson.fromJson(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"), + standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"), LegacyOperationalPolicy.class); assertNotNull(originalLop); @@ -179,44 +155,32 @@ public class LegacyProviderTest { assertEquals(gotLop, originalLop); String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json"); - String actualJsonOutput = gson.toJson(gotLop); + String actualJsonOutput = standardCoder.encode(gotLop); - assertEquals(actualJsonOutput.replaceAll("\\s+", ""), expectedJsonOutput.replaceAll("\\s+", "")); + assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", "")); } @Test - public void testPolicyUpdate() throws PfModelException { - try { + public void testPolicyUpdate() throws Exception { + assertThatThrownBy(() -> { new LegacyProvider().updateOperationalPolicy(null, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("dao is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new LegacyProvider().updateOperationalPolicy(null, new LegacyOperationalPolicy()); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("dao is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new LegacyProvider().updateOperationalPolicy(pfDao, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("legacyOperationalPolicy is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("legacyOperationalPolicy is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new LegacyProvider().updateOperationalPolicy(pfDao, new LegacyOperationalPolicy()); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("no policy found for policy ID: null", exc.getMessage()); - } + }).hasMessage("no policy found for policy ID: null"); LegacyOperationalPolicy originalLop = - gson.fromJson(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"), + standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"), LegacyOperationalPolicy.class); assertNotNull(originalLop); @@ -239,38 +203,26 @@ public class LegacyProviderTest { @Test - public void testPoliciesDelete() throws PfModelException { - try { + public void testPoliciesDelete() throws Exception { + assertThatThrownBy(() -> { new LegacyProvider().deleteOperationalPolicy(null, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("dao is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new LegacyProvider().deleteOperationalPolicy(null, ""); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("dao is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new LegacyProvider().deleteOperationalPolicy(pfDao, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("policyId is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("policyId is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new LegacyProvider().deleteOperationalPolicy(pfDao, "I Dont Exist"); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("no policy found for policy ID: I Dont Exist", exc.getMessage()); - } + }).hasMessage("no policy found for policy ID: I Dont Exist"); LegacyOperationalPolicy originalLop = - gson.fromJson(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"), + standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"), LegacyOperationalPolicy.class); assertNotNull(originalLop); @@ -283,20 +235,17 @@ public class LegacyProviderTest { assertEquals(gotLop, originalLop); String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json"); - String actualJsonOutput = gson.toJson(gotLop); + String actualJsonOutput = standardCoder.encode(gotLop); - assertEquals(actualJsonOutput.replaceAll("\\s+", ""), expectedJsonOutput.replaceAll("\\s+", "")); + assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", "")); LegacyOperationalPolicy deletedLop = new LegacyProvider().deleteOperationalPolicy(pfDao, originalLop.getPolicyId()); - assertEquals(deletedLop, originalLop); + assertEquals(originalLop, deletedLop); - try { + assertThatThrownBy(() -> { new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId()); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("no policy found for policy ID: operational.restart", exc.getMessage()); - } + }).hasMessage("no policy found for policy ID: operational.restart"); LegacyOperationalPolicy otherLop = new LegacyOperationalPolicy(); otherLop.setPolicyId("another-policy"); @@ -306,12 +255,9 @@ public class LegacyProviderTest { LegacyOperationalPolicy createdOtherLop = new LegacyProvider().createOperationalPolicy(pfDao, otherLop); assertEquals(otherLop, createdOtherLop); - try { + assertThatThrownBy(() -> { new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId()); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("no policy found for policy ID: operational.restart", exc.getMessage()); - } + }).hasMessage("no policy found for policy ID: operational.restart"); } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/serialization/LegacyOperationalPolicySerializationTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/serialization/LegacyOperationalPolicySerializationTest.java index 5d1fa42ad..c13583178 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/serialization/LegacyOperationalPolicySerializationTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/serialization/LegacyOperationalPolicySerializationTest.java @@ -24,13 +24,9 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import com.google.gson.Gson; -import com.google.gson.JsonSyntaxException; - -import java.io.IOException; - import org.junit.Before; import org.junit.Test; +import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.models.base.PfValidationResult; import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy; @@ -48,18 +44,19 @@ public class LegacyOperationalPolicySerializationTest { // Logger for this class private static final Logger LOGGER = LoggerFactory.getLogger(LegacyOperationalPolicySerializationTest.class); - private Gson gson; + private StandardCoder standardCoder; @Before public void setUp() { - gson = new Gson(); + standardCoder = new StandardCoder(); } @Test - public void testJsonDeserialization() throws JsonSyntaxException, IOException { + public void testJsonDeserialization() throws Exception { String vcpePolicyJson = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"); - LegacyOperationalPolicy legacyOperationalPolicy = gson.fromJson(vcpePolicyJson, LegacyOperationalPolicy.class); + LegacyOperationalPolicy legacyOperationalPolicy = + standardCoder.decode(vcpePolicyJson, LegacyOperationalPolicy.class); ToscaServiceTemplate serviceTemplate = new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java index 505e90e28..833e06a82 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java @@ -36,6 +36,7 @@ import java.io.IOException; import java.util.Map; import org.junit.Before; import org.junit.Test; +import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfValidationResult; @@ -129,12 +130,12 @@ public class MonitoringPolicySerializationTest { } private ToscaServiceTemplate deserializeMonitoringInputYaml(String resourcePath) - throws JsonSyntaxException, IOException { + throws Exception { Yaml yaml = new Yaml(); String policyYaml = ResourceUtils.getResourceAsString(resourcePath); Object yamlObject = yaml.load(policyYaml); - String yamlAsJsonString = new Gson().toJson(yamlObject); + String yamlAsJsonString = new StandardCoder().encode(yamlObject); ToscaServiceTemplate serviceTemplate = gson.fromJson(yamlAsJsonString, ToscaServiceTemplate.class); return serviceTemplate; } |