2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2023 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.acm.participant.policy.main.utils;
23 import static org.junit.Assert.assertTrue;
25 import java.io.FileNotFoundException;
26 import java.time.Instant;
27 import java.util.ArrayList;
28 import java.util.UUID;
29 import lombok.AccessLevel;
30 import lombok.NoArgsConstructor;
31 import org.onap.policy.clamp.acm.participant.policy.main.parameters.CommonTestData;
32 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantPrime;
33 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
34 import org.onap.policy.common.utils.resources.ResourceUtils;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 @NoArgsConstructor(access = AccessLevel.PRIVATE)
40 public final class TestListenerUtils {
42 private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
43 private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class);
46 * Method to create participantUpdateMsg.
48 * @return ParticipantPrime message
50 public static ParticipantPrime createParticipantPrimeMsg() {
51 final var participantPrimeMsg = new ParticipantPrime();
52 var participantId = CommonTestData.getParticipantId();
54 participantPrimeMsg.setParticipantId(participantId);
55 participantPrimeMsg.setTimestamp(Instant.now());
56 participantPrimeMsg.setTimestamp(Instant.ofEpochMilli(3000));
57 participantPrimeMsg.setMessageId(UUID.randomUUID());
59 var toscaServiceTemplate = testAutomationCompositionRead();
60 // Add policies to the toscaServiceTemplate
61 TestListenerUtils.addPoliciesToToscaServiceTemplate(toscaServiceTemplate);
63 participantPrimeMsg.setParticipantDefinitionUpdates(new ArrayList<>());
64 return participantPrimeMsg;
67 private static ToscaServiceTemplate testAutomationCompositionRead() {
68 var automationCompositionDirectoryContents = ResourceUtils.getDirectoryContents("clamp/acm/test");
70 boolean atLeastOneAutomationCompositionTested = false;
71 ToscaServiceTemplate toscaServiceTemplate = null;
73 for (String automationCompositionFilePath : automationCompositionDirectoryContents) {
74 if (!automationCompositionFilePath.endsWith(".yaml")) {
77 atLeastOneAutomationCompositionTested = true;
78 toscaServiceTemplate = testAutomationCompositionYamlSerialization(automationCompositionFilePath);
81 // Add policy_types to the toscaServiceTemplate
82 addPolicyTypesToToscaServiceTemplate(toscaServiceTemplate);
84 assertTrue(atLeastOneAutomationCompositionTested);
85 return toscaServiceTemplate;
88 private static void addPolicyTypesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) {
89 var policyTypeDirectoryContents = ResourceUtils.getDirectoryContents("policytypes");
91 for (String policyTypeFilePath : policyTypeDirectoryContents) {
92 String policyTypeString = ResourceUtils.getResourceAsString(policyTypeFilePath);
94 var foundPolicyTypeSt =
95 yamlTranslator.fromYaml(policyTypeString, ToscaServiceTemplate.class);
97 toscaServiceTemplate.setDerivedFrom(foundPolicyTypeSt.getDerivedFrom());
98 toscaServiceTemplate.setDescription(foundPolicyTypeSt.getDescription());
99 toscaServiceTemplate.setMetadata(foundPolicyTypeSt.getMetadata());
100 toscaServiceTemplate.setName(foundPolicyTypeSt.getName());
101 toscaServiceTemplate.setToscaDefinitionsVersion(foundPolicyTypeSt.getToscaDefinitionsVersion());
102 toscaServiceTemplate.setVersion(foundPolicyTypeSt.getVersion());
104 if (foundPolicyTypeSt.getDataTypes() != null) {
105 if (toscaServiceTemplate.getDataTypes() == null) {
106 toscaServiceTemplate.setDataTypes(foundPolicyTypeSt.getDataTypes());
108 toscaServiceTemplate.getDataTypes().putAll(foundPolicyTypeSt.getDataTypes());
112 if (toscaServiceTemplate.getPolicyTypes() == null) {
113 toscaServiceTemplate.setPolicyTypes(foundPolicyTypeSt.getPolicyTypes());
115 toscaServiceTemplate.getPolicyTypes().putAll(foundPolicyTypeSt.getPolicyTypes());
121 * Method to add polcies to the toscaServiceTemplate.
123 * @param toscaServiceTemplate to add policies
125 public static void addPoliciesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) {
126 var policiesDirectoryContents = ResourceUtils.getDirectoryContents("policies");
127 toscaServiceTemplate.getToscaTopologyTemplate().setPolicies(new ArrayList<>());
129 for (var policiesFilePath : policiesDirectoryContents) {
130 if (!policiesFilePath.endsWith("yaml")) {
134 var policiesString = ResourceUtils.getResourceAsString(policiesFilePath);
136 var foundPoliciesSt = yamlTranslator.fromYaml(policiesString, ToscaServiceTemplate.class);
137 if (foundPoliciesSt.getToscaTopologyTemplate() != null
138 && foundPoliciesSt.getToscaTopologyTemplate().getPolicies() != null) {
139 toscaServiceTemplate.getToscaTopologyTemplate().getPolicies()
140 .addAll(foundPoliciesSt.getToscaTopologyTemplate().getPolicies());
145 private static ToscaServiceTemplate testAutomationCompositionYamlSerialization(
146 String automationCompositionFilePath) {
148 String automationCompositionString = ResourceUtils.getResourceAsString(automationCompositionFilePath);
149 if (automationCompositionString == null) {
150 throw new FileNotFoundException(automationCompositionFilePath);
153 return yamlTranslator.fromYaml(automationCompositionString, ToscaServiceTemplate.class);
154 } catch (FileNotFoundException e) {
155 LOGGER.error("cannot find YAML file {}", automationCompositionFilePath);
156 throw new IllegalArgumentException(e);