3fb8b6d7ff5280d3374f5e04d44c97c9cd36311d
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.models.controlloop.concepts;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24
25 import java.util.Map;
26 import org.junit.jupiter.api.Test;
27 import org.onap.policy.common.utils.coder.Coder;
28 import org.onap.policy.common.utils.coder.CoderException;
29 import org.onap.policy.common.utils.coder.StandardCoder;
30 import org.onap.policy.common.utils.coder.StandardYamlCoder;
31 import org.onap.policy.common.utils.resources.ResourceUtils;
32 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
33 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
35
36 class ParticipantUtilsTest {
37
38     private static final Coder CODER = new StandardCoder();
39     private static final String TOSCA_TEMPLATE_YAML = "examples/controlloop/PMSubscriptionHandling.yaml";
40     private static final String CONTROL_LOOP_JSON = "src/test/resources/providers/TestControlLoops.json";
41     private static final String CONTROL_LOOP_ELEMENT = "org.onap.policy.clamp.controlloop.ControlLoopElement";
42     private static final String POLICY_CONTROL_LOOP_ELEMENT =
43             "org.onap.policy.clamp.controlloop.PolicyControlLoopElement";
44     private static final String PARTICIPANT_CONTROL_LOOP_ELEMENT = "org.onap.policy.clamp.controlloop.Participant";
45     private static final StandardYamlCoder YAML_TRANSLATOR = new StandardYamlCoder();
46
47     @Test
48     void testFindParticipantType() throws CoderException {
49         var identifier = new ToscaConceptIdentifier("Identifier", "1.0.1");
50         var result = ParticipantUtils.findParticipantType(Map.of("participantType", CODER.encode(identifier)));
51         assertThat(result).isEqualTo(identifier);
52     }
53
54     @Test
55     void testFindStartPhase() {
56         var identifier = 13;
57         var result = ParticipantUtils.findStartPhase(Map.of("startPhase", identifier));
58         assertThat(result).isEqualTo(identifier);
59     }
60
61     @Test
62     void testGetFirstStartPhase() throws CoderException {
63         var serviceTemplate = YAML_TRANSLATOR.decode(ResourceUtils.getResourceAsStream(TOSCA_TEMPLATE_YAML),
64                 ToscaServiceTemplate.class);
65         var controlLoops = CODER.decode(ResourceUtils.getResourceAsString(CONTROL_LOOP_JSON), ControlLoops.class);
66         var result = ParticipantUtils.getFirstStartPhase(controlLoops.getControlLoopList().get(0), serviceTemplate);
67         assertThat(result).isZero();
68     }
69
70     @Test
71     void testCheckIfNodeTemplateIsControlLoopElement() throws CoderException {
72         var serviceTemplate = YAML_TRANSLATOR.decode(ResourceUtils.getResourceAsStream(TOSCA_TEMPLATE_YAML),
73                 ToscaServiceTemplate.class);
74         var nodeTemplate = new ToscaNodeTemplate();
75         nodeTemplate.setType(CONTROL_LOOP_ELEMENT);
76         assertThat(ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(nodeTemplate, serviceTemplate)).isTrue();
77
78         nodeTemplate.setType(POLICY_CONTROL_LOOP_ELEMENT);
79         assertThat(ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(nodeTemplate, serviceTemplate)).isTrue();
80
81         nodeTemplate.setType(PARTICIPANT_CONTROL_LOOP_ELEMENT);
82         assertThat(ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(nodeTemplate, serviceTemplate)).isFalse();
83     }
84 }