/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019-2020 Nordix Foundation.
+ * Copyright (C) 2019-2021 Nordix Foundation.
* Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
import static org.junit.Assert.assertTrue;
import com.google.gson.GsonBuilder;
-
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Properties;
-
import org.apache.commons.lang3.ObjectUtils;
import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.junit.After;
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.authorative.concepts.ToscaEntityFilter;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
import org.yaml.snakeyaml.Yaml;
/**
* Test of the {@link AuthorativeToscaProvider} class.
- *
- * @author Liam Fallon (liam.fallon@est.tech)
*/
public class AuthorativeToscaProviderPolicyTypeTest {
private static final String VERSION = "version";
jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
- // H2, use "org.mariadb.jdbc.Driver" and "jdbc:mariadb://localhost:3306/policy" for locally installed MariaDB
- jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
- jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb");
+ if (System.getProperty("USE-MARIADB") != null) {
+ jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.mariadb.jdbc.Driver");
+ jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:mariadb://localhost:3306/policy");
+ } else {
+ jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
+ jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb");
+ }
daoParameters.setJdbcProperties(jdbcProperties);
assertNotNull(toscaServiceTemplate);
ToscaServiceTemplate createdServiceTemplate =
- new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
+ new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
- ToscaServiceTemplate gotServiceTemplate =
- new AuthorativeToscaProvider().getPolicyTypes(pfDao, policyTypeKey.getName(), policyTypeKey.getVersion());
+ ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao,
+ policyTypeKey.getName(), policyTypeKey.getVersion());
ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
List<ToscaPolicyType> gotPolicyTypeList =
- new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001);
+ new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001);
assertEquals(2, gotPolicyTypeList.size());
assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
assertEquals(2, gotPolicyTypeList.size());
assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
- assertThatThrownBy(
- () -> new AuthorativeToscaProvider().getPolicyTypeList(new DefaultPfDao(), POLICY_NO_VERSION, VERSION_001))
- .hasMessageContaining("Policy Framework DAO has not been initialized");
+ assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypeList(new DefaultPfDao(), POLICY_NO_VERSION,
+ VERSION_001)).hasMessageContaining("Policy Framework DAO has not been initialized");
assertTrue(new AuthorativeToscaProvider().getPolicyTypeList(pfDao, "i.dont.Exist", VERSION_001).isEmpty());
}
}).hasMessageMatching(DAO_IS_NULL);
assertThatThrownBy(() -> {
- new AuthorativeToscaProvider().getFilteredPolicyTypes(null, ToscaPolicyTypeFilter.builder().build());
+ new AuthorativeToscaProvider().getFilteredPolicyTypes(null,
+ ToscaEntityFilter.<ToscaPolicyType>builder().build());
}).hasMessageMatching(DAO_IS_NULL);
assertThatThrownBy(() -> {
}).hasMessageMatching(DAO_IS_NULL);
assertThatThrownBy(() -> {
- new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, ToscaPolicyTypeFilter.builder().build());
+ new AuthorativeToscaProvider().getFilteredPolicyTypeList(null,
+ ToscaEntityFilter.<ToscaPolicyType>builder().build());
}).hasMessageMatching(DAO_IS_NULL);
assertThatThrownBy(() -> {
}).hasMessageMatching("^filter is marked .*on.*ull but is null$");
assertThatThrownBy(() -> new AuthorativeToscaProvider().getFilteredPolicyTypeList(new DefaultPfDao(),
- ToscaPolicyTypeFilter.builder().name("i.dont.Exist").build()))
- .hasMessageContaining("Policy Framework DAO has not been initialized");
+ ToscaEntityFilter.<ToscaPolicyType>builder().name("i.dont.Exist").build()))
+ .hasMessageContaining("Policy Framework DAO has not been initialized");
- assertTrue(new AuthorativeToscaProvider()
- .getFilteredPolicyTypeList(pfDao, ToscaPolicyTypeFilter.builder().name("i.dont.Exist").build()).isEmpty());
+ assertTrue(new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
+ ToscaEntityFilter.<ToscaPolicyType>builder().name("i.dont.Exist").build()).isEmpty());
ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
assertNotNull(toscaServiceTemplate);
ToscaServiceTemplate createdServiceTemplate =
- new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
+ new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
- ToscaServiceTemplate gotServiceTemplate =
- new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaPolicyTypeFilter.builder().build());
+ ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
+ ToscaEntityFilter.<ToscaPolicyType>builder().build());
ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
- assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
- assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
+ checkEqualsNameDescription(beforePolicyType, gotPolicyType);
gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
- ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).build());
+ ToscaEntityFilter.<ToscaPolicyType>builder().name(policyTypeKey.getName()).build());
gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
- assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
- assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
+ checkEqualsNameDescription(beforePolicyType, gotPolicyType);
- gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao,
- ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).version(VERSION_001).build());
+ gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaEntityFilter
+ .<ToscaPolicyType>builder().name(policyTypeKey.getName()).version(VERSION_001).build());
gotPolicyType = gotServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
- assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
- assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
+ checkEqualsNameDescription(beforePolicyType, gotPolicyType);
List<ToscaPolicyType> gotPolicyTypeList =
- new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001);
+ new AuthorativeToscaProvider().getPolicyTypeList(pfDao, POLICY_NO_VERSION, VERSION_001);
assertEquals(2, gotPolicyTypeList.size());
assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
- gotPolicyTypeList =
- new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, ToscaPolicyTypeFilter.builder().build());
+ gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
+ ToscaEntityFilter.<ToscaPolicyType>builder().build());
assertEquals(2, gotPolicyTypeList.size());
assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
- ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).build());
+ ToscaEntityFilter.<ToscaPolicyType>builder().name(policyTypeKey.getName()).build());
assertEquals(1, gotPolicyTypeList.size());
assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
- gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
- ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).version(VERSION_001).build());
+ gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, ToscaEntityFilter
+ .<ToscaPolicyType>builder().name(policyTypeKey.getName()).version(VERSION_001).build());
assertEquals(1, gotPolicyTypeList.size());
assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao,
- ToscaPolicyTypeFilter.builder().version("1.0.0").build());
+ ToscaEntityFilter.<ToscaPolicyType>builder().version("1.0.0").build());
assertEquals(1, gotPolicyTypeList.size());
assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName()));
}
+ private void checkEqualsNameDescription(ToscaPolicyType beforePolicyType, ToscaPolicyType gotPolicyType) {
+ assertEquals(beforePolicyType.getName(), gotPolicyType.getName());
+ assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription()));
+ }
+
@Test
public void testPolicyTypesCreate() throws Exception {
assertThatThrownBy(() -> {
assertNotNull(toscaServiceTemplate);
ToscaServiceTemplate createdServiceTemplate =
- new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
+ new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
assertNotNull(toscaServiceTemplate);
ToscaServiceTemplate createdServiceTemplate =
- new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
+ new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
ToscaServiceTemplate updatedServiceTemplate =
- new AuthorativeToscaProvider().updatePolicyTypes(pfDao, toscaServiceTemplate);
+ new AuthorativeToscaProvider().updatePolicyTypes(pfDao, toscaServiceTemplate);
ToscaPolicyType updatedPolicy = updatedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
assertEquals(true, beforePolicyType.getName().equals(updatedPolicy.getName()));
assertNotNull(toscaServiceTemplate);
ToscaServiceTemplate createdServiceTemplate =
- new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
+ new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate);
PfConceptKey policyTypeKey = new PfConceptKey(POLICY_NO_VERSION_VERSION1);
assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName()));
assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription()));
- ToscaServiceTemplate deletedServiceTemplate =
- new AuthorativeToscaProvider().deletePolicyType(pfDao, policyTypeKey.getName(), policyTypeKey.getVersion());
+ ToscaServiceTemplate deletedServiceTemplate = new AuthorativeToscaProvider().deletePolicyType(pfDao,
+ policyTypeKey.getName(), policyTypeKey.getVersion());
ToscaPolicyType deletedPolicy = deletedServiceTemplate.getPolicyTypes().get(policyTypeKey.getName());
assertEquals(true, beforePolicyType.getName().equals(deletedPolicy.getName()));
new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate);
}).hasMessage("An incoming list of concepts must have at least one entry");
}
+
+ @Test
+ public void testNullParameters() throws Exception {
+ assertThatThrownBy(() -> new AuthorativeToscaProvider().getPolicyTypeList(null, null, null))
+ .hasMessageMatching("^dao is marked .*on.*ull but is null$");
+ }
}