7c163870ea05f130fe6ae1bde6f1e09bce77c24d
[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.messages.dmaap.participant;
22
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessageUtils.assertSerializable;
26 import static org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessageUtils.removeVariableFields;
27
28 import java.time.Instant;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.UUID;
32 import org.junit.jupiter.api.Test;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatisticsList;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopInfo;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopStatistics;
39 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition;
40 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantHealthStatus;
41 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState;
42 import org.onap.policy.common.utils.coder.CoderException;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
45
46 class ParticipantStatusTest {
47
48     @Test
49     void testCopyConstructor() throws CoderException {
50         assertThatThrownBy(() -> new ParticipantStatus(null)).isInstanceOf(NullPointerException.class);
51
52         final ParticipantStatus orig = new ParticipantStatus();
53
54         // verify with null values
55         assertEquals(removeVariableFields(orig.toString()),
56                 removeVariableFields(new ParticipantStatus(orig).toString()));
57
58         // verify with all values
59         ToscaConceptIdentifier id = new ToscaConceptIdentifier("id", "1.2.3");
60         orig.setControlLoopId(id);
61         orig.setParticipantId(id);
62         orig.setParticipantType(id);
63         orig.setMessageId(UUID.randomUUID());
64         orig.setState(ParticipantState.ACTIVE);
65         orig.setHealthStatus(ParticipantHealthStatus.HEALTHY);
66         orig.setTimestamp(Instant.ofEpochMilli(3000));
67
68         ControlLoopInfo clInfo = getControlLoopInfo(id);
69         orig.setControlLoopInfoList(List.of(clInfo));
70
71         ParticipantDefinition participantDefinitionUpdate = new ParticipantDefinition();
72         participantDefinitionUpdate.setParticipantId(id);
73         ControlLoopElementDefinition clDefinition = getClElementDefinition(id);
74         participantDefinitionUpdate.setControlLoopElementDefinitionList(List.of(clDefinition));
75         orig.setParticipantDefinitionUpdates(List.of(participantDefinitionUpdate));
76
77         assertEquals(removeVariableFields(orig.toString()),
78                 removeVariableFields(new ParticipantStatus(orig).toString()));
79
80         assertSerializable(orig, ParticipantStatus.class);
81     }
82
83     private ControlLoopInfo getControlLoopInfo(ToscaConceptIdentifier id) {
84         ControlLoopInfo clInfo = new ControlLoopInfo();
85         clInfo.setState(ControlLoopState.PASSIVE2RUNNING);
86         clInfo.setControlLoopId(id);
87
88         ControlLoopStatistics clStatistics = new ControlLoopStatistics();
89         clStatistics.setControlLoopId(id);
90         clStatistics.setAverageExecutionTime(12345);
91         clStatistics.setEventCount(12345);
92         clStatistics.setLastEnterTime(12345);
93         clStatistics.setLastExecutionTime(12345);
94         clStatistics.setLastStart(12345);
95         clStatistics.setTimeStamp(Instant.ofEpochMilli(3000));
96         clStatistics.setUpTime(12345);
97         ClElementStatisticsList clElementStatisticsList = new ClElementStatisticsList();
98         ClElementStatistics clElementStatistics = new ClElementStatistics();
99         clElementStatistics.setParticipantId(new ToscaConceptIdentifier("defName", "0.0.1"));
100         clElementStatistics.setTimeStamp(Instant.now());
101         clElementStatisticsList.setClElementStatistics(List.of(clElementStatistics));
102         clStatistics.setClElementStatisticsList(clElementStatisticsList);
103
104         clInfo.setControlLoopStatistics(clStatistics);
105         return clInfo;
106     }
107
108     private ControlLoopElementDefinition getClElementDefinition(ToscaConceptIdentifier id) {
109         ToscaNodeTemplate toscaNodeTemplate = new ToscaNodeTemplate();
110         toscaNodeTemplate.setName("nodeTemplate");
111         toscaNodeTemplate.setDerivedFrom("parentNodeTemplate");
112         toscaNodeTemplate.setDescription("Description of nodeTemplate");
113         toscaNodeTemplate.setVersion("1.2.3");
114
115         ControlLoopElementDefinition clDefinition = new ControlLoopElementDefinition();
116         clDefinition.setClElementDefinitionId(id);
117         clDefinition.setControlLoopElementToscaNodeTemplate(toscaNodeTemplate);
118         Map<String, String> commonPropertiesMap = Map.of("Prop1", "PropValue");
119         clDefinition.setCommonPropertiesMap(commonPropertiesMap);
120         return clDefinition;
121     }
122 }