2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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 Map<EventHandlerPeeredMode, PeeredReference> peerReferenceMap = new EnumMap<>(EventHandlerPeeredMode.class);
62 public void init(final String producerName, final EventHandlerParameters producerParameters)
63 throws ApexEventException {
64 this.name = producerName;
66 // Check and get the Properties
67 if (!(producerParameters.getCarrierTechnologyParameters() instanceof DummyCarrierTechnologyParameters)) {
68 String message = "specified producer properties are not applicable to a dummy producer (" + this.name + ")";
70 throw new ApexEventException(message);
72 dummyProducerProperties =
73 (DummyCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
75 new File(dummyProducerProperties.getPropertyFileName()).delete();
82 public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) {
83 return peerReferenceMap.get(peeredMode);
90 public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {
91 peerReferenceMap.put(peeredMode, peeredReference);
98 public void sendEvent(final long executionId, final Properties executionProperties, final String eventName,
99 final Object eventAsJsonString) {
100 // Check if this is a synchronized event, if so we have received a reply
101 final SynchronousEventCache synchronousEventCache =
102 (SynchronousEventCache) peerReferenceMap.get(EventHandlerPeeredMode.SYNCHRONOUS);
103 if (synchronousEventCache != null) {
104 synchronousEventCache.removeCachedEventToApexIfExists(executionId);
107 RunTestEvent testEvent = new RunTestEvent();
109 testEvent.fromJson((String) eventAsJsonString);
110 } catch (CoderException ce) {
111 String message = "could not decode event from JSON";
112 LOGGER.warn(message, ce);
113 throw new ApexEventRuntimeException(message, ce);
115 if (!dummyProducerProperties.getTestToRun().equals(testEvent.getTestToRun())) {
116 String message = "tests in received test event and parameters do not match " + testEvent.getTestToRun()
117 + ":" + dummyProducerProperties.getTestToRun();
118 LOGGER.warn(message);
119 throw new ApexEventRuntimeException(message);
123 executionProperties.store(new FileOutputStream(new File(dummyProducerProperties.getPropertyFileName())),
125 } catch (IOException ioe) {
126 String message = "writing of executor properties for testing failed from file: "
127 + dummyProducerProperties.getPropertyFileName();
128 LOGGER.warn(message, ioe);
129 throw new ApexEventRuntimeException(message, ioe);