aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRashmi Pujar <rashmi.pujar1@bell.ca>2022-02-22 01:39:00 -0500
committerRashmi Pujar <rashmi.pujar1@bell.ca>2022-02-23 01:01:51 -0500
commitbc9b5183122abf075bc48b4c7add2ad1ef887fad (patch)
treebcd320681437b13e91c75ee547bdc2ef951f531f
parent19efd9034bd19bea5e2506328ee59bf5d3f27172 (diff)
Spring repository and service layer for policy-api
- Add the spring repository and service layers to policy-api. - Unit tests are modified to use the spring service layers Next-up: Migrate the usage of policy-models-provider in policy-api to spring boot based services to talk to database (POLICY-3924) Issue-ID: POLICY-3923 Signed-off-by: Rashmi Pujar <rashmi.pujar1@bell.ca> Change-Id: Ib6840040b32f24f019da802d3b246dab1bfccbe3
-rw-r--r--main/pom.xml5
-rw-r--r--main/src/main/java/org/onap/policy/api/main/PolicyApiApplication.java3
-rw-r--r--main/src/main/java/org/onap/policy/api/main/repository/PdpGroupRepository.java31
-rw-r--r--main/src/main/java/org/onap/policy/api/main/repository/PolicyRepository.java31
-rw-r--r--main/src/main/java/org/onap/policy/api/main/repository/PolicyTypeRepository.java31
-rw-r--r--main/src/main/java/org/onap/policy/api/main/repository/ToscaServiceTemplateRepository.java31
-rw-r--r--main/src/main/java/org/onap/policy/api/main/service/PdpGroupService.java92
-rw-r--r--main/src/main/java/org/onap/policy/api/main/service/PolicyService.java44
-rw-r--r--main/src/main/java/org/onap/policy/api/main/service/PolicyTypeService.java44
-rw-r--r--main/src/main/java/org/onap/policy/api/main/service/ToscaServiceTemplateService.java428
-rw-r--r--main/src/main/resources/application.yaml14
-rw-r--r--main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java488
-rw-r--r--main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyTypeProvider.java199
-rw-r--r--main/src/test/java/org/onap/policy/api/main/service/TestCommonToscaServiceTemplateService.java97
-rw-r--r--main/src/test/java/org/onap/policy/api/main/service/TestPdpGroupService.java84
-rw-r--r--main/src/test/java/org/onap/policy/api/main/service/TestPolicyService.java49
-rw-r--r--main/src/test/java/org/onap/policy/api/main/service/TestPolicyTypeService.java49
-rw-r--r--main/src/test/java/org/onap/policy/api/main/service/TestToscaServiceTemplateServiceForPolicyCrud.java437
-rw-r--r--main/src/test/java/org/onap/policy/api/main/service/TestToscaServiceTemplateServiceForPolicyTypeCrud.java213
-rw-r--r--main/src/test/resources/pdpgroups/PdpGroups.json125
20 files changed, 1808 insertions, 687 deletions
diff --git a/main/pom.xml b/main/pom.xml
index fffaa03f..963f6d4c 100644
--- a/main/pom.xml
+++ b/main/pom.xml
@@ -96,6 +96,11 @@
<version>${version.springboot.actuator}</version>
</dependency>
<dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-data-jpa</artifactId>
+ <version>${version.springboot}</version>
+ </dependency>
+ <dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>${version.io.micrometer}</version>
diff --git a/main/src/main/java/org/onap/policy/api/main/PolicyApiApplication.java b/main/src/main/java/org/onap/policy/api/main/PolicyApiApplication.java
index 25c4d937..d022372c 100644
--- a/main/src/main/java/org/onap/policy/api/main/PolicyApiApplication.java
+++ b/main/src/main/java/org/onap/policy/api/main/PolicyApiApplication.java
@@ -22,8 +22,11 @@ package org.onap.policy.api.main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.autoconfigure.domain.EntityScan;
@SpringBootApplication
+@EntityScan(
+ basePackages = {"org.onap.policy.models.pdp.persistence.concepts", "org.onap.policy.models.tosca.simple.concepts"})
public class PolicyApiApplication {
public static void main(String[] args) {
SpringApplication.run(PolicyApiApplication.class, args);
diff --git a/main/src/main/java/org/onap/policy/api/main/repository/PdpGroupRepository.java b/main/src/main/java/org/onap/policy/api/main/repository/PdpGroupRepository.java
new file mode 100644
index 00000000..3365442a
--- /dev/null
+++ b/main/src/main/java/org/onap/policy/api/main/repository/PdpGroupRepository.java
@@ -0,0 +1,31 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * 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.
+ * 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.repository;
+
+import org.onap.policy.models.base.PfConceptKey;
+import org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.stereotype.Repository;
+
+@Repository
+public interface PdpGroupRepository extends JpaRepository<JpaPdpGroup, PfConceptKey> {
+
+} \ No newline at end of file
diff --git a/main/src/main/java/org/onap/policy/api/main/repository/PolicyRepository.java b/main/src/main/java/org/onap/policy/api/main/repository/PolicyRepository.java
new file mode 100644
index 00000000..1d34dace
--- /dev/null
+++ b/main/src/main/java/org/onap/policy/api/main/repository/PolicyRepository.java
@@ -0,0 +1,31 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * 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.
+ * 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.repository;
+
+import org.onap.policy.models.base.PfConceptKey;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.stereotype.Repository;
+
+@Repository
+public interface PolicyRepository extends JpaRepository<JpaToscaPolicy, PfConceptKey> {
+
+} \ No newline at end of file
diff --git a/main/src/main/java/org/onap/policy/api/main/repository/PolicyTypeRepository.java b/main/src/main/java/org/onap/policy/api/main/repository/PolicyTypeRepository.java
new file mode 100644
index 00000000..a49e5ed9
--- /dev/null
+++ b/main/src/main/java/org/onap/policy/api/main/repository/PolicyTypeRepository.java
@@ -0,0 +1,31 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * 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.
+ * 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.repository;
+
+import org.onap.policy.models.base.PfConceptKey;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.stereotype.Repository;
+
+@Repository
+public interface PolicyTypeRepository extends JpaRepository<JpaToscaPolicyType, PfConceptKey> {
+
+} \ No newline at end of file
diff --git a/main/src/main/java/org/onap/policy/api/main/repository/ToscaServiceTemplateRepository.java b/main/src/main/java/org/onap/policy/api/main/repository/ToscaServiceTemplateRepository.java
new file mode 100644
index 00000000..281b7150
--- /dev/null
+++ b/main/src/main/java/org/onap/policy/api/main/repository/ToscaServiceTemplateRepository.java
@@ -0,0 +1,31 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * 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.
+ * 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.repository;
+
+import org.onap.policy.models.base.PfConceptKey;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.stereotype.Repository;
+
+@Repository
+public interface ToscaServiceTemplateRepository extends JpaRepository<JpaToscaServiceTemplate, PfConceptKey> {
+
+} \ No newline at end of file
diff --git a/main/src/main/java/org/onap/policy/api/main/service/PdpGroupService.java b/main/src/main/java/org/onap/policy/api/main/service/PdpGroupService.java
new file mode 100644
index 00000000..0bac8722
--- /dev/null
+++ b/main/src/main/java/org/onap/policy/api/main/service/PdpGroupService.java
@@ -0,0 +1,92 @@
+/*
+ * ============LICENSE_START=======================================================
+ * 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.
+ * 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.service;
+
+import java.util.List;
+import java.util.stream.Collectors;
+import javax.ws.rs.core.Response;
+import lombok.RequiredArgsConstructor;
+import org.onap.policy.api.main.repository.PdpGroupRepository;
+import org.onap.policy.models.base.PfModelRuntimeException;
+import org.onap.policy.models.pdp.concepts.PdpGroup;
+import org.onap.policy.models.pdp.concepts.PdpSubGroup;
+import org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+@Service
+@Transactional(readOnly = true)
+@RequiredArgsConstructor
+public class PdpGroupService {
+
+ private final PdpGroupRepository pdpGroupRepository;
+
+ /**
+ * Fetch all the PDP groups from the DB.
+ * @return a list of {@link PdpGroup}
+ */
+ private List<PdpGroup> getAllPdpGroups() {
+ return pdpGroupRepository.findAll().stream().map(JpaPdpGroup::toAuthorative).collect(Collectors.toList());
+ }
+
+ /**
+ * Assert that the policy type is not supported in any PDP group.
+ *
+ * @param policyTypeName the policy type name
+ * @param policyTypeVersion the policy type version
+ * @throws PfModelRuntimeException if the policy type is supported in a PDP group
+ */
+ public void assertPolicyTypeNotSupportedInPdpGroup(final String policyTypeName, final String policyTypeVersion)
+ throws PfModelRuntimeException {
+ final var policyTypeIdentifier = new ToscaConceptIdentifier(policyTypeName, policyTypeVersion);
+ for (PdpGroup pdpGroup : getAllPdpGroups()) {
+ for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
+ if (pdpSubGroup.getSupportedPolicyTypes().contains(policyTypeIdentifier)) {
+ throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
+ "policy type is in use, it is referenced in PDP group " + pdpGroup.getName() + " subgroup "
+ + pdpSubGroup.getPdpType());
+ }
+ }
+ }
+ }
+
+ /**
+ * Assert that the policy is not deployed in a PDP group.
+ *
+ * @param policyName the policy name
+ * @param policyVersion the policy version
+ * @throws PfModelRuntimeException thrown if the policy is deployed in a PDP group
+ */
+ public void assertPolicyNotDeployedInPdpGroup(final String policyName, final String policyVersion)
+ throws PfModelRuntimeException {
+ final var policyIdentifier = new ToscaConceptIdentifier(policyName, policyVersion);
+ for (PdpGroup pdpGroup : getAllPdpGroups()) {
+ for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
+ if (pdpSubGroup.getPolicies().contains(policyIdentifier)) {
+ throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
+ "policy is in use, it is deployed in PDP group " + pdpGroup.getName() + " subgroup "
+ + pdpSubGroup.getPdpType());
+ }
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/main/src/main/java/org/onap/policy/api/main/service/PolicyService.java b/main/src/main/java/org/onap/policy/api/main/service/PolicyService.java
new file mode 100644
index 00000000..d31df024
--- /dev/null
+++ b/main/src/main/java/org/onap/policy/api/main/service/PolicyService.java
@@ -0,0 +1,44 @@
+/*
+ * ============LICENSE_START=======================================================
+ * 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.
+ * 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.service;
+
+import lombok.RequiredArgsConstructor;
+import org.onap.policy.api.main.repository.PolicyRepository;
+import org.onap.policy.models.base.PfConceptKey;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+@Service
+@Transactional
+@RequiredArgsConstructor
+public class PolicyService {
+
+ private final PolicyRepository policyRepository;
+
+ /**
+ * Delete the specified policy.
+ *
+ * @param policyKey the policy key containing name and version
+ */
+ public void deletePolicy(final PfConceptKey policyKey) {
+ policyRepository.deleteById(policyKey);
+ }
+} \ No newline at end of file
diff --git a/main/src/main/java/org/onap/policy/api/main/service/PolicyTypeService.java b/main/src/main/java/org/onap/policy/api/main/service/PolicyTypeService.java
new file mode 100644
index 00000000..39244cc3
--- /dev/null
+++ b/main/src/main/java/org/onap/policy/api/main/service/PolicyTypeService.java
@@ -0,0 +1,44 @@
+/*
+ * ============LICENSE_START=======================================================
+ * 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.
+ * 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.service;
+
+import lombok.RequiredArgsConstructor;
+import org.onap.policy.api.main.repository.PolicyTypeRepository;
+import org.onap.policy.models.base.PfConceptKey;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+@Service
+@Transactional
+@RequiredArgsConstructor
+public class PolicyTypeService {
+
+ private final PolicyTypeRepository policyTypeRepository;
+
+ /**
+ * Delete the specified policyType.
+ *
+ * @param policyTypeKey the policy type key containing name and version
+ */
+ public void deletePolicyType(final PfConceptKey policyTypeKey) {
+ policyTypeRepository.deleteById(policyTypeKey);
+ }
+} \ No newline at end of file
diff --git a/main/src/main/java/org/onap/policy/api/main/service/ToscaServiceTemplateService.java b/main/src/main/java/org/onap/policy/api/main/service/ToscaServiceTemplateService.java
new file mode 100644
index 00000000..ba61dd41
--- /dev/null
+++ b/main/src/main/java/org/onap/policy/api/main/service/ToscaServiceTemplateService.java
@@ -0,0 +1,428 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * 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.
+ * 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.service;
+
+import java.util.List;
+import javax.ws.rs.core.Response;
+import lombok.NonNull;
+import lombok.RequiredArgsConstructor;
+import org.apache.commons.collections4.CollectionUtils;
+import org.onap.policy.api.main.repository.ToscaServiceTemplateRepository;
+import org.onap.policy.api.main.rest.PolicyFetchMode;
+import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.models.base.PfConceptKey;
+import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.base.PfModelRuntimeException;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityFilter;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
+import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
+import org.onap.policy.models.tosca.utils.ToscaServiceTemplateUtils;
+import org.onap.policy.models.tosca.utils.ToscaUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+@Service
+@Transactional
+@RequiredArgsConstructor
+public class ToscaServiceTemplateService {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(ToscaServiceTemplateService.class);
+
+ // Recurring string constants
+ private static final String POLICY_TYPE = "policy type ";
+ private static final String NOT_FOUND = " not found";
+ public static final String SERVICE_TEMPLATE_NOT_FOUND_MSG = "service template not found in database";
+ public static final String DO_NOT_EXIST_MSG = " do not exist";
+
+ private final ToscaServiceTemplateRepository toscaServiceTemplateRepository;
+ private final PdpGroupService pdpGroupService;
+ private final PolicyTypeService policyTypeService;
+ private final PolicyService policyService;
+
+ /**
+ * Retrieves a list of policy types matching specified policy type name and version.
+ *
+ * @param policyTypeName the name of policy type
+ * @param policyTypeVersion the version of policy type
+ * @return the ToscaServiceTemplate object
+ */
+ public ToscaServiceTemplate fetchPolicyTypes(final String policyTypeName, final String policyTypeVersion)
+ throws PfModelException {
+ return getFilteredPolicyTypes(policyTypeName, policyTypeVersion);
+ }
+
+ /**
+ * Retrieves a list of policy types with the latest versions.
+ *
+ * @param policyTypeName the name of policy type
+ * @return the ToscaServiceTemplate object
+ */
+ public ToscaServiceTemplate fetchLatestPolicyTypes(final String policyTypeName) throws PfModelException {
+ return getFilteredPolicyTypes(policyTypeName, ToscaEntityFilter.LATEST_VERSION);
+ }
+
+ /**
+ * Creates a new policy type.
+ *
+ * @param body the entity body of policy type
+ * @return the TOSCA service template containing the created policy types
+ * @throws PfModelRuntimeException on errors creating policy types
+ */
+ public ToscaServiceTemplate createPolicyType(@NonNull final ToscaServiceTemplate body)
+ throws PfModelRuntimeException {
+ final var incomingServiceTemplate = new JpaToscaServiceTemplate(body);
+ LOGGER.debug("->createPolicyType: serviceTemplate={}", incomingServiceTemplate);
+
+ // assert incoming body contains policyTypes
+ ToscaUtils.assertPolicyTypesExist(incomingServiceTemplate);
+
+ // append the incoming fragment to the DB TOSCA service template
+ final var serviceTemplateToWrite =
+ ToscaServiceTemplateUtils.addFragment(getDefaultJpaToscaServiceTemplate(), incomingServiceTemplate);
+
+ final var result = serviceTemplateToWrite.validate("service template");
+ if (!result.isValid()) {
+ throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, result.getResult());
+ } else {
+ toscaServiceTemplateRepository.save(serviceTemplateToWrite);
+ LOGGER.debug("<-createPolicyType: writtenServiceTemplate={}", serviceTemplateToWrite);
+ }
+ return body;
+ }
+
+ /**
+ * Delete the policy type matching specified policy type name and version.
+ *
+ * @param policyTypeName the name of policy type
+ * @param policyTypeVersion the version of policy type, if the version of the key is null,
+ * all versions of the policy type are deleted.
+ * @return the TOSCA service template containing the policy types that were deleted
+ * @throws PfModelRuntimeException on errors deleting policy types
+ */
+ public ToscaServiceTemplate deletePolicyType(final String policyTypeName, final String policyTypeVersion)
+ throws PfModelRuntimeException {
+ final var policyTypeKey = new PfConceptKey(policyTypeName, policyTypeVersion);
+ LOGGER.debug("->deletePolicyType: name={}, version={}", policyTypeName, policyTypeVersion);
+
+ // terminate deletion if supported in a PdpGroup
+ pdpGroupService.assertPolicyTypeNotSupportedInPdpGroup(policyTypeName, policyTypeVersion);
+
+ final var serviceTemplate = getDefaultJpaToscaServiceTemplate();
+
+ // terminate deletion if not found
+ if (!ToscaUtils.doPolicyTypesExist(serviceTemplate)) {
+ throw new PfModelRuntimeException(Response.Status.NOT_FOUND, "no policy types found");
+ }
+
+ final var policyTypeForDeletion = serviceTemplate.getPolicyTypes().get(policyTypeKey);
+ if (policyTypeForDeletion == null) {
+ throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
+ POLICY_TYPE + policyTypeKey.getId() + NOT_FOUND);
+ }
+
+ final var result = new BeanValidationResult("policy types", serviceTemplate);
+
+ for (final var policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
+ final var ancestorList = ToscaUtils
+ .getEntityTypeAncestors(serviceTemplate.getPolicyTypes(), policyType, result);
+ // terminate deletion if referenced by another via derived_from property
+ if (ancestorList.contains(policyTypeForDeletion)) {
+ throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, POLICY_TYPE + policyTypeKey.getId()
+ + " is in use, it is referenced in policy type " + policyType.getId());
+ }
+ }
+ if (ToscaUtils.doPoliciesExist(serviceTemplate)) {
+ for (final var policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
+ // terminate deletion if referenced by a policy
+ if (policyTypeKey.equals(policy.getType())) {
+ throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, POLICY_TYPE
+ + policyTypeKey.getId() + " is in use, it is referenced in policy " + policy.getId());
+ }
+ }
+ }
+
+ // remove policyType from service template and write to DB
+ serviceTemplate.getPolicyTypes().getConceptMap().remove(policyTypeKey);
+ toscaServiceTemplateRepository.save(serviceTemplate);
+
+ // remove the entry from the Policy table
+ policyTypeService.deletePolicyType(policyTypeKey);
+
+ // prepare return service template object
+ var deletedServiceTemplate = new JpaToscaServiceTemplate();
+ deletedServiceTemplate.setPolicyTypes(new JpaToscaPolicyTypes());
+ deletedServiceTemplate.getPolicyTypes().getConceptMap().put(policyTypeKey, policyTypeForDeletion);
+
+ LOGGER.debug("<-deletePolicyType: key={}, serviceTemplate={}", policyTypeKey, deletedServiceTemplate);
+ return deletedServiceTemplate.toAuthorative();
+ }
+
+ /**
+ * Retrieves a list of policies matching specified name and version of both policy type and policy.
+ *
+ * @param policyTypeName the name of policy type
+ * @param policyTypeVersion the version of policy type
+ * @param policyName the name of policy
+ * @param policyVersion the version of policy
+ * @param mode the fetch mode for policies
+ * @return the ToscaServiceTemplate object with the policies found
+ * @throws PfModelException on errors getting the policy
+ */
+ public ToscaServiceTemplate fetchPolicies(final String policyTypeName, final String policyTypeVersion,
+ final String policyName, final String policyVersion, final PolicyFetchMode mode) throws PfModelException {
+ return getFilteredPolicies(policyTypeName, policyTypeVersion, policyName, policyVersion, mode);
+ }
+
+ /**
+ * Retrieves a list of policies with the latest versions that match specified policy type id and version.
+ *
+ * @param policyTypeName the name of policy type
+ * @param policyTypeVersion the version of policy type
+ * @param policyName the name of the policy
+ * @param mode the fetch mode for policies
+ * @return the ToscaServiceTemplate object with the policies found
+ * @throws PfModelException on errors getting the policy
+ */
+ public ToscaServiceTemplate fetchLatestPolicies(final String policyTypeName, final String policyTypeVersion,
+ final String policyName, final PolicyFetchMode mode) throws PfModelException {
+ return getFilteredPolicies(policyTypeName, policyTypeVersion, policyName, ToscaTypedEntityFilter.LATEST_VERSION,
+ mode);
+ }
+
+ /**
+ * Creates one or more new policies for the same policy type name and version.
+ *
+ * @param policyTypeName the name of policy type
+ * @param policyTypeVersion the version of policy type
+ * @param body the entity body of polic(ies)
+ * @return the ToscaServiceTemplate object containing the policy types that were created
+ * @throws PfModelRuntimeException on errors creating the policy
+ */
+ public ToscaServiceTemplate createPolicy(final String policyTypeName, final String policyTypeVersion,
+ final ToscaServiceTemplate body) throws PfModelRuntimeException {
+ return createPolicies(body);
+ }
+
+ /**
+ * Creates one or more new policies.
+ *
+ * @param body the entity body of policy
+ * @return the ToscaServiceTemplate object containing the policy types that were created
+ * @throws PfModelRuntimeException on errors creating the policy
+ */
+ public ToscaServiceTemplate createPolicies(final ToscaServiceTemplate body) throws PfModelRuntimeException {
+ final var incomingServiceTemplate = new JpaToscaServiceTemplate(body);
+
+ // assert incoming body contains policies
+ ToscaUtils.assertPoliciesExist(incomingServiceTemplate);
+
+ // append the incoming fragment to the DB TOSCA service template
+ final var serviceTemplateToWrite =
+ ToscaServiceTemplateUtils.addFragment(getDefaultJpaToscaServiceTemplate(), incomingServiceTemplate);
+
+ final var result = serviceTemplateToWrite.validate("Policies CRUD service template.");
+ if (!result.isValid()) {
+ throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, result.getResult());
+ }
+
+ toscaServiceTemplateRepository.save(serviceTemplateToWrite);
+
+ LOGGER.debug("<-appendServiceTemplateFragment: returnServiceTempalate={}", serviceTemplateToWrite);
+ return body;
+ }
+
+ /**
+ * Deletes the policy matching specified name and version of both policy type and policy.
+ *
+ * @param policyTypeName the name of policy type
+ * @param policyTypeVersion the version of policy type
+ * @param policyName the name of policy
+ * @param policyVersion the version of policy
+ * @return the ToscaServiceTemplate object containing the policies that were deleted
+ * @throws PfModelRuntimeException on errors deleting the policy
+ */
+ public ToscaServiceTemplate deletePolicy(final String policyTypeName, final String policyTypeVersion,
+ final String policyName, final String policyVersion) throws PfModelRuntimeException {
+ final var policyKey = new PfConceptKey(policyName, policyVersion);
+ LOGGER.debug("->deletePolicy: name={}, version={}", policyName, policyVersion);
+
+ // terminate if deployed in a PdpGroup
+ pdpGroupService.assertPolicyNotDeployedInPdpGroup(policyName, policyVersion);
+
+ final var serviceTemplate = getDefaultJpaToscaServiceTemplate();
+
+ // terminate deletion if not found
+ if (!ToscaUtils.doPoliciesExist(serviceTemplate)) {
+ throw new PfModelRuntimeException(Response.Status.NOT_FOUND, "no policies found");
+ }
+
+ final var policyForDeletion = serviceTemplate.getTopologyTemplate().getPolicies().get(policyKey);
+ if (policyForDeletion == null) {
+ throw new PfModelRuntimeException(Response.Status.NOT_FOUND, "policy " + policyKey.getId() + NOT_FOUND);
+ }
+
+ // remove policy from service template and write to DB
+ serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().remove(policyKey);
+ toscaServiceTemplateRepository.save(serviceTemplate);
+
+ // remove the entry from the Policy table
+ policyService.deletePolicy(policyKey);
+
+ // prepare return service template object
+ var deletedServiceTemplate = new JpaToscaServiceTemplate();
+ deletedServiceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
+ deletedServiceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
+ deletedServiceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, policyForDeletion);
+
+ LOGGER.debug("<-deletePolicy: key={}, serviceTemplate={}", policyKey, deletedServiceTemplate);
+ return deletedServiceTemplate.toAuthorative();
+ }
+
+ /**
+ * Retrieves TOSCA service template with the specified version of the policy type.
+ *
+ * @param policyTypeName the name of the policy type
+ * @param policyTypeVersion the version of the policy type
+ * @return the TOSCA service template containing the specified version of the policy type
+ * @throws PfModelException on errors getting the policy type
+ */
+ private ToscaServiceTemplate getFilteredPolicyTypes(final String policyTypeName, final String policyTypeVersion)
+ throws PfModelException {
+ final var dbServiceTemplate = getDefaultJpaToscaServiceTemplate();
+ final var policyTypeFilter =
+ ToscaEntityFilter.<ToscaPolicyType>builder().name(policyTypeName).version(policyTypeVersion).build();
+ LOGGER.debug("->getFilteredPolicyTypes: filter={}, serviceTemplate={}", policyTypeFilter, dbServiceTemplate);
+
+ // validate that policyTypes exist in db
+ if (!ToscaUtils.doPolicyTypesExist(dbServiceTemplate)) {
+ throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
+ "policy types for filter " + policyTypeFilter + DO_NOT_EXIST_MSG);
+ }
+
+ // fetch all polices and filter by policyType, policy name and version
+ final var serviceTemplate = new SimpleToscaProvider()
+ .getCascadedPolicyTypes(dbServiceTemplate, policyTypeName, policyTypeVersion);
+ var simpleToscaProvider = new SimpleToscaProvider();
+
+ List<ToscaPolicyType> filteredPolicyTypes = serviceTemplate.getPolicyTypes().toAuthorativeList();
+ filteredPolicyTypes = policyTypeFilter.filter(filteredPolicyTypes);
+
+ // validate that filtered policyTypes exist
+ if (CollectionUtils.isEmpty(filteredPolicyTypes)) {
+ throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
+ "policy types for filter " + policyTypeFilter + DO_NOT_EXIST_MSG);
+ }
+
+ // prepare return service template object
+ var returnServiceTemplate = new JpaToscaServiceTemplate();
+ for (var policyType : filteredPolicyTypes) {
+ final var cascadedServiceTemplate = simpleToscaProvider
+ .getCascadedPolicyTypes(dbServiceTemplate, policyType.getName(), policyType.getVersion());
+ returnServiceTemplate =
+ ToscaServiceTemplateUtils.addFragment(returnServiceTemplate, cascadedServiceTemplate);
+ }
+
+ LOGGER.debug("<-getFilteredPolicyTypes: filter={}, serviceTemplate={}", policyTypeFilter,
+ returnServiceTemplate);
+ return returnServiceTemplate.toAuthorative();
+
+ }
+
+ /**
+ * Retrieves TOSCA service template with the specified version of the policy.
+ *
+ * @param policyName the name of the policy
+ * @param policyVersion the version of the policy
+ * @param mode the fetch mode for policies
+ * @return the TOSCA service template containing the specified version of the policy
+ * @throws PfModelException on errors getting the policy
+ */
+ private ToscaServiceTemplate getFilteredPolicies(final String policyTypeName, final String policyTypeVersion,
+ final String policyName, final String policyVersion, final PolicyFetchMode mode) throws PfModelException {
+ final var policyFilter = ToscaTypedEntityFilter.<ToscaPolicy>builder()
+ .name(policyName).version(policyVersion).type(policyTypeName).typeVersion(policyTypeVersion).build();
+ final var dbServiceTemplate = getDefaultJpaToscaServiceTemplate();
+ LOGGER.debug("<-getFilteredPolicies: filter={}, serviceTemplate={}", policyFilter, dbServiceTemplate);
+
+ // validate that policies exist in db
+ if (!ToscaUtils.doPolicyTypesExist(dbServiceTemplate)) {
+ throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
+ "policies for filter " + policyFilter + DO_NOT_EXIST_MSG);
+ }
+
+ final var version =
+ ToscaTypedEntityFilter.LATEST_VERSION.equals(policyFilter.getVersion()) ? null : policyFilter.getVersion();
+
+ // fetch all polices and filter by policyType, policy name and version
+ final var simpleToscaProvider = new SimpleToscaProvider();
+ final var serviceTemplate =
+ simpleToscaProvider.getCascadedPolicies(dbServiceTemplate, policyFilter.getName(), version);
+
+ var filteredPolicies = serviceTemplate.getTopologyTemplate()
+ .getPolicies().toAuthorativeList();
+ filteredPolicies = policyFilter.filter(filteredPolicies);
+
+ // validate that filtered policies exist
+ if (CollectionUtils.isEmpty(filteredPolicies)) {
+ throw new PfModelRuntimeException(Response.Status.NOT_FOUND,
+ "policies for filter " + policyFilter + DO_NOT_EXIST_MSG);
+ }
+
+ // prepare return service template object
+ var returnServiceTemplate = new JpaToscaServiceTemplate();
+ for (var policy : filteredPolicies) {
+ final var cascadedServiceTemplate = simpleToscaProvider
+ .getCascadedPolicies(dbServiceTemplate, policy.getName(), policy.getVersion());
+ returnServiceTemplate =
+ ToscaServiceTemplateUtils.addFragment(returnServiceTemplate, cascadedServiceTemplate);
+ }
+
+ if (mode == null || PolicyFetchMode.BARE.equals(mode)) {
+ returnServiceTemplate.setPolicyTypes(null);
+ returnServiceTemplate.setDataTypes(null);
+ }
+ LOGGER.debug("<-getFilteredPolicies: filter={}, , serviceTemplate={}", policyFilter, returnServiceTemplate);
+ return returnServiceTemplate.toAuthorative();
+ }
+
+ /**
+ * Get Service Template.
+ * @return the Service Template read from the database
+ */
+ private JpaToscaServiceTemplate getDefaultJpaToscaServiceTemplate() {
+ final var defaultServiceTemplateOpt = toscaServiceTemplateRepository
+ .findById(new PfConceptKey(JpaToscaServiceTemplate.DEFAULT_NAME, JpaToscaServiceTemplate.DEFAULT_VERSION));
+ if (defaultServiceTemplateOpt.isEmpty()) {
+ throw new PfModelRuntimeException(Response.Status.NOT_FOUND, SERVICE_TEMPLATE_NOT_FOUND_MSG);
+ }
+ LOGGER.debug("<-getDefaultJpaToscaServiceTemplate: serviceTemplate={}", defaultServiceTemplateOpt.get());
+ return defaultServiceTemplateOpt.get();
+ }
+} \ No newline at end of file
diff --git a/main/src/main/resources/application.yaml b/main/src/main/resources/application.yaml
index 96b79823..f46810e4 100644
--- a/main/src/main/resources/application.yaml
+++ b/main/src/main/resources/application.yaml
@@ -6,6 +6,20 @@ spring:
name: policyadmin
password: zb!XztG34
mvc.converters.preferred-json-mapper: gson
+ datasource:
+ url: jdbc:mariadb://mariadb:3306/policyadmin
+ driverClassName: org.mariadb.jdbc.Driver
+ username: policy_user
+ password: policy_user
+ jpa:
+ properties:
+ hibernate:
+ dialect: org.hibernate.dialect.MariaDB103Dialect
+ hibernate:
+ ddl-auto: none
+ naming:
+ physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
+ implicit-strategy: org.onap.policy.common.spring.utils.CustomImplicitNamingStrategy
policy-api:
name: ApiGroup
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
deleted file mode 100644
index ad394e33..00000000
--- a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java
+++ /dev/null
@@ -1,488 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * ONAP Policy API
- * ================================================================================
- * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2019-2021 Nordix Foundation.
- * 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * SPDX-License-Identifier: Apache-2.0
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.api.main.rest.provider;
-
-import static org.assertj.core.api.Assertions.assertThat;
-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.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.fail;
-
-import java.util.ArrayList;
-import java.util.List;
-import org.junit.Ignore;
-import org.junit.Test;
-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;
-import org.onap.policy.common.utils.resources.ResourceUtils;
-import org.onap.policy.models.base.PfModelException;
-import org.onap.policy.models.pdp.concepts.Pdp;
-import org.onap.policy.models.pdp.concepts.PdpGroup;
-import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
-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.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 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";
- private static final String POLICY_RESOURCE_WITH_BAD_POLICYTYPE_ID = "policies/vCPE.policy.bad.policytypeid.json";
- private static final String POLICY_RESOURCE_WITH_BAD_POLICYTYPE_VERSION =
- "policies/vCPE.policy.bad.policytypeversion.json";
- private static final String POLICY_RESOURCE_WITH_NO_POLICY_VERSION = "policies/vCPE.policy.no.policyversion.json";
- private static final String POLICY_RESOURCE_WITH_DIFFERENT_FIELDS =
- "policies/vCPE.policy.different.policy.fields.json";
- private static final String MULTIPLE_POLICIES_RESOURCE = "policies/vCPE.policies.optimization.input.tosca.json";
-
- private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON =
- "policytypes/onap.policies.controlloop.operational.Common.yaml";
- private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_DROOLS =
- "policytypes/onap.policies.controlloop.operational.common.Drools.yaml";
- 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";
-
- @Autowired
- private PolicyProvider policyProvider;
- @Autowired
- private PolicyTypeProvider policyTypeProvider;
- @Autowired
- private PolicyModelsProvider databaseProvider;
-
- @Test
- public void testFetchPolicies() {
-
- assertThatThrownBy(() -> {
- policyProvider.fetchPolicies("dummy", "1.0.0", null, null, null);
- }).hasMessage("service template not found in database");
-
- assertThatThrownBy(() -> {
- policyProvider.fetchPolicies("dummy", "1.0.0", "dummy", null, null);
- }).hasMessage("service template not found in database");
-
- assertThatThrownBy(() -> {
- policyProvider.fetchPolicies("dummy", "1.0.0", "dummy", "1.0.0", null);
- }).hasMessage("service template not found in database");
-
- assertThatThrownBy(() -> {
- policyProvider.fetchPolicies(null, null, "dummy", "1.0.0", null);
- }).hasMessage("service template not found in database");
- }
-
- @Test
- public void testFetchLatestPolicies() {
-
- assertThatThrownBy(() -> {
- policyProvider.fetchLatestPolicies("dummy", "dummy", "dummy", null);
- }).hasMessage("service template not found in database");
- }
-
- @Test
- public void testFetchDeployedPolicies() {
- String policyId = "onap.restart.tca";
- String policyVersion = "1.0.0";
- String policyTypeVersion = "1.0.0";
- String policyTypeId = "onap.policies.monitoring.cdap.tca.hi.lo.app";
-
- try {
- assertEquals(0, databaseProvider.getPdpGroups("name").size());
- assertEquals(0, databaseProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).size());
-
- assertNotNull(databaseProvider.createPdpGroups(new ArrayList<>()));
- assertNotNull(databaseProvider.updatePdpGroups(new ArrayList<>()));
-
- PdpGroup pdpGroup = new PdpGroup();
- pdpGroup.setName("group");
- pdpGroup.setVersion("1.2.3");
- pdpGroup.setPdpGroupState(PdpState.ACTIVE);
- pdpGroup.setPdpSubgroups(new ArrayList<>());
- List<PdpGroup> groupList = new ArrayList<>();
- groupList.add(pdpGroup);
-
- PdpSubGroup pdpSubGroup = new PdpSubGroup();
- pdpSubGroup.setPdpType("type");
- pdpSubGroup.setDesiredInstanceCount(123);
- pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>());
- pdpSubGroup.getSupportedPolicyTypes().add(new ToscaConceptIdentifier(policyTypeId, policyTypeVersion));
- pdpGroup.getPdpSubgroups().add(pdpSubGroup);
-
- Pdp pdp = new Pdp();
- pdp.setInstanceId("type-0");
- pdp.setMessage("Hello");
- pdp.setPdpState(PdpState.ACTIVE);
- pdp.setHealthy(PdpHealthStatus.UNKNOWN);
- pdpSubGroup.setPdpInstances(new ArrayList<>());
- pdpSubGroup.getPdpInstances().add(pdp);
-
- // Create Pdp Groups
- assertEquals(123, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0)
- .getDesiredInstanceCount());
- assertEquals(1, databaseProvider.getPdpGroups("group").size());
-
- // Create Policy Type
- assertThatCode(() -> {
- ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
- .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
- policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
- }).doesNotThrowAnyException();
-
- // Create Policy
- assertThatCode(() -> {
- String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
- ToscaServiceTemplate policyServiceTemplate =
- standardCoder.decode(policyString, ToscaServiceTemplate.class);
- ToscaServiceTemplate serviceTemplate =
- policyProvider.createPolicy(policyTypeId, policyTypeVersion, policyServiceTemplate);
- assertFalse(serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).isEmpty());
- }).doesNotThrowAnyException();
-
- // Update pdpSubGroup
- pdpSubGroup.setPolicies(new ArrayList<>());
- pdpSubGroup.getPolicies().add(new ToscaConceptIdentifier(policyId, policyVersion));
- assertEquals(1,
- databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0).getPolicies().size());
-
- // Test validateDeleteEligibility exception path(!pdpGroups.isEmpty())
- assertThatThrownBy(() -> {
- policyProvider.deletePolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", "onap.restart.tca",
- "1.0.0");
- }).hasMessageContaining("policy is in use, it is deployed in PDP group group subgroup type");
- } catch (Exception exc) {
- fail("Test should not throw an exception");
- }
- }
-
- @Test
- public void testCreatePolicy() throws Exception {
-
- assertThatThrownBy(() -> {
- policyProvider.createPolicy("dummy", "1.0.0", new ToscaServiceTemplate());
- }).hasMessage("topology template not specified on service template");
-
- ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
- .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
- policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
-
- assertThatCode(() -> policyTypeProvider.createPolicyType(policyTypeServiceTemplate)).doesNotThrowAnyException();
-
- assertThatThrownBy(() -> {
- String badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_BAD_POLICYTYPE_ID);
- ToscaServiceTemplate badPolicyServiceTemplate =
- standardCoder.decode(badPolicyString, ToscaServiceTemplate.class);
- policyProvider.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0",
- badPolicyServiceTemplate);
- }).hasMessage(
- "Version not specified, the version of this TOSCA entity must be specified in "
- + "the type_version field");
-
- assertThatThrownBy(() -> {
- String badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_BAD_POLICYTYPE_VERSION);
- ToscaServiceTemplate badPolicyServiceTemplate =
- standardCoder.decode(badPolicyString, ToscaServiceTemplate.class);
- policyProvider.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0",
- badPolicyServiceTemplate);
- }).hasMessageContaining(
- "item \"policy type\" value \"onap.policies.monitoring.cdap.tca.hi.lo.app:2.0.0\" INVALID, not found");
-
- assertThatThrownBy(() -> {
- String badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_NO_POLICY_VERSION);
- ToscaServiceTemplate badPolicyServiceTemplate =
- standardCoder.decode(badPolicyString, ToscaServiceTemplate.class);
- policyProvider.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0",
- badPolicyServiceTemplate);
- }).hasMessageContaining("item \"version\" value \"0.0.0\" INVALID, is null");
-
- String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
- ToscaServiceTemplate policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class);
- ToscaServiceTemplate serviceTemplate = policyProvider
- .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate);
- assertFalse(serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).isEmpty());
-
- assertThatThrownBy(() -> {
- String badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_DIFFERENT_FIELDS);
- ToscaServiceTemplate badPolicyServiceTemplate =
- standardCoder.decode(badPolicyString, ToscaServiceTemplate.class);
- policyProvider.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0",
- badPolicyServiceTemplate);
- }).hasMessageContaining(
- "item \"entity\" value \"onap.restart.tca:1.0.0\" INVALID, " + "does not equal existing entity");
- }
-
- @Test
- public void testCreateOperationalDroolsPolicy() throws CoderException, PfModelException {
- ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode(
- ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON), ToscaServiceTemplate.class);
-
- policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
-
- policyTypeServiceTemplate = standardYamlCoder.decode(
- ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_DROOLS), ToscaServiceTemplate.class);
- policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
-
- String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_OPERATIONAL);
- ToscaServiceTemplate policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class);
- ToscaServiceTemplate serviceTemplate =
- policyProvider.createPolicy(POLICY_TYPE_OPERATIONAL_DROOLS, "1.0.0", policyServiceTemplate);
- assertFalse(serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).isEmpty());
- }
-
- @Test
- public void testSimpleCreatePolicy() throws Exception {
-
- assertThatThrownBy(() -> {
- String multiPoliciesString = ResourceUtils.getResourceAsString(MULTIPLE_POLICIES_RESOURCE);
- ToscaServiceTemplate multiPoliciesServiceTemplate =
- standardCoder.decode(multiPoliciesString, ToscaServiceTemplate.class);
- policyProvider.createPolicies(multiPoliciesServiceTemplate);
- }).hasMessageContaining(
- "no policy types are defined on the service template for the policies in the topology template");
-
- // Create required policy types
- ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode(
- ResourceUtils.getResourceAsString("policytypes/onap.policies.Optimization.yaml"),
- ToscaServiceTemplate.class);
- policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
-
- policyTypeServiceTemplate = standardYamlCoder.decode(
- ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.Resource.yaml"),
- ToscaServiceTemplate.class);
- policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
-
- policyTypeServiceTemplate = standardYamlCoder.decode(
- ResourceUtils
- .getResourceAsString("policytypes/onap.policies.optimization.resource.AffinityPolicy.yaml"),
- ToscaServiceTemplate.class);
- policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
-
- policyTypeServiceTemplate = standardYamlCoder.decode(
- ResourceUtils
- .getResourceAsString("policytypes/onap.policies.optimization.resource.DistancePolicy.yaml"),
- ToscaServiceTemplate.class);
- policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
-
- policyTypeServiceTemplate = standardYamlCoder.decode(
- ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.resource.Vim_fit.yaml"),
- ToscaServiceTemplate.class);
- policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
-
- policyTypeServiceTemplate = standardYamlCoder.decode(
- ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.resource.HpaPolicy.yaml"),
- ToscaServiceTemplate.class);
- policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
-
- policyTypeServiceTemplate = standardYamlCoder.decode(
- ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.resource.VnfPolicy.yaml"),
- ToscaServiceTemplate.class);
- policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
-
- policyTypeServiceTemplate = standardYamlCoder.decode(
- ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.Service.yaml"),
- ToscaServiceTemplate.class);
- policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
-
- policyTypeServiceTemplate = standardYamlCoder.decode(
- ResourceUtils
- .getResourceAsString("policytypes/onap.policies.optimization.service.SubscriberPolicy.yaml"),
- ToscaServiceTemplate.class);
- policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
-
- policyTypeServiceTemplate = standardYamlCoder.decode(
- ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.service.QueryPolicy.yaml"),
- ToscaServiceTemplate.class);
- policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
-
- policyTypeServiceTemplate = standardYamlCoder.decode(
- ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.tcagen2.yaml"),
- ToscaServiceTemplate.class);
- policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
-
- // Create multiple policies in one call
- String multiPoliciesString = ResourceUtils.getResourceAsString(MULTIPLE_POLICIES_RESOURCE);
- ToscaServiceTemplate multiPoliciesServiceTemplate =
- standardCoder.decode(multiPoliciesString, ToscaServiceTemplate.class);
-
- assertThatCode(() -> {
- policyProvider.createPolicies(multiPoliciesServiceTemplate);
- policyProvider.createPolicies(multiPoliciesServiceTemplate);
- }).doesNotThrowAnyException();
- }
-
- @Test
- public void testDeletePolicy() {
-
- assertThatThrownBy(() -> {
- policyProvider.deletePolicy("dummy", "1.0.0", "dummy", "1.0.0");
- }).hasMessage("service template not found in database");
-
- assertThatCode(() -> {
- ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
- .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
- policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
- }).doesNotThrowAnyException();
-
- assertThatCode(() -> {
- String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
- ToscaServiceTemplate policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class);
- ToscaServiceTemplate serviceTemplate = policyProvider
- .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate);
- assertFalse(serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).isEmpty());
- }).doesNotThrowAnyException();
-
- assertThatCode(() -> {
- ToscaServiceTemplate serviceTemplate = policyProvider
- .deletePolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", "onap.restart.tca", "1.0.0");
- assertFalse(serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).isEmpty());
- }).doesNotThrowAnyException();
-
- assertThatThrownBy(() -> {
- policyProvider.deletePolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", "onap.restart.tca",
- "1.0.0");
- }).hasMessageContaining("no policies found");
- }
-
- @Test
- public void testFetchAllPolicies() throws Exception {
- // Create Policy Type
- ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
- .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
- policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
-
- // Create Policy
- String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
- ToscaServiceTemplate policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class);
- ToscaServiceTemplate serviceTemplate = policyProvider
- .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate);
-
- assertThat(serviceTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1);
-
- // Test fetch all policies
- policyTypeServiceTemplate = policyProvider.fetchPolicies(null, null, null, null, null);
-
- assertThat(policyTypeServiceTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1);
- }
-
- @Test
- public void testFetchSpecificPolicy_availablePolicy() throws Exception {
- // Create Policy Type
- ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
- .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
- policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
-
- // Create Policy
- String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
- ToscaServiceTemplate policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class);
- ToscaServiceTemplate serviceTemplate = policyProvider
- .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate);
-
- assertThat(serviceTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1);
-
- // Test fetch specific policy
- assertThat(policyProvider.fetchPolicies(null, null, "onap.restart.tca", "1.0.0", null)
- .getToscaTopologyTemplate().getPolicies()).hasSize(1);
- }
-
- @Test
- public void testFetchSpecificPolicy_unavailablePolicy() throws Exception {
- // Create Policy Type
- ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
- .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
- policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
-
- // Create Policy
- String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
- ToscaServiceTemplate policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class);
- ToscaServiceTemplate serviceTemplate = policyProvider
- .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate);
- assertNotNull(serviceTemplate.getToscaTopologyTemplate().getPolicies());
- assertThat(serviceTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1);
-
- // Test fetch specific policy
- assertThatThrownBy(() -> policyProvider.fetchPolicies(null, null, "onap.restart.tca", "2.0.0", null))
- .hasMessageContaining("policies for onap.restart.tca:2.0.0 do not exist");
- }
-
- @Test
- public void testDeleteSpecificPolicy_availablePolicy() throws Exception {
- ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
- .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
- policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
-
- String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
- ToscaServiceTemplate policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class);
- ToscaServiceTemplate serviceTemplate = policyProvider
- .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate);
- assertThat(serviceTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1);
-
- ToscaServiceTemplate svcTemplate = policyProvider.deletePolicy(null, null, "onap.restart.tca", "1.0.0");
- assertThat(svcTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1);
- }
-
- @Test
- public void testDeleteSpecificPolicy_unavailablePolicy() throws Exception {
- ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
- .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
- policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
-
- String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
- ToscaServiceTemplate policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class);
- ToscaServiceTemplate serviceTemplate = policyProvider
- .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate);
- assertThat(serviceTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1);
-
- assertThatThrownBy(() -> policyProvider.deletePolicy(null, null, "onap.restart.tca", "2.0.0"))
- .hasMessageContaining("not found");
-
- 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
deleted file mode 100644
index 6b7630c7..00000000
--- a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyTypeProvider.java
+++ /dev/null
@@ -1,199 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * ONAP Policy API
- * ================================================================================
- * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2019-2021 Nordix Foundation.
- * 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.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * SPDX-License-Identifier: Apache-2.0
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.api.main.rest.provider;
-
-import static org.assertj.core.api.Assertions.assertThatCode;
-import static org.assertj.core.api.Assertions.assertThatThrownBy;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-
-import org.junit.Ignore;
-import org.junit.Test;
-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.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 StandardYamlCoder standardYamlCoder = new StandardYamlCoder();
-
- @Autowired
- private PolicyProvider policyProvider;
-
- @Autowired
- private PolicyTypeProvider policyTypeProvider;
-
- private static final String POLICY_TYPE_VERSION = "1.0.0";
-
- private static final String POLICY_RESOURCE_MONITORING = "policies/vCPE.policy.monitoring.input.tosca.yaml";
- private static final String POLICY_TYPE_RESOURCE_MONITORING = "policytypes/onap.policies.monitoring.tcagen2.yaml";
- private static final String POLICY_TYPE_RESOURCE_WITH_NO_VERSION =
- "policytypes/onap.policies.optimization.Resource.no.version.yaml";
- private static final String POLICY_TYPE_NAME_MONITORING = "onap.policies.monitoring.tcagen2";
-
- private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON =
- "policytypes/onap.policies.controlloop.operational.Common.yaml";
- private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_DROOLS =
- "policytypes/onap.policies.controlloop.operational.common.Drools.yaml";
- private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_APEX =
- "policytypes/onap.policies.controlloop.operational.common.Apex.yaml";
- private static final String POLICY_TYPE_OPERATIONAL_COMMON = "onap.policies.controlloop.operational.Common";
- 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";
-
- @Test
- public void testFetchPolicyTypes() throws Exception {
-
- ToscaServiceTemplate serviceTemplate = policyTypeProvider.fetchPolicyTypes(null, null);
- assertFalse(serviceTemplate.getPolicyTypes().isEmpty());
-
- assertThatThrownBy(() -> {
- policyTypeProvider.fetchPolicyTypes("dummy", null);
- }).hasMessage("policy types for filter ToscaEntityFilter(name=dummy, version=null) do not exist");
-
- assertThatThrownBy(() -> {
- policyTypeProvider.fetchPolicyTypes("dummy", "dummy");
- }).hasMessage("policy types for filter ToscaEntityFilter(name=dummy, version=dummy) do not exist");
- }
-
- @Test
- public void testFetchLatestPolicyTypes() {
-
- assertThatThrownBy(() -> {
- policyTypeProvider.fetchLatestPolicyTypes("dummy");
- }).hasMessage("policy types for filter ToscaEntityFilter(name=dummy, version=LATEST) do not exist");
- }
-
- @Test
- public void testCreatePolicyType() throws Exception {
-
- ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
- .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_MONITORING), ToscaServiceTemplate.class);
- ToscaServiceTemplate serviceTemplate = policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
- assertFalse(serviceTemplate.getPolicyTypes().isEmpty());
-
- assertThatCode(() -> {
- policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
- }).doesNotThrowAnyException();
-
- ToscaPolicyType policyType = policyTypeServiceTemplate.getPolicyTypes().get("onap.policies.monitoring.tcagen2");
- policyType.setDescription("Some other description");
-
- assertThatThrownBy(() -> {
- policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
- }).hasMessageContaining("item \"entity\" value \"onap.policies.monitoring.tcagen2:1.0.0\" INVALID, "
- + "does not equal existing entity");
-
- assertThatThrownBy(() -> {
- ToscaServiceTemplate badPolicyType =
- standardYamlCoder.decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_WITH_NO_VERSION),
- ToscaServiceTemplate.class);
- policyTypeProvider.createPolicyType(badPolicyType);
- }).hasMessageContaining("item \"version\" value \"0.0.0\" INVALID, is null");
-
- policyTypeProvider.deletePolicyType(POLICY_TYPE_NAME_MONITORING, POLICY_TYPE_VERSION);
- }
-
- @Test
- public void testCreateOperationalPolicyTypes() throws CoderException, PfModelException {
- ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode(
- ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON), ToscaServiceTemplate.class);
- ToscaServiceTemplate serviceTemplate = policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
-
- assertNotNull(serviceTemplate.getPolicyTypes().get(POLICY_TYPE_OPERATIONAL_COMMON));
-
- policyTypeServiceTemplate = standardYamlCoder.decode(
- ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_DROOLS), ToscaServiceTemplate.class);
- serviceTemplate = policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
- assertNotNull(serviceTemplate.getPolicyTypes().get(POLICY_TYPE_OPERATIONAL_DROOLS));
-
- policyTypeProvider.deletePolicyType(POLICY_TYPE_OPERATIONAL_DROOLS, POLICY_TYPE_VERSION);
- policyTypeProvider.deletePolicyType(POLICY_TYPE_OPERATIONAL_COMMON, POLICY_TYPE_VERSION);
- }
-
- @Test
- public void testCreateApexOperationalPolicyTypes() throws CoderException, PfModelException {
- ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder.decode(
- ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON), ToscaServiceTemplate.class);
- ToscaServiceTemplate serviceTemplate = policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
- policyTypeServiceTemplate = standardYamlCoder.decode(
- ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_APEX), ToscaServiceTemplate.class);
- serviceTemplate = policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
- assertNotNull(serviceTemplate.getPolicyTypes().get(POLICY_TYPE_OPERATIONAL_APEX));
- policyTypeProvider.deletePolicyType(POLICY_TYPE_OPERATIONAL_APEX, POLICY_TYPE_VERSION);
- }
-
- @Test
- public void testDeletePolicyType() throws Exception {
-
- ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder
- .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_MONITORING), ToscaServiceTemplate.class);
- ToscaServiceTemplate serviceTemplate = policyTypeProvider.createPolicyType(policyTypeServiceTemplate);
- assertFalse(serviceTemplate.getPolicyTypes().isEmpty());
-
- ToscaServiceTemplate policyServiceTemplate = standardYamlCoder
- .decode(ResourceUtils.getResourceAsString(POLICY_RESOURCE_MONITORING), ToscaServiceTemplate.class);
- policyProvider.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate);
-
- String exceptionMessage = "policy type onap.policies.monitoring.tcagen2:1.0.0 is in use, "
- + "it is referenced in policy onap.restart.tca:1.0.0";
- assertThatThrownBy(() -> {
- policyTypeProvider.deletePolicyType("onap.policies.monitoring.tcagen2", "1.0.0");
- }).hasMessage(exceptionMessage);
-
- serviceTemplate =
- policyProvider.deletePolicy("onap.policies.monitoring.tcagen2", "1.0.0", "onap.restart.tca", "1.0.0");
- assertFalse(serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).isEmpty());
-
- serviceTemplate = policyTypeProvider.deletePolicyType("onap.policies.monitoring.tcagen2", "1.0.0");
- assertFalse(serviceTemplate.getPolicyTypes().isEmpty());
-
- assertThatThrownBy(() -> {
- 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/service/TestCommonToscaServiceTemplateService.java b/main/src/test/java/org/onap/policy/api/main/service/TestCommonToscaServiceTemplateService.java
new file mode 100644
index 00000000..8d80cac8
--- /dev/null
+++ b/main/src/test/java/org/onap/policy/api/main/service/TestCommonToscaServiceTemplateService.java
@@ -0,0 +1,97 @@
+/*
+ * ============LICENSE_START=======================================================
+ * 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.
+ * 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.service;
+
+import java.util.Optional;
+import org.junit.Before;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.onap.policy.api.main.repository.ToscaServiceTemplateRepository;
+import org.onap.policy.models.base.PfConceptKey;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
+
+/**
+ * This class offers common mock utility methods for uni testing {@link ToscaServiceTemplateService}.
+ */
+public class TestCommonToscaServiceTemplateService {
+
+ protected enum Operation {
+ CREATE_POLICY_TYPE,
+ DELETE_POLICY_TYPE,
+ CREATE_POLICY,
+ DELETE_POLICY;
+ }
+
+ @Mock
+ protected ToscaServiceTemplateRepository toscaServiceTemplateRepository;
+ @Mock
+ protected PolicyTypeService policyTypeService;
+ @Mock
+ protected PolicyService policyService;
+
+ /**
+ * Setup the DB TOSCA service template object post create, and delete request.
+ * @param dbSvcTemplate ToscaServiceTemplate object to update
+ * @param svcTemplateFragment the CRUD operation response ToscaServiceTemplate object
+ * @param operation the CRUD operation performed
+ */
+ protected void mockDbServiceTemplate(ToscaServiceTemplate dbSvcTemplate, ToscaServiceTemplate svcTemplateFragment,
+ TestToscaServiceTemplateServiceForPolicyCrud.Operation operation) {
+ if (operation != null) {
+ switch (operation) {
+ case CREATE_POLICY_TYPE:
+ dbSvcTemplate.getPolicyTypes().putAll(svcTemplateFragment.getPolicyTypes());
+ if (svcTemplateFragment.getDataTypes() != null) {
+ if (dbSvcTemplate.getDataTypes() == null) {
+ dbSvcTemplate.setDataTypes(svcTemplateFragment.getDataTypes());
+ } else {
+ dbSvcTemplate.getDataTypes().putAll(svcTemplateFragment.getDataTypes());
+ }
+ }
+ break;
+ case DELETE_POLICY_TYPE:
+ dbSvcTemplate.getPolicyTypes().keySet().removeAll(svcTemplateFragment.getPolicyTypes().keySet());
+ break;
+ case CREATE_POLICY:
+ dbSvcTemplate.setToscaTopologyTemplate(svcTemplateFragment.getToscaTopologyTemplate());
+ break;
+ case DELETE_POLICY:
+ dbSvcTemplate.getToscaTopologyTemplate().setPolicies(null);
+ break;
+ default:
+ break;
+ }
+ }
+ Mockito.when(toscaServiceTemplateRepository.findById(new PfConceptKey(JpaToscaServiceTemplate.DEFAULT_NAME,
+ JpaToscaServiceTemplate.DEFAULT_VERSION)))
+ .thenReturn(Optional.of(new JpaToscaServiceTemplate(dbSvcTemplate)));
+ }
+
+ /**
+ * Setup to return empty DB service template.
+ */
+ @Before
+ public void setUp() {
+ Mockito.when(toscaServiceTemplateRepository.findById(new PfConceptKey(JpaToscaServiceTemplate.DEFAULT_NAME,
+ JpaToscaServiceTemplate.DEFAULT_VERSION))).thenReturn(Optional.of(new JpaToscaServiceTemplate()));
+ }
+} \ No newline at end of file
diff --git a/main/src/test/java/org/onap/policy/api/main/service/TestPdpGroupService.java b/main/src/test/java/org/onap/policy/api/main/service/TestPdpGroupService.java
new file mode 100644
index 00000000..4c04a08e
--- /dev/null
+++ b/main/src/test/java/org/onap/policy/api/main/service/TestPdpGroupService.java
@@ -0,0 +1,84 @@
+/*
+ * ============LICENSE_START=======================================================
+ * 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.
+ * 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.service;
+
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.mockito.Mockito.when;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.onap.policy.api.main.repository.PdpGroupRepository;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.common.utils.resources.ResourceUtils;
+import org.onap.policy.models.pdp.concepts.PdpGroups;
+import org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup;
+
+@RunWith(MockitoJUnitRunner.class)
+public class TestPdpGroupService {
+
+ @Mock
+ private PdpGroupRepository pdpGroupRepository;
+
+ @InjectMocks
+ private PdpGroupService pdpGroupService;
+
+ /**
+ * Test setup.
+ * @throws CoderException decode errors
+ */
+ @Before
+ public void setUp() throws CoderException {
+ var pdpGroups = new StandardCoder().decode(ResourceUtils.getResourceAsString("pdpgroups/PdpGroups.json"),
+ PdpGroups.class).getGroups();
+ List<JpaPdpGroup> jpaPdpGroupList = new ArrayList<>();
+ pdpGroups.forEach(pdpGroup -> jpaPdpGroupList.add(new JpaPdpGroup(pdpGroup)));
+
+ when(pdpGroupRepository.findAll()).thenReturn(jpaPdpGroupList);
+ }
+
+ @Test
+ public void testAssertPolicyTypeNotSupportedInPdpGroup() {
+ assertThatCode(() -> pdpGroupService.assertPolicyTypeNotSupportedInPdpGroup("policy_type_not_supported",
+ "1.0.0")).doesNotThrowAnyException();
+
+ assertThatThrownBy(() -> pdpGroupService.assertPolicyTypeNotSupportedInPdpGroup(
+ "onap.policies.controlloop.guard.common.FrequencyLimiter", "1.0.0"))
+ .hasMessage("policy type is in use, it is referenced in PDP group defaultGroup subgroup xacml");
+ }
+
+ @Test
+ public void testAssertPolicyNotDeployedInPdpGroup() {
+ assertThatCode(() -> pdpGroupService.assertPolicyNotDeployedInPdpGroup("policy_not_deployed", "1.0.0"))
+ .doesNotThrowAnyException();
+
+ assertThatThrownBy(() -> pdpGroupService.assertPolicyNotDeployedInPdpGroup(
+ "onap.policies.controlloop.operational.common.apex.SampleDomain", "1.0.0"))
+ .hasMessage("policy is in use, it is deployed in PDP group defaultGroup subgroup apex");
+ }
+} \ No newline at end of file
diff --git a/main/src/test/java/org/onap/policy/api/main/service/TestPolicyService.java b/main/src/test/java/org/onap/policy/api/main/service/TestPolicyService.java
new file mode 100644
index 00000000..f9baf76a
--- /dev/null
+++ b/main/src/test/java/org/onap/policy/api/main/service/TestPolicyService.java
@@ -0,0 +1,49 @@
+/*
+ * ============LICENSE_START=======================================================
+ * 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.
+ * 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.service;
+
+import static org.assertj.core.api.Assertions.assertThatCode;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.onap.policy.api.main.repository.PolicyRepository;
+import org.onap.policy.models.base.PfConceptKey;
+
+@RunWith(MockitoJUnitRunner.class)
+public class TestPolicyService {
+
+ @Mock
+ private PolicyRepository policyRepository;
+
+ @InjectMocks
+ private PolicyService policyService;
+
+ @Test
+ public void testDeletePolicy() {
+ PfConceptKey id = new PfConceptKey("dummy", "1.0.0");
+ Mockito.doNothing().when(policyRepository).deleteById(id);
+ assertThatCode(() -> policyService.deletePolicy(id)).doesNotThrowAnyException();
+ }
+} \ No newline at end of file
diff --git a/main/src/test/java/org/onap/policy/api/main/service/TestPolicyTypeService.java b/main/src/test/java/org/onap/policy/api/main/service/TestPolicyTypeService.java
new file mode 100644
index 00000000..21f1ba4d
--- /dev/null
+++ b/main/src/test/java/org/onap/policy/api/main/service/TestPolicyTypeService.java
@@ -0,0 +1,49 @@
+/*
+ * ============LICENSE_START=======================================================
+ * 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.
+ * 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.service;
+
+import static org.assertj.core.api.Assertions.assertThatCode;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.onap.policy.api.main.repository.PolicyTypeRepository;
+import org.onap.policy.models.base.PfConceptKey;
+
+@RunWith(MockitoJUnitRunner.class)
+public class TestPolicyTypeService {
+
+ @Mock
+ private PolicyTypeRepository policyTypeRepository;
+
+ @InjectMocks
+ private PolicyTypeService policyTypeService;
+
+ @Test
+ public void testDeletePolicy() {
+ PfConceptKey id = new PfConceptKey("dummy", "1.0.0");
+ Mockito.doNothing().when(policyTypeRepository).deleteById(id);
+ assertThatCode(() -> policyTypeService.deletePolicyType(id)).doesNotThrowAnyException();
+ }
+} \ No newline at end of file
diff --git a/main/src/test/java/org/onap/policy/api/main/service/TestToscaServiceTemplateServiceForPolicyCrud.java b/main/src/test/java/org/onap/policy/api/main/service/TestToscaServiceTemplateServiceForPolicyCrud.java
new file mode 100644
index 00000000..69864282
--- /dev/null
+++ b/main/src/test/java/org/onap/policy/api/main/service/TestToscaServiceTemplateServiceForPolicyCrud.java
@@ -0,0 +1,437 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP Policy API
+ * ================================================================================
+ * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019-2021 Nordix Foundation.
+ * 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.
+ * 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.service;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+
+import java.util.Optional;
+import javax.ws.rs.core.Response;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.common.utils.coder.StandardYamlCoder;
+import org.onap.policy.common.utils.resources.ResourceUtils;
+import org.onap.policy.models.base.PfConceptKey;
+import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.base.PfModelRuntimeException;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
+
+/**
+ * This class performs unit test of Policy CRUD operations as implemented in {@link ToscaServiceTemplateService}.
+ *
+ * @author Chenfei Gao (cgao@research.att.com)
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class TestToscaServiceTemplateServiceForPolicyCrud extends TestCommonToscaServiceTemplateService {
+
+ 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";
+ private static final String POLICY_RESOURCE_WITH_BAD_POLICYTYPE_ID = "policies/vCPE.policy.bad.policytypeid.json";
+ private static final String POLICY_RESOURCE_WITH_BAD_POLICYTYPE_VERSION =
+ "policies/vCPE.policy.bad.policytypeversion.json";
+ private static final String POLICY_RESOURCE_WITH_NO_POLICY_VERSION = "policies/vCPE.policy.no.policyversion.json";
+ private static final String POLICY_RESOURCE_WITH_DIFFERENT_FIELDS =
+ "policies/vCPE.policy.different.policy.fields.json";
+ private static final String MULTIPLE_POLICIES_RESOURCE = "policies/vCPE.policies.optimization.input.tosca.json";
+
+ private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON =
+ "policytypes/onap.policies.controlloop.operational.Common.yaml";
+ private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_DROOLS =
+ "policytypes/onap.policies.controlloop.operational.common.Drools.yaml";
+ 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";
+
+ @Mock
+ private PdpGroupService pdpGroupService;
+
+ @InjectMocks
+ private ToscaServiceTemplateService toscaServiceTemplateService;
+
+ @Before
+ public void setUp() {
+ super.setUp();
+ }
+
+ @Test
+ public void testFetchPolicies() {
+ Mockito.when(toscaServiceTemplateRepository.findById(new PfConceptKey(JpaToscaServiceTemplate.DEFAULT_NAME,
+ JpaToscaServiceTemplate.DEFAULT_VERSION))).thenReturn(Optional.empty());
+
+ assertThatThrownBy(() -> {
+ toscaServiceTemplateService.fetchPolicies("dummy", "1.0.0", null, null, null);
+ }).hasMessage("service template not found in database");
+
+ assertThatThrownBy(() -> {
+ toscaServiceTemplateService.fetchPolicies("dummy", "1.0.0", "dummy", null, null);
+ }).hasMessage("service template not found in database");
+
+ assertThatThrownBy(() -> {
+ toscaServiceTemplateService.fetchPolicies("dummy", "1.0.0", "dummy", "1.0.0", null);
+ }).hasMessage("service template not found in database");
+
+ assertThatThrownBy(() -> {
+ toscaServiceTemplateService.fetchPolicies(null, null, "dummy", "1.0.0", null);
+ }).hasMessage("service template not found in database");
+ }
+
+ @Test
+ public void testFetchLatestPolicies() {
+ Mockito.when(toscaServiceTemplateRepository.findById(new PfConceptKey(JpaToscaServiceTemplate.DEFAULT_NAME,
+ JpaToscaServiceTemplate.DEFAULT_VERSION))).thenReturn(Optional.empty());
+
+ assertThatThrownBy(() -> {
+ toscaServiceTemplateService.fetchLatestPolicies("dummy", "dummy", "dummy", null);
+ }).hasMessage("service template not found in database");
+ }
+
+ @Test
+ public void testCreatePolicy() throws Exception {
+ assertThatThrownBy(() -> {
+ toscaServiceTemplateService.createPolicy("dummy", "1.0.0", new ToscaServiceTemplate());
+ }).hasMessage("topology template not specified on service template");
+
+ var policyTypeServiceTemplate = standardYamlCoder
+ .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
+ var serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
+ mockDbServiceTemplate(serviceTemplate, null, null);
+
+ assertThatCode(() -> toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate))
+ .doesNotThrowAnyException();
+
+ assertThatThrownBy(() -> {
+ var badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_BAD_POLICYTYPE_ID);
+ var badPolicyServiceTemplate =
+ standardCoder.decode(badPolicyString, ToscaServiceTemplate.class);
+ toscaServiceTemplateService.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0",
+ badPolicyServiceTemplate);
+ }).hasMessage(
+ "Version not specified, the version of this TOSCA entity must be specified in "
+ + "the type_version field");
+
+ assertThatThrownBy(() -> {
+ var badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_BAD_POLICYTYPE_VERSION);
+ var badPolicyServiceTemplate =
+ standardCoder.decode(badPolicyString, ToscaServiceTemplate.class);
+ toscaServiceTemplateService.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0",
+ badPolicyServiceTemplate);
+ }).hasMessageContaining(
+ "item \"policy type\" value \"onap.policies.monitoring.cdap.tca.hi.lo.app:2.0.0\" INVALID, not found");
+
+ assertThatThrownBy(() -> {
+ var badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_NO_POLICY_VERSION);
+ var badPolicyServiceTemplate =
+ standardCoder.decode(badPolicyString, ToscaServiceTemplate.class);
+ toscaServiceTemplateService.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0",
+ badPolicyServiceTemplate);
+ }).hasMessageContaining("item \"version\" value \"0.0.0\" INVALID, is null");
+
+ var policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
+ var policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class);
+ var createPolicyResponseFragment = toscaServiceTemplateService
+ .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate);
+ assertFalse(createPolicyResponseFragment.getToscaTopologyTemplate().getPolicies().get(0).isEmpty());
+ mockDbServiceTemplate(serviceTemplate, createPolicyResponseFragment, Operation.CREATE_POLICY);
+
+ assertThatThrownBy(() -> {
+ var badPolicyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_WITH_DIFFERENT_FIELDS);
+ var badPolicyServiceTemplate =
+ standardCoder.decode(badPolicyString, ToscaServiceTemplate.class);
+ toscaServiceTemplateService.createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0",
+ badPolicyServiceTemplate);
+ }).hasMessageContaining(
+ "item \"entity\" value \"onap.restart.tca:1.0.0\" INVALID, " + "does not equal existing entity");
+ }
+
+ @Test
+ public void testCreateOperationalDroolsPolicy() throws CoderException {
+ var policyTypeServiceTemplate = standardYamlCoder.decode(
+ ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON), ToscaServiceTemplate.class);
+
+ var serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
+ mockDbServiceTemplate(serviceTemplate, null, null);
+
+ policyTypeServiceTemplate = standardYamlCoder.decode(
+ ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_DROOLS), ToscaServiceTemplate.class);
+ var createPolicyTypeResponseFragment = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
+ mockDbServiceTemplate(serviceTemplate, createPolicyTypeResponseFragment, Operation.CREATE_POLICY_TYPE);
+
+ var policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE_OPERATIONAL);
+ var policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class);
+ serviceTemplate =
+ toscaServiceTemplateService.createPolicy(POLICY_TYPE_OPERATIONAL_DROOLS, "1.0.0", policyServiceTemplate);
+ assertFalse(serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).isEmpty());
+ }
+
+ @Test
+ public void testSimpleCreatePolicy() throws Exception {
+
+ assertThatThrownBy(() -> {
+ String multiPoliciesString = ResourceUtils.getResourceAsString(MULTIPLE_POLICIES_RESOURCE);
+ ToscaServiceTemplate multiPoliciesServiceTemplate =
+ standardCoder.decode(multiPoliciesString, ToscaServiceTemplate.class);
+ toscaServiceTemplateService.createPolicies(multiPoliciesServiceTemplate);
+ }).hasMessageContaining(
+ "no policy types are defined on the service template for the policies in the topology template");
+
+ // Create required policy types
+ var policyTypeServiceTemplate = standardYamlCoder.decode(
+ ResourceUtils.getResourceAsString("policytypes/onap.policies.Optimization.yaml"),
+ ToscaServiceTemplate.class);
+ var serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
+ mockDbServiceTemplate(serviceTemplate, null, null);
+
+ policyTypeServiceTemplate = standardYamlCoder.decode(
+ ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.Resource.yaml"),
+ ToscaServiceTemplate.class);
+ var createPolicyTypeResponseFragment = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
+ mockDbServiceTemplate(serviceTemplate, createPolicyTypeResponseFragment, Operation.CREATE_POLICY_TYPE);
+
+ policyTypeServiceTemplate = standardYamlCoder.decode(
+ ResourceUtils
+ .getResourceAsString("policytypes/onap.policies.optimization.resource.AffinityPolicy.yaml"),
+ ToscaServiceTemplate.class);
+ createPolicyTypeResponseFragment = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
+ mockDbServiceTemplate(serviceTemplate, createPolicyTypeResponseFragment, Operation.CREATE_POLICY_TYPE);
+
+ policyTypeServiceTemplate = standardYamlCoder.decode(
+ ResourceUtils
+ .getResourceAsString("policytypes/onap.policies.optimization.resource.DistancePolicy.yaml"),
+ ToscaServiceTemplate.class);
+ createPolicyTypeResponseFragment = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
+ mockDbServiceTemplate(serviceTemplate, createPolicyTypeResponseFragment, Operation.CREATE_POLICY_TYPE);
+
+ policyTypeServiceTemplate = standardYamlCoder.decode(
+ ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.resource.Vim_fit.yaml"),
+ ToscaServiceTemplate.class);
+ createPolicyTypeResponseFragment = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
+ mockDbServiceTemplate(serviceTemplate, createPolicyTypeResponseFragment, Operation.CREATE_POLICY_TYPE);
+
+ policyTypeServiceTemplate = standardYamlCoder.decode(
+ ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.resource.HpaPolicy.yaml"),
+ ToscaServiceTemplate.class);
+ createPolicyTypeResponseFragment = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
+ mockDbServiceTemplate(serviceTemplate, createPolicyTypeResponseFragment, Operation.CREATE_POLICY_TYPE);
+
+ policyTypeServiceTemplate = standardYamlCoder.decode(
+ ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.resource.VnfPolicy.yaml"),
+ ToscaServiceTemplate.class);
+ createPolicyTypeResponseFragment = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
+ mockDbServiceTemplate(serviceTemplate, createPolicyTypeResponseFragment, Operation.CREATE_POLICY_TYPE);
+
+ policyTypeServiceTemplate = standardYamlCoder.decode(
+ ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.Service.yaml"),
+ ToscaServiceTemplate.class);
+ createPolicyTypeResponseFragment = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
+ mockDbServiceTemplate(serviceTemplate, createPolicyTypeResponseFragment, Operation.CREATE_POLICY_TYPE);
+
+ policyTypeServiceTemplate = standardYamlCoder.decode(
+ ResourceUtils
+ .getResourceAsString("policytypes/onap.policies.optimization.service.SubscriberPolicy.yaml"),
+ ToscaServiceTemplate.class);
+ createPolicyTypeResponseFragment = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
+ mockDbServiceTemplate(serviceTemplate, createPolicyTypeResponseFragment, Operation.CREATE_POLICY_TYPE);
+
+ policyTypeServiceTemplate = standardYamlCoder.decode(
+ ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.service.QueryPolicy.yaml"),
+ ToscaServiceTemplate.class);
+ createPolicyTypeResponseFragment = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
+ mockDbServiceTemplate(serviceTemplate, createPolicyTypeResponseFragment, Operation.CREATE_POLICY_TYPE);
+
+ policyTypeServiceTemplate = standardYamlCoder.decode(
+ ResourceUtils.getResourceAsString("policytypes/onap.policies.monitoring.tcagen2.yaml"),
+ ToscaServiceTemplate.class);
+ createPolicyTypeResponseFragment = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
+ mockDbServiceTemplate(serviceTemplate, createPolicyTypeResponseFragment, Operation.CREATE_POLICY_TYPE);
+
+ // Create multiple policies in one call
+ var multiPoliciesString = ResourceUtils.getResourceAsString(MULTIPLE_POLICIES_RESOURCE);
+ var multiPoliciesServiceTemplate =
+ standardCoder.decode(multiPoliciesString, ToscaServiceTemplate.class);
+
+ assertThatCode(() -> {
+ toscaServiceTemplateService.createPolicies(multiPoliciesServiceTemplate);
+ toscaServiceTemplateService.createPolicies(multiPoliciesServiceTemplate);
+ }).doesNotThrowAnyException();
+ }
+
+ @Test
+ public void testDeletePolicy() throws CoderException, PfModelException {
+
+ assertThatThrownBy(() -> {
+ toscaServiceTemplateService.deletePolicy("dummy", "1.0.0", "dummy", "1.0.0");
+ }).hasMessage("no policies found");
+
+ var policyTypeServiceTemplate = standardYamlCoder
+ .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
+ var serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
+ mockDbServiceTemplate(serviceTemplate, null, null);
+
+ var policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
+ var policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class);
+ var createPolicyResponseFragment = toscaServiceTemplateService
+ .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate);
+ assertFalse(createPolicyResponseFragment.getToscaTopologyTemplate().getPolicies().get(0).isEmpty());
+ mockDbServiceTemplate(serviceTemplate, createPolicyResponseFragment, Operation.CREATE_POLICY);
+
+ var exceptionMessage = "policy is in use, it is deployed in PDP group dummy subgroup dummy";
+ Mockito.doThrow(new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, exceptionMessage))
+ .when(pdpGroupService).assertPolicyNotDeployedInPdpGroup("onap.restart.tca", "1.0.0");
+ assertThatThrownBy(() -> {
+ toscaServiceTemplateService.deletePolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0",
+ "onap.restart.tca", "1.0.0");
+ }).hasMessage(exceptionMessage);
+ Mockito.doNothing().when(pdpGroupService).assertPolicyNotDeployedInPdpGroup("onap.restart.tca", "1.0.0");
+
+ var deletePolicyResponseFragment = toscaServiceTemplateService
+ .deletePolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", "onap.restart.tca", "1.0.0");
+ assertFalse(deletePolicyResponseFragment.getToscaTopologyTemplate().getPolicies().get(0).isEmpty());
+
+ mockDbServiceTemplate(serviceTemplate, deletePolicyResponseFragment, Operation.DELETE_POLICY);
+ assertThatThrownBy(() -> {
+ toscaServiceTemplateService.deletePolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0",
+ "onap.restart.tca", "1.0.0");
+ }).hasMessageContaining("no policies found");
+ }
+
+ @Test
+ public void testFetchAllPolicies() throws Exception {
+ // Create Policy Type
+ var policyTypeServiceTemplate = standardYamlCoder
+ .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
+ var serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
+ mockDbServiceTemplate(serviceTemplate, null, null);
+
+ // Create Policy
+ var policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
+ var policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class);
+ var createPolicyResponseFragment = toscaServiceTemplateService
+ .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate);
+ mockDbServiceTemplate(serviceTemplate, createPolicyResponseFragment, Operation.CREATE_POLICY);
+
+ assertThat(serviceTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1);
+
+ // Test fetch all policies
+ serviceTemplate = toscaServiceTemplateService.fetchPolicies(null, null, null, null, null);
+
+ assertThat(serviceTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1);
+ }
+
+ @Test
+ public void testFetchSpecificPolicy_availablePolicy() throws Exception {
+ // Create Policy Type
+ var policyTypeServiceTemplate = standardYamlCoder
+ .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
+ var serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
+ mockDbServiceTemplate(serviceTemplate, null, null);
+
+ // Create Policy
+ var policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
+ var policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class);
+ var createPolicyResponseFragment = toscaServiceTemplateService
+ .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate);
+ mockDbServiceTemplate(serviceTemplate, createPolicyResponseFragment, Operation.CREATE_POLICY);
+
+ assertThat(serviceTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1);
+
+ // Test fetch specific policy
+ assertThat(toscaServiceTemplateService.fetchPolicies(null, null, "onap.restart.tca", "1.0.0", null)
+ .getToscaTopologyTemplate().getPolicies()).hasSize(1);
+ }
+
+ @Test
+ public void testFetchSpecificPolicy_unavailablePolicy() throws Exception {
+ // Create Policy Type
+ var policyTypeServiceTemplate = standardYamlCoder
+ .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
+ var serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
+ mockDbServiceTemplate(serviceTemplate, null, null);
+
+ // Create Policy
+ var policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
+ var policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class);
+ var createPolicyResponseFragment = toscaServiceTemplateService
+ .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate);
+ mockDbServiceTemplate(serviceTemplate, createPolicyResponseFragment, Operation.CREATE_POLICY);
+ assertNotNull(serviceTemplate.getToscaTopologyTemplate().getPolicies());
+ assertThat(serviceTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1);
+
+ // Test fetch specific policy
+ assertThatThrownBy(() -> toscaServiceTemplateService.fetchPolicies(null, null, "onap.restart.tca",
+ "2.0.0", null)).hasMessageContaining("policies for onap.restart.tca:2.0.0 do not exist");
+ }
+
+ @Test
+ public void testDeleteSpecificPolicy_availablePolicy() throws Exception {
+ var policyTypeServiceTemplate = standardYamlCoder
+ .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
+ var serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
+ mockDbServiceTemplate(serviceTemplate, null, null);
+
+ var policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
+ var policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class);
+ var createPolicyResponseFragment = toscaServiceTemplateService
+ .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate);
+ assertThat(createPolicyResponseFragment.getToscaTopologyTemplate().getPolicies()).hasSize(1);
+ mockDbServiceTemplate(serviceTemplate, createPolicyResponseFragment, Operation.CREATE_POLICY);
+
+ serviceTemplate = toscaServiceTemplateService.deletePolicy(null, null, "onap.restart.tca", "1.0.0");
+ assertThat(serviceTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1);
+ }
+
+ @Test
+ public void testDeleteSpecificPolicy_unavailablePolicy() throws Exception {
+ var policyTypeServiceTemplate = standardYamlCoder
+ .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class);
+ var serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
+ mockDbServiceTemplate(serviceTemplate, null, null);
+
+ var policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE);
+ var policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class);
+ var createPolicyResponseFragment = toscaServiceTemplateService
+ .createPolicy("onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate);
+ assertThat(createPolicyResponseFragment.getToscaTopologyTemplate().getPolicies()).hasSize(1);
+ mockDbServiceTemplate(serviceTemplate, createPolicyResponseFragment, Operation.CREATE_POLICY);
+
+ assertThatThrownBy(() -> toscaServiceTemplateService.deletePolicy(null, null, "onap.restart.tca", "2.0.0"))
+ .hasMessageContaining("not found");
+
+ assertThatThrownBy(() -> toscaServiceTemplateService.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/service/TestToscaServiceTemplateServiceForPolicyTypeCrud.java b/main/src/test/java/org/onap/policy/api/main/service/TestToscaServiceTemplateServiceForPolicyTypeCrud.java
new file mode 100644
index 00000000..cea2bcf0
--- /dev/null
+++ b/main/src/test/java/org/onap/policy/api/main/service/TestToscaServiceTemplateServiceForPolicyTypeCrud.java
@@ -0,0 +1,213 @@
+/*
+ * ============LICENSE_START=======================================================
+ * 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.
+ * 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.service;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+
+import javax.ws.rs.core.Response;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.junit.MockitoJUnitRunner;
+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.base.PfModelRuntimeException;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+
+/**
+ * This class performs unit test of Policy Type CRUD operations as implemented in {@link ToscaServiceTemplateService}.
+ *
+ * @author Chenfei Gao (cgao@research.att.com)
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class TestToscaServiceTemplateServiceForPolicyTypeCrud extends TestCommonToscaServiceTemplateService {
+
+ private static StandardYamlCoder coder = new StandardYamlCoder();
+ private static final String POLICY_TYPE_VERSION = "1.0.0";
+
+ private static final String POLICY_RESOURCE_MONITORING = "policies/vCPE.policy.monitoring.input.tosca.yaml";
+ private static final String POLICY_TYPE_RESOURCE_MONITORING = "policytypes/onap.policies.monitoring.tcagen2.yaml";
+ private static final String POLICY_TYPE_RESOURCE_WITH_NO_VERSION =
+ "policytypes/onap.policies.optimization.Resource.no.version.yaml";
+ private static final String POLICY_TYPE_NAME_MONITORING = "onap.policies.monitoring.tcagen2";
+
+ private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON =
+ "policytypes/onap.policies.controlloop.operational.Common.yaml";
+ private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_DROOLS =
+ "policytypes/onap.policies.controlloop.operational.common.Drools.yaml";
+ private static final String POLICY_TYPE_RESOURCE_OPERATIONAL_APEX =
+ "policytypes/onap.policies.controlloop.operational.common.Apex.yaml";
+ private static final String POLICY_TYPE_OPERATIONAL_COMMON = "onap.policies.controlloop.operational.Common";
+ 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";
+
+ @Mock
+ private PdpGroupService pdpGroupService;
+
+ @InjectMocks
+ private ToscaServiceTemplateService toscaServiceTemplateService;
+
+ /**
+ * Test setup.
+ */
+ @Before
+ public void setUp() {
+ super.setUp();
+ }
+
+ @Test
+ public void testFetchPolicyTypes() throws PfModelException {
+ assertThatThrownBy(() -> {
+ toscaServiceTemplateService.fetchPolicyTypes("dummy", null);
+ }).hasMessage("policy types for filter ToscaEntityFilter(name=dummy, version=null) do not exist");
+
+ assertThatThrownBy(() -> {
+ toscaServiceTemplateService.fetchPolicyTypes("dummy", "dummy");
+ }).hasMessage("policy types for filter ToscaEntityFilter(name=dummy, version=dummy) do not exist");
+
+ // FIXME
+ // ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.fetchPolicyTypes(null, null);
+ // assertFalse(serviceTemplate.getPolicyTypes().isEmpty());
+ }
+
+ @Test
+ public void testFetchLatestPolicyTypes() {
+
+ assertThatThrownBy(() -> {
+ toscaServiceTemplateService.fetchLatestPolicyTypes("dummy");
+ }).hasMessage("policy types for filter ToscaEntityFilter(name=dummy, version=LATEST) do not exist");
+ }
+
+ @Test
+ public void testCreatePolicyType() throws CoderException {
+ var policyTypeServiceTemplate = coder
+ .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_MONITORING), ToscaServiceTemplate.class);
+ var serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
+ assertFalse(serviceTemplate.getPolicyTypes().isEmpty());
+ assertEquals(2, serviceTemplate.getPolicyTypes().size());
+ mockDbServiceTemplate(serviceTemplate, null, null);
+
+ policyTypeServiceTemplate.getPolicyTypes().get("onap.policies.monitoring.tcagen2")
+ .setDescription("Some other description");
+
+ assertThatThrownBy(() -> {
+ toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
+ }).hasMessageContaining("item \"entity\" value \"onap.policies.monitoring.tcagen2:1.0.0\" INVALID, "
+ + "does not equal existing entity");
+
+ assertThatThrownBy(() -> {
+ ToscaServiceTemplate badPolicyType =
+ coder.decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_WITH_NO_VERSION),
+ ToscaServiceTemplate.class);
+ toscaServiceTemplateService.createPolicyType(badPolicyType);
+ }).hasMessageContaining("item \"version\" value \"0.0.0\" INVALID, is null");
+
+ toscaServiceTemplateService.deletePolicyType(POLICY_TYPE_NAME_MONITORING, POLICY_TYPE_VERSION);
+ }
+
+ @Test
+ public void testCreateOperationalPolicyTypes() throws CoderException {
+ ToscaServiceTemplate policyTypeServiceTemplate = coder.decode(
+ ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON), ToscaServiceTemplate.class);
+ ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
+ assertNotNull(serviceTemplate.getPolicyTypes().get(POLICY_TYPE_OPERATIONAL_COMMON));
+ mockDbServiceTemplate(serviceTemplate, null, null);
+
+ policyTypeServiceTemplate = coder.decode(
+ ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_DROOLS), ToscaServiceTemplate.class);
+ var createPolicyTypeResponseFragment = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
+ assertNotNull(createPolicyTypeResponseFragment.getPolicyTypes().get(POLICY_TYPE_OPERATIONAL_DROOLS));
+ mockDbServiceTemplate(serviceTemplate, createPolicyTypeResponseFragment, Operation.CREATE_POLICY_TYPE);
+
+ var deletePolicyTypeResponseFragment = toscaServiceTemplateService
+ .deletePolicyType(POLICY_TYPE_OPERATIONAL_DROOLS, POLICY_TYPE_VERSION);
+ mockDbServiceTemplate(serviceTemplate, deletePolicyTypeResponseFragment, Operation.DELETE_POLICY_TYPE);
+ toscaServiceTemplateService.deletePolicyType(POLICY_TYPE_OPERATIONAL_COMMON, POLICY_TYPE_VERSION);
+ }
+
+ @Test
+ public void testCreateApexOperationalPolicyTypes() throws CoderException {
+ var policyTypeServiceTemplate = coder.decode(
+ ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_COMMON), ToscaServiceTemplate.class);
+ var serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
+
+ mockDbServiceTemplate(serviceTemplate, null, null);
+ policyTypeServiceTemplate = coder.decode(
+ ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_OPERATIONAL_APEX), ToscaServiceTemplate.class);
+ var createPolicyTypeResponseFragment = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
+ assertNotNull(createPolicyTypeResponseFragment.getPolicyTypes().get(POLICY_TYPE_OPERATIONAL_APEX));
+
+ mockDbServiceTemplate(serviceTemplate, createPolicyTypeResponseFragment, Operation.CREATE_POLICY_TYPE);
+ toscaServiceTemplateService.deletePolicyType(POLICY_TYPE_OPERATIONAL_APEX, POLICY_TYPE_VERSION);
+ }
+
+ @Test
+ public void testDeletePolicyType() throws CoderException {
+ var policyTypeServiceTemplate = coder
+ .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE_MONITORING), ToscaServiceTemplate.class);
+ var serviceTemplate = toscaServiceTemplateService.createPolicyType(policyTypeServiceTemplate);
+ assertFalse(serviceTemplate.getPolicyTypes().isEmpty());
+
+ var policyServiceTemplate = coder
+ .decode(ResourceUtils.getResourceAsString(POLICY_RESOURCE_MONITORING), ToscaServiceTemplate.class);
+ mockDbServiceTemplate(serviceTemplate, null, null);
+ var createPolicyResponseFragment = toscaServiceTemplateService.createPolicy("onap.policies.monitoring.tcagen2",
+ "1.0.0", policyServiceTemplate);
+
+ mockDbServiceTemplate(serviceTemplate, createPolicyResponseFragment, Operation.CREATE_POLICY);
+ var exceptionMessage = "policy type onap.policies.monitoring.tcagen2:1.0.0 is in use, "
+ + "it is referenced in policy onap.restart.tca:1.0.0";
+ assertThatThrownBy(() -> {
+ toscaServiceTemplateService.deletePolicyType("onap.policies.monitoring.tcagen2", "1.0.0");
+ }).hasMessage(exceptionMessage);
+
+ var deletePolicyResponseFragment = toscaServiceTemplateService
+ .deletePolicy("onap.policies.monitoring.tcagen2", "1.0.0", "onap.restart.tca", "1.0.0");
+ assertFalse(deletePolicyResponseFragment.getToscaTopologyTemplate().getPolicies().get(0).isEmpty());
+ mockDbServiceTemplate(serviceTemplate, deletePolicyResponseFragment, Operation.DELETE_POLICY);
+
+ exceptionMessage = "policy type is in use, it is referenced in PDP group dummy subgroup dummy";
+ Mockito.doThrow(new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, exceptionMessage))
+ .when(pdpGroupService).assertPolicyTypeNotSupportedInPdpGroup("onap.policies.monitoring.tcagen2", "1.0.0");
+ assertThatThrownBy(() -> {
+ toscaServiceTemplateService.deletePolicyType("onap.policies.monitoring.tcagen2", "1.0.0");
+ }).hasMessage(exceptionMessage);
+
+ Mockito.doNothing().when(pdpGroupService)
+ .assertPolicyTypeNotSupportedInPdpGroup("onap.policies.monitoring.tcagen2", "1.0.0");
+ var deletePolicyTypeResponseFragment = toscaServiceTemplateService
+ .deletePolicyType("onap.policies.monitoring.tcagen2", "1.0.0");
+ assertFalse(deletePolicyTypeResponseFragment.getPolicyTypes().isEmpty());
+
+ mockDbServiceTemplate(serviceTemplate, deletePolicyTypeResponseFragment, Operation.DELETE_POLICY_TYPE);
+ assertThatThrownBy(() -> {
+ toscaServiceTemplateService.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/resources/pdpgroups/PdpGroups.json b/main/src/test/resources/pdpgroups/PdpGroups.json
new file mode 100644
index 00000000..5ec387ed
--- /dev/null
+++ b/main/src/test/resources/pdpgroups/PdpGroups.json
@@ -0,0 +1,125 @@
+{
+ "groups": [
+ {
+ "name": "defaultGroup",
+ "version": "1.0.0",
+ "description": "The default group that registers all supported policy types and pdps.",
+ "pdpGroupState": "ACTIVE",
+ "pdpSubgroups": [
+ {
+ "pdpType": "xacml",
+ "supportedPolicyTypes": [
+ {
+ "name": "onap.policies.controlloop.guard.common.FrequencyLimiter",
+ "version": "1.0.0"
+ },
+ {
+ "name": "onap.policies.controlloop.guard.common.MinMax",
+ "version": "1.0.0"
+ },
+ {
+ "name": "onap.policies.controlloop.guard.common.Blacklist",
+ "version": "1.0.0"
+ },
+ {
+ "name": "onap.policies.controlloop.guard.common.Filter",
+ "version": "1.0.0"
+ },
+ {
+ "name": "onap.policies.controlloop.guard.coordination.FirstBlocksSecond",
+ "version": "1.0.0"
+ },
+ {
+ "name": "onap.policies.monitoring.*",
+ "version": "1.0.0"
+ },
+ {
+ "name": "onap.policies.optimization.*",
+ "version": "1.0.0"
+ },
+ {
+ "name": "onap.policies.optimization.resource.AffinityPolicy",
+ "version": "1.0.0"
+ },
+ {
+ "name": "onap.policies.optimization.resource.DistancePolicy",
+ "version": "1.0.0"
+ },
+ {
+ "name": "onap.policies.optimization.resource.HpaPolicy",
+ "version": "1.0.0"
+ },
+ {
+ "name": "onap.policies.optimization.resource.OptimizationPolicy",
+ "version": "1.0.0"
+ },
+ {
+ "name": "onap.policies.optimization.resource.PciPolicy",
+ "version": "1.0.0"
+ },
+ {
+ "name": "onap.policies.optimization.service.QueryPolicy",
+ "version": "1.0.0"
+ },
+ {
+ "name": "onap.policies.optimization.service.SubscriberPolicy",
+ "version": "1.0.0"
+ },
+ {
+ "name": "onap.policies.optimization.resource.Vim_fit",
+ "version": "1.0.0"
+ },
+ {
+ "name": "onap.policies.optimization.resource.VnfPolicy",
+ "version": "1.0.0"
+ },
+ {
+ "name": "onap.policies.native.Xacml",
+ "version": "1.0.0"
+ },
+ {
+ "name": "onap.policies.Naming",
+ "version": "1.0.0"
+ },
+ {
+ "name": "onap.policies.match.*",
+ "version": "1.0.0"
+ }
+ ],
+ "currentInstanceCount": 0,
+ "desiredInstanceCount": 1,
+ "policies": []
+ },
+ {
+ "pdpType": "drools",
+ "supportedPolicyTypes": [
+ {
+ "name": "onap.policies.controlloop.operational.common.Drools",
+ "version": "1.0.0"
+ }
+ ],
+ "currentInstanceCount": 0,
+ "desiredInstanceCount": 1,
+ "policies": []
+ },
+ {
+ "pdpType": "apex",
+ "supportedPolicyTypes": [
+ {
+ "name": "onap.policies.controlloop.operational.common.Apex",
+ "version": "1.0.0"
+ }
+ ],
+ "policies": [
+ {
+ "name": "onap.policies.controlloop.operational.common.apex.SampleDomain",
+ "version": "1.0.0"
+ }
+ ],
+ "currentInstanceCount": 0,
+ "desiredInstanceCount": 1
+ }
+ ]
+ }
+ ]
+} \ No newline at end of file