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