aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/test/java/org/onap/policy/pap/main/service/ToscaServiceTemplateServiceTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/test/java/org/onap/policy/pap/main/service/ToscaServiceTemplateServiceTest.java')
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/service/ToscaServiceTemplateServiceTest.java55
1 files changed, 50 insertions, 5 deletions
diff --git a/main/src/test/java/org/onap/policy/pap/main/service/ToscaServiceTemplateServiceTest.java b/main/src/test/java/org/onap/policy/pap/main/service/ToscaServiceTemplateServiceTest.java
index 04c1a281..b64ad287 100644
--- a/main/src/test/java/org/onap/policy/pap/main/service/ToscaServiceTemplateServiceTest.java
+++ b/main/src/test/java/org/onap/policy/pap/main/service/ToscaServiceTemplateServiceTest.java
@@ -1,6 +1,7 @@
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2022 Bell Canada. All rights reserved.
+ * Modifications Copyright (C) 2022 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,7 +24,10 @@ package org.onap.policy.pap.main.service;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import java.util.List;
+import java.util.Map;
import java.util.Optional;
+import javax.ws.rs.core.Response;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -38,6 +42,8 @@ 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.ToscaNodeTemplate;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
import org.onap.policy.pap.main.repository.ToscaServiceTemplateRepository;
@@ -54,14 +60,22 @@ public class ToscaServiceTemplateServiceTest {
private static final String INVALID_VERSION_ERR_MSG =
"parameter \"version\": value \"version\", does not match regular expression \"^(\\d+.){2}\\d+$\"";
+ private static final String NODE_TEMPLATE_NAME = "tca_metadata";
+ private static final String NODE_TEMPLATE_VERSION = "1.0.0";
+
@Mock
private ToscaServiceTemplateRepository toscaRepository;
@InjectMocks
private ToscaServiceTemplateService toscaService;
+ @Mock
+ private ToscaNodeTemplateService nodeTemplateService;
+
private ToscaServiceTemplate serviceTemplate;
+ private ToscaNodeTemplate nodeTemplate;
+
private StandardCoder coder = new StandardYamlCoder();
/**
@@ -71,16 +85,31 @@ public class ToscaServiceTemplateServiceTest {
*/
@Before
public void setup() throws CoderException {
- ToscaServiceTemplate toscaPolicyType = coder
- .decode(ResourceUtils.getResourceAsString("e2e/monitoring.policy-type.yaml"), ToscaServiceTemplate.class);
+
+ coder.decode(ResourceUtils.getResourceAsString("e2e/policyMetadataSet.yaml"),
+ ToscaServiceTemplate.class).getToscaTopologyTemplate().getNodeTemplates()
+ .forEach((key, value) -> nodeTemplate = value);
+
+ ToscaServiceTemplate toscaPolicyType =
+ coder.decode(ResourceUtils.getResourceAsString("e2e/monitoring.policy-type.yaml"),
+ ToscaServiceTemplate.class);
ToscaServiceTemplate toscaPolicy =
- coder.decode(ResourceUtils.getResourceAsString("e2e/monitoring.policy.yaml"), ToscaServiceTemplate.class);
+ coder.decode(ResourceUtils.getResourceAsString("e2e/monitoring.policy.yaml"),
+ ToscaServiceTemplate.class);
+ // Add metadataSet reference to the policy metadata
+ toscaPolicy.getToscaTopologyTemplate().getPolicies().forEach(e -> e.entrySet().iterator().next().getValue()
+ .getMetadata().putAll(Map.of("metadataSetName", NODE_TEMPLATE_NAME,
+ "metadataSetVersion", NODE_TEMPLATE_VERSION)));
serviceTemplate = new ToscaServiceTemplate(toscaPolicyType);
serviceTemplate.setToscaTopologyTemplate(toscaPolicy.getToscaTopologyTemplate());
Mockito
.when(toscaRepository.findById(
new PfConceptKey(JpaToscaServiceTemplate.DEFAULT_NAME, JpaToscaServiceTemplate.DEFAULT_VERSION)))
.thenReturn(Optional.of(new JpaToscaServiceTemplate(serviceTemplate)));
+
+ Mockito
+ .when(nodeTemplateService.getToscaNodeTemplate(NODE_TEMPLATE_NAME, NODE_TEMPLATE_VERSION))
+ .thenReturn(nodeTemplate);
}
@@ -89,17 +118,33 @@ public class ToscaServiceTemplateServiceTest {
assertThatThrownBy(() -> toscaService.getPolicyList(NAME, VERSION))
.isInstanceOf(PfModelRuntimeException.class).hasRootCauseMessage(INVALID_VERSION_ERR_MSG);
- assertThat(toscaService.getPolicyList(NAME, VERSION_1)).hasSize(0);
+ assertThat(toscaService.getPolicyList(NAME, VERSION_1)).isEmpty();
assertThat(toscaService.getPolicyList("onap.restart.tca", VERSION_1)).hasSize(1);
}
@Test
+ public void testPolicyForMetadataSet() throws PfModelException {
+ List<ToscaPolicy> policies = toscaService.getPolicyList("onap.restart.tca", VERSION_1);
+
+ assertThat(policies.get(0).getMetadata()).containsEntry("metadataSet", nodeTemplate.getMetadata());
+
+ Mockito
+ .when(nodeTemplateService.getToscaNodeTemplate(NODE_TEMPLATE_NAME, NODE_TEMPLATE_VERSION))
+ .thenThrow(new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE,
+ "node template for onap.restart.tca:1.0.0 do not exist in the database"));
+
+ assertThatThrownBy(() -> toscaService.getPolicyList("onap.restart.tca", VERSION_1))
+ .isInstanceOf(PfModelRuntimeException.class)
+ .hasMessage("node template for onap.restart.tca:1.0.0 do not exist in the database");
+ }
+
+ @Test
public void testGetPolicyTypeList() throws PfModelException {
assertThatThrownBy(() -> toscaService.getPolicyTypeList(NAME, VERSION))
.isInstanceOf(PfModelRuntimeException.class).hasRootCauseMessage(INVALID_VERSION_ERR_MSG);
- assertThat(toscaService.getPolicyTypeList(NAME, VERSION_1)).hasSize(0);
+ assertThat(toscaService.getPolicyTypeList(NAME, VERSION_1)).isEmpty();
assertThat(toscaService.getPolicyTypeList("onap.policies.monitoring.cdap.tca.hi.lo.app", VERSION_1)).hasSize(2);
assertThat(toscaService.getPolicyTypeList("onap.policies.Monitoring", VERSION_1)).hasSize(1);