2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2021-2022 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.acm.participant.intermediary.main.parameters;
 
  24 import java.util.Collections;
 
  25 import java.util.LinkedHashMap;
 
  26 import java.util.List;
 
  28 import java.util.TreeMap;
 
  29 import java.util.UUID;
 
  30 import org.mockito.Mockito;
 
  31 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
 
  32 import org.onap.policy.clamp.acm.participant.intermediary.handler.AutomationCompositionHandler;
 
  33 import org.onap.policy.clamp.acm.participant.intermediary.handler.DummyParticipantParameters;
 
  34 import org.onap.policy.clamp.acm.participant.intermediary.handler.ParticipantHandler;
 
  35 import org.onap.policy.clamp.acm.participant.intermediary.parameters.ParticipantIntermediaryParameters;
 
  36 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
 
  37 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
 
  38 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
 
  39 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
 
  40 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregisterAck;
 
  41 import org.onap.policy.common.endpoints.event.comm.TopicSink;
 
  42 import org.onap.policy.common.endpoints.parameters.TopicParameters;
 
  43 import org.onap.policy.common.utils.coder.Coder;
 
  44 import org.onap.policy.common.utils.coder.CoderException;
 
  45 import org.onap.policy.common.utils.coder.StandardCoder;
 
  46 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
 
  49  * Class to hold/create all parameters for test cases.
 
  51 public class CommonTestData {
 
  52     public static final String PARTICIPANT_GROUP_NAME = "AutomationCompositionParticipantGroup";
 
  53     public static final String DESCRIPTION = "Participant description";
 
  54     public static final long TIME_INTERVAL = 2000;
 
  55     public static final List<TopicParameters> TOPIC_PARAMS = List.of(getTopicParams());
 
  56     public static final Coder CODER = new StandardCoder();
 
  57     private static final Object lockit = new Object();
 
  58     public static final UUID AC_ID_0 = UUID.randomUUID();
 
  59     public static final UUID AC_ID_1 = UUID.randomUUID();
 
  62      * Get ParticipantIntermediaryParameters.
 
  64      * @return ParticipantIntermediaryParameters
 
  66     public ParticipantIntermediaryParameters getParticipantIntermediaryParameters() {
 
  68             return CODER.convert(getIntermediaryParametersMap(PARTICIPANT_GROUP_NAME),
 
  69                     ParticipantIntermediaryParameters.class);
 
  70         } catch (final CoderException e) {
 
  71             throw new RuntimeException("cannot create ParticipantSimulatorParameters from map", e);
 
  76      * Get ParticipantParameters.
 
  78      * @return ParticipantParameters
 
  80     public static DummyParticipantParameters getParticipantParameters() {
 
  82             return CODER.convert(getParametersMap(PARTICIPANT_GROUP_NAME), DummyParticipantParameters.class);
 
  83         } catch (final CoderException e) {
 
  84             throw new RuntimeException("cannot create ParticipantSimulatorParameters from map", e);
 
  89      * Returns a property map for a Parameters map for test cases.
 
  91      * @param name name of the parameters
 
  92      * @return a property map suitable for constructing an object
 
  94     public static Map<String, Object> getParametersMap(final String name) {
 
  95         final Map<String, Object> map = new TreeMap<>();
 
  96         map.put("intermediaryParameters", getIntermediaryParametersMap(name));
 
 101      * Returns a property map for a intermediaryParameters map for test cases.
 
 103      * @param name name of the parameters
 
 104      * @return a property map suitable for constructing an object
 
 106     public static Map<String, Object> getIntermediaryParametersMap(final String name) {
 
 107         final Map<String, Object> map = new TreeMap<>();
 
 108         map.put("name", name);
 
 109         map.put("participantId", getParticipantId());
 
 110         map.put("description", DESCRIPTION);
 
 111         map.put("participantType", getParticipantId());
 
 112         map.put("reportingTimeIntervalMs", TIME_INTERVAL);
 
 113         map.put("clampAutomationCompositionTopics", getTopicParametersMap(false));
 
 119      * Returns a property map for a TopicParameters map for test cases.
 
 121      * @param isEmpty boolean value to represent that object created should be empty or not
 
 122      * @return a property map suitable for constructing an object
 
 124     public static Map<String, Object> getTopicParametersMap(final boolean isEmpty) {
 
 125         final Map<String, Object> map = new TreeMap<>();
 
 127             map.put("topicSources", TOPIC_PARAMS);
 
 128             map.put("topicSinks", TOPIC_PARAMS);
 
 134      * Returns topic parameters for test cases.
 
 136      * @return topic parameters
 
 138     public static TopicParameters getTopicParams() {
 
 139         final var topicParams = new TopicParameters();
 
 140         topicParams.setTopic("POLICY-ACRUNTIME-PARTICIPANT");
 
 141         topicParams.setTopicCommInfrastructure("dmaap");
 
 142         topicParams.setServers(List.of("localhost"));
 
 147      * Returns participantId for test cases.
 
 149      * @return participant Id
 
 151     public static ToscaConceptIdentifier getParticipantId() {
 
 152         return new ToscaConceptIdentifier("org.onap.PM_CDS_Blueprint", "1.0.1");
 
 156      * Returns a participantMessagePublisher for MessageSender.
 
 158      * @return participant Message Publisher
 
 160     private ParticipantMessagePublisher getParticipantMessagePublisher() {
 
 161         synchronized (lockit) {
 
 162             var participantMessagePublisher = new ParticipantMessagePublisher();
 
 163             participantMessagePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
 
 164             return participantMessagePublisher;
 
 169      * Returns a mocked AutomationCompositionHandler for test cases.
 
 171      * @return AutomationCompositionHandler
 
 173     public AutomationCompositionHandler getMockAutomationCompositionHandler() {
 
 174         return new AutomationCompositionHandler(getParticipantParameters(), getParticipantMessagePublisher());
 
 178      * Returns a mocked ParticipantHandler for test cases.
 
 180      * @return participant Handler
 
 182     public ParticipantHandler getMockParticipantHandler() {
 
 183         var parameters = getParticipantParameters();
 
 184         var automationCompositionHandler = getMockAutomationCompositionHandler();
 
 185         var publisher = new ParticipantMessagePublisher();
 
 186         publisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
 
 187         return new ParticipantHandler(parameters, publisher, automationCompositionHandler);
 
 191      * Returns a mocked ParticipantHandler for test cases.
 
 193      * @return participant Handler
 
 195      * @throws CoderException if there is an error with .json file.
 
 197     public ParticipantHandler getParticipantHandlerAutomationCompositions() throws CoderException {
 
 198         var automationCompositionHandler = Mockito.mock(AutomationCompositionHandler.class);
 
 199         Mockito.doReturn(getTestAutomationCompositions()).when(automationCompositionHandler)
 
 200                 .getAutomationCompositions();
 
 201         Mockito.doReturn(getTestAutomationCompositionMap()).when(automationCompositionHandler)
 
 202                 .getAutomationCompositionMap();
 
 203         var publisher = new ParticipantMessagePublisher();
 
 204         publisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
 
 205         var parameters = getParticipantParameters();
 
 206         var participantHandler = new ParticipantHandler(parameters, publisher, automationCompositionHandler);
 
 207         participantHandler.sendParticipantRegister();
 
 208         participantHandler.handleParticipantStatusReq(null);
 
 209         participantHandler.sendParticipantDeregister();
 
 210         var participantDeregisterAckMsg = new ParticipantDeregisterAck();
 
 211         participantDeregisterAckMsg.setResponseTo(UUID.randomUUID());
 
 212         participantHandler.handleParticipantDeregisterAck(participantDeregisterAckMsg);
 
 213         return participantHandler;
 
 217      * Returns a Map of ToscaConceptIdentifier and AutomationComposition for test cases.
 
 219      * @return automationCompositionMap
 
 221      * @throws CoderException if there is an error with .json file.
 
 223     public Map<UUID, AutomationComposition> getTestAutomationCompositionMap() throws CoderException {
 
 224         var automationCompositions = getTestAutomationCompositions();
 
 225         var automationComposition = automationCompositions.getAutomationCompositionList().get(1);
 
 226         Map<UUID, AutomationComposition> automationCompositionMap = new LinkedHashMap<>();
 
 227         automationCompositionMap.put(automationComposition.getInstanceId(), automationComposition);
 
 228         return automationCompositionMap;
 
 232      * Returns List of AutomationComposition for test cases.
 
 234      * @return AutomationCompositions
 
 236      * @throws CoderException if there is an error with .json file.
 
 238     public AutomationCompositions getTestAutomationCompositions() throws CoderException {
 
 239         var automationCompositions = new StandardCoder().decode(
 
 240                 new File("src/test/resources/providers/TestAutomationCompositions.json"), AutomationCompositions.class);
 
 241         automationCompositions.getAutomationCompositionList().get(1).setInstanceId(AC_ID_0);
 
 242         automationCompositions.getAutomationCompositionList().get(1).setInstanceId(AC_ID_1);
 
 243         return automationCompositions;
 
 247      * Returns a map for a elementsOnThisParticipant for test cases.
 
 249      * @param uuid UUID and id ToscaConceptIdentifier
 
 250      * @return a map suitable for elementsOnThisParticipant
 
 252     public Map<UUID, AutomationCompositionElement> setAutomationCompositionElementTest(UUID uuid,
 
 253             ToscaConceptIdentifier id) {
 
 254         var acElement = new AutomationCompositionElement();
 
 255         acElement.setId(uuid);
 
 256         acElement.setParticipantId(id);
 
 257         acElement.setDefinition(id);
 
 258         acElement.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
 
 260         Map<UUID, AutomationCompositionElement> elementsOnThisParticipant = new LinkedHashMap<>();
 
 261         elementsOnThisParticipant.put(uuid, acElement);
 
 262         return elementsOnThisParticipant;
 
 266      * Returns a AutomationCompositionHandler with elements on the id,uuid.
 
 268      * @param id ToscaConceptIdentifier and uuid UUID
 
 269      * @return a AutomationCompositionHander with elements
 
 271     public AutomationCompositionHandler setTestAutomationCompositionHandler(ToscaConceptIdentifier id, UUID uuid)
 
 272             throws CoderException {
 
 273         var ach = getMockAutomationCompositionHandler();
 
 275         var key = getTestAutomationCompositionMap().keySet().iterator().next();
 
 276         var value = getTestAutomationCompositionMap().get(key);
 
 277         ach.getAutomationCompositionMap().put(key, value);
 
 279         var keyElem = setAutomationCompositionElementTest(uuid, id).keySet().iterator().next();
 
 280         var valueElem = setAutomationCompositionElementTest(uuid, id).get(keyElem);
 
 281         ach.getElementsOnThisParticipant().put(keyElem, valueElem);