2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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.apex.testsuites.integration.uservice.executionproperties;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.util.EnumMap;
28 import java.util.Properties;
30 import org.onap.policy.apex.service.engine.event.ApexEventException;
31 import org.onap.policy.apex.service.engine.event.ApexEventProducer;
32 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
33 import org.onap.policy.apex.service.engine.event.PeeredReference;
34 import org.onap.policy.apex.service.engine.event.SynchronousEventCache;
35 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
36 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
37 import org.onap.policy.common.utils.coder.CoderException;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * Dummy Apex event producer for testing event properties.
44 * @author Liam Fallon (liam.fallon@est.tech)
46 public class DummyApexEventProducer implements ApexEventProducer {
47 // Get a reference to the logger
48 private static final Logger LOGGER = LoggerFactory.getLogger(DummyApexEventProducer.class);
50 // The parameters read from the parameter service
51 private DummyCarrierTechnologyParameters dummyProducerProperties;
53 // The name for this producer
54 private String name = null;
56 // The peer references for this event handler
57 private Map<EventHandlerPeeredMode, PeeredReference> peerReferenceMap = new EnumMap<>(EventHandlerPeeredMode.class);
60 public void init(final String producerName, final EventHandlerParameters producerParameters)
61 throws ApexEventException {
62 this.name = producerName;
64 // Check and get the Properties
65 if (!(producerParameters.getCarrierTechnologyParameters() instanceof DummyCarrierTechnologyParameters)) {
66 String message = "specified producer properties are not applicable to a dummy producer (" + this.name + ")";
68 throw new ApexEventException(message);
70 dummyProducerProperties =
71 (DummyCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
73 new File(dummyProducerProperties.getPropertyFileName()).delete();
80 public String getName() {
88 public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) {
89 return peerReferenceMap.get(peeredMode);
96 public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {
97 peerReferenceMap.put(peeredMode, peeredReference);
104 public void sendEvent(final long executionId, final Properties executionProperties, final String eventName,
105 final Object eventAsJsonString) {
106 // Check if this is a synchronized event, if so we have received a reply
107 final SynchronousEventCache synchronousEventCache =
108 (SynchronousEventCache) peerReferenceMap.get(EventHandlerPeeredMode.SYNCHRONOUS);
109 if (synchronousEventCache != null) {
110 synchronousEventCache.removeCachedEventToApexIfExists(executionId);
113 RunTestEvent testEvent = new RunTestEvent();
115 testEvent.fromJson((String) eventAsJsonString);
116 } catch (CoderException ce) {
117 String message = "could not decode event from JSON";
118 LOGGER.warn(message, ce);
119 throw new ApexEventRuntimeException(message, ce);
121 if (!dummyProducerProperties.getTestToRun().equals(testEvent.getTestToRun())) {
122 String message = "tests in received test event and parameters do not match " + testEvent.getTestToRun()
123 + ":" + dummyProducerProperties.getTestToRun();
124 LOGGER.warn(message);
125 throw new ApexEventRuntimeException(message);
129 executionProperties.store(new FileOutputStream(new File(dummyProducerProperties.getPropertyFileName())),
131 } catch (IOException ioe) {
132 String message = "writing of executor properties for testing failed from file: "
133 + dummyProducerProperties.getPropertyFileName();
134 LOGGER.warn(message, ioe);
135 throw new ApexEventRuntimeException(message, ioe);