670f4991fded5af4070cead5e23306023f438a5a
[policy/apex-pdp.git] / testsuites / integration / integration-uservice-test / src / test / java / org / onap / policy / apex / testsuites / integration / uservice / executionproperties / DummyApexEventProducer.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.testsuites.integration.uservice.executionproperties;
22
23 import java.io.File;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.util.EnumMap;
27 import java.util.Map;
28 import java.util.Properties;
29
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;
40
41 /**
42  * Dummy Apex event producer for testing event properties.
43  *
44  * @author Liam Fallon (liam.fallon@est.tech)
45  */
46 public class DummyApexEventProducer implements ApexEventProducer {
47     // Get a reference to the logger
48     private static final Logger LOGGER = LoggerFactory.getLogger(DummyApexEventProducer.class);
49
50     // The parameters read from the parameter service
51     private DummyCarrierTechnologyParameters dummyProducerProperties;
52
53     // The name for this producer
54     private String name = null;
55
56     // The peer references for this event handler
57     private Map<EventHandlerPeeredMode, PeeredReference> peerReferenceMap = new EnumMap<>(EventHandlerPeeredMode.class);
58
59     @Override
60     public void init(final String producerName, final EventHandlerParameters producerParameters)
61             throws ApexEventException {
62         this.name = producerName;
63
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 + ")";
67             LOGGER.warn(message);
68             throw new ApexEventException(message);
69         }
70         dummyProducerProperties =
71                 (DummyCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
72
73         new File(dummyProducerProperties.getPropertyFileName()).delete();
74     }
75
76     /**
77      * {@inheritDoc}.
78      */
79     @Override
80     public String getName() {
81         return name;
82     }
83
84     /**
85      * {@inheritDoc}.
86      */
87     @Override
88     public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) {
89         return peerReferenceMap.get(peeredMode);
90     }
91
92     /**
93      * {@inheritDoc}.
94      */
95     @Override
96     public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {
97         peerReferenceMap.put(peeredMode, peeredReference);
98     }
99
100     /**
101      * {@inheritDoc}.
102      */
103     @Override
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);
111         }
112
113         RunTestEvent testEvent = new RunTestEvent();
114         try {
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);
120         }
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);
126         }
127
128         try {
129             executionProperties.store(new FileOutputStream(new File(dummyProducerProperties.getPropertyFileName())),
130                     "");
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);
136         }
137     }
138
139     /**
140      * {@inheritDoc}.
141      */
142     @Override
143     public void stop() {
144         // Not used
145     }
146 }