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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.participant.intermediary.main.parameters;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.List;
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;
43 * Class to hold/create all parameters for test cases.
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();
54 * Get ParticipantIntermediaryParameters.
56 * @return ParticipantIntermediaryParameters
58 public ParticipantIntermediaryParameters getParticipantIntermediaryParameters() {
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);
68 * Get ParticipantParameters.
70 * @return ParticipantParameters
72 public static DummyParticipantParameters getParticipantParameters() {
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);
82 * Returns a property map for a Parameters map for test cases.
84 * @param name name of the parameters
85 * @return a property map suitable for constructing an object
87 public static Map<String, Object> getParametersMap(final String name) {
88 final Map<String, Object> map = new TreeMap<>();
89 map.put("intermediaryParameters", getIntermediaryParametersMap(name));
94 * Returns a property map for a intermediaryParameters map for test cases.
96 * @param name name of the parameters
97 * @return a property map suitable for constructing an object
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));
112 * Returns a property map for a TopicParameters map for test cases.
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
117 public static Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
118 final Map<String, Object> map = new TreeMap<>();
120 map.put("topicSources", TOPIC_PARAMS);
121 map.put("topicSinks", TOPIC_PARAMS);
127 * Returns topic parameters for test cases.
129 * @return topic parameters
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"));
140 * Returns participantId for test cases.
142 * @return participant Id
144 public static ToscaConceptIdentifier getParticipantId() {
145 return new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.1");
149 * Returns a participantMessagePublisher for MessageSender.
151 * @return participant Message Publisher
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;
163 * Returns a mocked ControlLoopHandler for test cases.
165 * @return ControlLoopHandler
167 public ControlLoopHandler getMockControlLoopHandler() {
168 return new ControlLoopHandler(
169 getParticipantParameters(),
170 getParticipantMessagePublisher());
174 * Returns a mocked ParticipantHandler for test cases.
176 * @return participant Handler
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;