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;
29 import org.onap.policy.apex.service.engine.event.ApexEventException;
30 import org.onap.policy.apex.service.engine.event.ApexEventProducer;
31 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
32 import org.onap.policy.apex.service.engine.event.PeeredReference;
33 import org.onap.policy.apex.service.engine.event.SynchronousEventCache;
34 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
35 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
36 import org.onap.policy.common.utils.coder.CoderException;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * Dummy Apex event producer for testing event properties.
43 * @author Liam Fallon (liam.fallon@est.tech)
45 public class DummyApexEventProducer implements ApexEventProducer {
46 // Get a reference to the logger
47 private static final Logger LOGGER = LoggerFactory.getLogger(DummyApexEventProducer.class);
49 // The parameters read from the parameter service
50 private DummyCarrierTechnologyParameters dummyProducerProperties;
52 // The name for this producer
53 private String name = null;
55 // The peer references for this event handler
56 private Map<EventHandlerPeeredMode, PeeredReference> peerReferenceMap = new EnumMap<>(EventHandlerPeeredMode.class);
59 public void init(final String producerName, final EventHandlerParameters producerParameters)
60 throws ApexEventException {
61 this.name = producerName;
63 // Check and get the Properties
64 if (!(producerParameters.getCarrierTechnologyParameters() instanceof DummyCarrierTechnologyParameters)) {
65 String message = "specified producer properties are not applicable to a dummy producer (" + this.name + ")";
67 throw new ApexEventException(message);
69 dummyProducerProperties =
70 (DummyCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
72 new File(dummyProducerProperties.getPropertyFileName()).delete();
79 public String getName() {
87 public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) {
88 return peerReferenceMap.get(peeredMode);
95 public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {
96 peerReferenceMap.put(peeredMode, peeredReference);
103 public void sendEvent(final long executionId, final Properties executionProperties, final String eventName,
104 final Object eventAsJsonString) {
105 // Check if this is a synchronized event, if so we have received a reply
106 final SynchronousEventCache synchronousEventCache =
107 (SynchronousEventCache) peerReferenceMap.get(EventHandlerPeeredMode.SYNCHRONOUS);
108 if (synchronousEventCache != null) {
109 synchronousEventCache.removeCachedEventToApexIfExists(executionId);
112 RunTestEvent testEvent = new RunTestEvent();
114 testEvent.fromJson((String) eventAsJsonString);
115 } catch (CoderException ce) {
116 String message = "could not decode event from JSON";
117 LOGGER.warn(message, ce);
118 throw new ApexEventRuntimeException(message, ce);
120 if (!dummyProducerProperties.getTestToRun().equals(testEvent.getTestToRun())) {
121 String message = "tests in received test event and parameters do not match " + testEvent.getTestToRun()
122 + ":" + dummyProducerProperties.getTestToRun();
123 LOGGER.warn(message);
124 throw new ApexEventRuntimeException(message);
128 executionProperties.store(new FileOutputStream(new File(dummyProducerProperties.getPropertyFileName())),
130 } catch (IOException ioe) {
131 String message = "writing of executor properties for testing failed from file: "
132 + dummyProducerProperties.getPropertyFileName();
133 LOGGER.warn(message, ioe);
134 throw new ApexEventRuntimeException(message, ioe);