ae20f4b7f1748aef499ea1671bb0c322941fcc72
[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.participant.intermediary.main.parameters;
22
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.TreeMap;
28 import org.mockito.Mockito;
29 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantMessagePublisher;
30 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ControlLoopHandler;
31 import org.onap.policy.clamp.controlloop.participant.intermediary.handler.DummyParticipantParameters;
32 import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantIntermediaryParameters;
33 import org.onap.policy.common.endpoints.event.comm.TopicSink;
34 import org.onap.policy.common.endpoints.parameters.TopicParameters;
35 import org.onap.policy.common.utils.coder.Coder;
36 import org.onap.policy.common.utils.coder.CoderException;
37 import org.onap.policy.common.utils.coder.StandardCoder;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
39
40 /**
41  * Class to hold/create all parameters for test cases.
42  */
43 public class CommonTestData {
44     public static final String PARTICIPANT_GROUP_NAME = "ControlLoopParticipantGroup";
45     public static final String DESCRIPTION = "Participant description";
46     public static final long TIME_INTERVAL = 2000;
47     public static final List<TopicParameters> TOPIC_PARAMS = Arrays.asList(getTopicParams());
48
49     public static final Coder CODER = new StandardCoder();
50     private static final Object lockit = new Object();
51
52     /**
53      * Get ParticipantIntermediaryParameters.
54      *
55      * @return ParticipantIntermediaryParameters
56      */
57     public ParticipantIntermediaryParameters getParticipantIntermediaryParameters() {
58         try {
59             return CODER.convert(getIntermediaryParametersMap(PARTICIPANT_GROUP_NAME),
60                     ParticipantIntermediaryParameters.class);
61         } catch (final CoderException e) {
62             throw new RuntimeException("cannot create ParticipantSimulatorParameters from map", e);
63         }
64     }
65
66     /**
67      * Get ParticipantParameters.
68      *
69      * @return ParticipantParameters
70      */
71     public static DummyParticipantParameters getParticipantParameters() {
72         try {
73             return CODER.convert(getParametersMap(PARTICIPANT_GROUP_NAME),
74                     DummyParticipantParameters.class);
75         } catch (final CoderException e) {
76             throw new RuntimeException("cannot create ParticipantSimulatorParameters from map", e);
77         }
78     }
79
80     /**
81      * Returns a property map for a Parameters map for test cases.
82      *
83      * @param name name of the parameters
84      * @return a property map suitable for constructing an object
85      */
86     public static Map<String, Object> getParametersMap(final String name) {
87         final Map<String, Object> map = new TreeMap<>();
88         map.put("intermediaryParameters", getIntermediaryParametersMap(name));
89         return map;
90     }
91
92     /**
93      * Returns a property map for a intermediaryParameters map for test cases.
94      *
95      * @param name name of the parameters
96      * @return a property map suitable for constructing an object
97      */
98     public static Map<String, Object> getIntermediaryParametersMap(final String name) {
99         final Map<String, Object> map = new TreeMap<>();
100         map.put("name", name);
101         map.put("participantId", getParticipantId());
102         map.put("description", DESCRIPTION);
103         map.put("participantType", getParticipantId());
104         map.put("reportingTimeIntervalMs", TIME_INTERVAL);
105         map.put("clampControlLoopTopics", getTopicParametersMap(false));
106
107         return map;
108     }
109
110     /**
111      * Returns a property map for a TopicParameters map for test cases.
112      *
113      * @param isEmpty boolean value to represent that object created should be empty or not
114      * @return a property map suitable for constructing an object
115      */
116     public static Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
117         final Map<String, Object> map = new TreeMap<>();
118         if (!isEmpty) {
119             map.put("topicSources", TOPIC_PARAMS);
120             map.put("topicSinks", TOPIC_PARAMS);
121         }
122         return map;
123     }
124
125     /**
126      * Returns topic parameters for test cases.
127      *
128      * @return topic parameters
129      */
130     public static TopicParameters getTopicParams() {
131         final TopicParameters topicParams = new TopicParameters();
132         topicParams.setTopic("POLICY-CLRUNTIME-PARTICIPANT");
133         topicParams.setTopicCommInfrastructure("dmaap");
134         topicParams.setServers(Arrays.asList("localhost"));
135         return topicParams;
136     }
137
138     /**
139      * Returns participantId for test cases.
140      *
141      * @return participant Id
142      */
143     public static ToscaConceptIdentifier getParticipantId() {
144         return new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.1");
145     }
146
147     /**
148      * Returns a participantMessagePublisher for MessageSender.
149      *
150      * @return participant Message Publisher
151      */
152     private ParticipantMessagePublisher getParticipantMessagePublisher() {
153         synchronized (lockit) {
154             ParticipantMessagePublisher participantMessagePublisher =
155                     new ParticipantMessagePublisher();
156             participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
157             return participantMessagePublisher;
158         }
159     }
160
161     /**
162      * Returns a mocked ControlLoopHandler for test cases.
163      *
164      * @return ControlLoopHandler
165      */
166     public ControlLoopHandler getMockControlLoopHandler() {
167         return new ControlLoopHandler(
168                 getParticipantParameters(),
169                 getParticipantMessagePublisher());
170     }
171 }