2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019, 2024 Nordix Foundation.
4 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.testsuites.integration.uservice.executionproperties;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.util.EnumMap;
29 import java.util.Properties;
31 import org.onap.policy.apex.service.engine.event.ApexEventException;
32 import org.onap.policy.apex.service.engine.event.ApexEventProducer;
33 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
34 import org.onap.policy.apex.service.engine.event.PeeredReference;
35 import org.onap.policy.apex.service.engine.event.SynchronousEventCache;
36 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
37 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
38 import org.onap.policy.common.utils.coder.CoderException;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Dummy Apex event producer for testing event properties.
45 * @author Liam Fallon (liam.fallon@est.tech)
47 public class DummyApexEventProducer implements ApexEventProducer {
48 // Get a reference to the logger
49 private static final Logger LOGGER = LoggerFactory.getLogger(DummyApexEventProducer.class);
51 // The parameters read from the parameter service
52 private DummyCarrierTechnologyParameters dummyProducerProperties;
54 // The name for this producer
56 private String name = null;
58 // The peer references for this event handler
59 private final Map<EventHandlerPeeredMode, PeeredReference> peerReferenceMap =
60 new EnumMap<>(EventHandlerPeeredMode.class);
63 public void init(final String producerName, final EventHandlerParameters producerParameters)
64 throws ApexEventException {
65 this.name = producerName;
67 // Check and get the Properties
68 if (!(producerParameters.getCarrierTechnologyParameters() instanceof DummyCarrierTechnologyParameters)) {
69 String message = "specified producer properties are not applicable to a dummy producer (" + this.name + ")";
71 throw new ApexEventException(message);
73 dummyProducerProperties =
74 (DummyCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
76 new File(dummyProducerProperties.getPropertyFileName()).delete();
83 public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) {
84 return peerReferenceMap.get(peeredMode);
91 public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {
92 peerReferenceMap.put(peeredMode, peeredReference);
99 public void sendEvent(final long executionId, final Properties executionProperties, final String eventName,
100 final Object eventAsJsonString) {
101 // Check if this is a synchronized event, if so we have received a reply
102 final SynchronousEventCache synchronousEventCache =
103 (SynchronousEventCache) peerReferenceMap.get(EventHandlerPeeredMode.SYNCHRONOUS);
104 if (synchronousEventCache != null) {
105 synchronousEventCache.removeCachedEventToApexIfExists(executionId);
108 RunTestEvent testEvent = new RunTestEvent();
110 testEvent.fromJson((String) eventAsJsonString);
111 } catch (CoderException ce) {
112 String message = "could not decode event from JSON";
113 LOGGER.warn(message, ce);
114 throw new ApexEventRuntimeException(message, ce);
116 if (!dummyProducerProperties.getTestToRun().equals(testEvent.getTestToRun())) {
117 String message = "tests in received test event and parameters do not match " + testEvent.getTestToRun()
118 + ":" + dummyProducerProperties.getTestToRun();
119 LOGGER.warn(message);
120 throw new ApexEventRuntimeException(message);
124 executionProperties.store(new FileOutputStream(dummyProducerProperties.getPropertyFileName()),
126 } catch (IOException ioe) {
127 String message = "writing of executor properties for testing failed from file: "
128 + dummyProducerProperties.getPropertyFileName();
129 LOGGER.warn(message, ioe);
130 throw new ApexEventRuntimeException(message, ioe);