a33c12f2e46cafdac34f22f67c1726ece568d42c
[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.models.acm.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.models.acm.messages.dmaap.participant.ParticipantMessageUtils.assertSerializable;
26 import static org.onap.policy.clamp.models.acm.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.models.acm.concepts.AcElementStatistics;
34 import org.onap.policy.clamp.models.acm.concepts.AcElementStatisticsList;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
36 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionInfo;
37 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
38 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionStatistics;
39 import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
40 import org.onap.policy.clamp.models.acm.concepts.ParticipantHealthStatus;
41 import org.onap.policy.clamp.models.acm.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 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
46
47 class ParticipantStatusTest {
48
49     @Test
50     void testCopyConstructor() throws CoderException {
51         assertThatThrownBy(() -> new ParticipantStatus(null)).isInstanceOf(NullPointerException.class);
52
53         final ParticipantStatus orig = new ParticipantStatus();
54
55         // verify with null values
56         assertEquals(removeVariableFields(orig.toString()),
57                 removeVariableFields(new ParticipantStatus(orig).toString()));
58
59         // verify with all values
60         ToscaConceptIdentifier id = new ToscaConceptIdentifier("id", "1.2.3");
61         orig.setAutomationCompositionId(id);
62         orig.setParticipantId(id);
63         ToscaConceptIdentifier type = new ToscaConceptIdentifier("type", "2.3.4");
64         orig.setParticipantType(type);
65         orig.setMessageId(UUID.randomUUID());
66         orig.setState(ParticipantState.ACTIVE);
67         orig.setHealthStatus(ParticipantHealthStatus.HEALTHY);
68         orig.setTimestamp(Instant.ofEpochMilli(3000));
69
70         AutomationCompositionInfo acInfo = getAutomationCompositionInfo(id);
71         orig.setAutomationCompositionInfoList(List.of(acInfo));
72
73         ParticipantDefinition participantDefinitionUpdate = new ParticipantDefinition();
74         participantDefinitionUpdate.setParticipantId(id);
75         participantDefinitionUpdate.setParticipantType(type);
76         AutomationCompositionElementDefinition acDefinition = getAcElementDefinition(id);
77         participantDefinitionUpdate.setAutomationCompositionElementDefinitionList(List.of(acDefinition));
78         orig.setParticipantDefinitionUpdates(List.of(participantDefinitionUpdate));
79
80         assertEquals(removeVariableFields(orig.toString()),
81                 removeVariableFields(new ParticipantStatus(orig).toString()));
82
83         assertSerializable(orig, ParticipantStatus.class);
84     }
85
86     private AutomationCompositionInfo getAutomationCompositionInfo(ToscaConceptIdentifier id) {
87         AutomationCompositionInfo acInfo = new AutomationCompositionInfo();
88         acInfo.setState(AutomationCompositionState.PASSIVE2RUNNING);
89         acInfo.setAutomationCompositionId(id);
90
91         AutomationCompositionStatistics acStatistics = new AutomationCompositionStatistics();
92         acStatistics.setAutomationCompositionId(id);
93         acStatistics.setAverageExecutionTime(12345);
94         acStatistics.setEventCount(12345);
95         acStatistics.setLastEnterTime(12345);
96         acStatistics.setLastExecutionTime(12345);
97         acStatistics.setLastStart(12345);
98         acStatistics.setTimeStamp(Instant.ofEpochMilli(3000));
99         acStatistics.setUpTime(12345);
100         AcElementStatisticsList acElementStatisticsList = new AcElementStatisticsList();
101         AcElementStatistics acElementStatistics = new AcElementStatistics();
102         acElementStatistics.setParticipantId(new ToscaConceptIdentifier("defName", "0.0.1"));
103         acElementStatistics.setTimeStamp(Instant.now());
104         acElementStatisticsList.setAcElementStatistics(List.of(acElementStatistics));
105         acStatistics.setAcElementStatisticsList(acElementStatisticsList);
106
107         acInfo.setAutomationCompositionStatistics(acStatistics);
108         return acInfo;
109     }
110
111     private AutomationCompositionElementDefinition getAcElementDefinition(ToscaConceptIdentifier id) {
112         ToscaNodeTemplate toscaNodeTemplate = new ToscaNodeTemplate();
113         toscaNodeTemplate.setName("nodeTemplate");
114         toscaNodeTemplate.setDerivedFrom("parentNodeTemplate");
115         toscaNodeTemplate.setDescription("Description of nodeTemplate");
116         toscaNodeTemplate.setVersion("1.2.3");
117
118         AutomationCompositionElementDefinition acDefinition = new AutomationCompositionElementDefinition();
119         acDefinition.setAcElementDefinitionId(id);
120         acDefinition.setAutomationCompositionElementToscaNodeTemplate(toscaNodeTemplate);
121
122         ToscaProperty property = new ToscaProperty();
123         property.setName("test");
124         property.setType("testType");
125         Map<String, ToscaProperty> commonPropertiesMap = Map.of("Prop1", property);
126         acDefinition.setCommonPropertiesMap(commonPropertiesMap);
127         return acDefinition;
128     }
129 }