Changes for checkstyle 8.32
[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 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;
39
40 /**
41  * Dummy Apex event producer for testing event properties.
42  *
43  * @author Liam Fallon (liam.fallon@est.tech)
44  */
45 public class DummyApexEventProducer implements ApexEventProducer {
46     // Get a reference to the logger
47     private static final Logger LOGGER = LoggerFactory.getLogger(DummyApexEventProducer.class);
48
49     // The parameters read from the parameter service
50     private DummyCarrierTechnologyParameters dummyProducerProperties;
51
52     // The name for this producer
53     private String name = null;
54
55     // The peer references for this event handler
56     private Map<EventHandlerPeeredMode, PeeredReference> peerReferenceMap = new EnumMap<>(EventHandlerPeeredMode.class);
57
58     @Override
59     public void init(final String producerName, final EventHandlerParameters producerParameters)
60             throws ApexEventException {
61         this.name = producerName;
62
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 + ")";
66             LOGGER.warn(message);
67             throw new ApexEventException(message);
68         }
69         dummyProducerProperties =
70                 (DummyCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
71
72         new File(dummyProducerProperties.getPropertyFileName()).delete();
73     }
74
75     /**
76      * {@inheritDoc}.
77      */
78     @Override
79     public String getName() {
80         return name;
81     }
82
83     /**
84      * {@inheritDoc}.
85      */
86     @Override
87     public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) {
88         return peerReferenceMap.get(peeredMode);
89     }
90
91     /**
92      * {@inheritDoc}.
93      */
94     @Override
95     public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {
96         peerReferenceMap.put(peeredMode, peeredReference);
97     }
98
99     /**
100      * {@inheritDoc}.
101      */
102     @Override
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);
110         }
111
112         RunTestEvent testEvent = new RunTestEvent();
113         try {
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);
119         }
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);
125         }
126
127         try {
128             executionProperties.store(new FileOutputStream(new File(dummyProducerProperties.getPropertyFileName())),
129                     "");
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);
135         }
136     }
137
138     /**
139      * {@inheritDoc}.
140      */
141     @Override
142     public void stop() {
143         // Not used
144     }
145 }