Convert junit4 to junit5
[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, 2024 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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.testsuites.integration.uservice.executionproperties;
23
24 import java.io.File;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.util.EnumMap;
28 import java.util.Map;
29 import java.util.Properties;
30 import lombok.Getter;
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;
41
42 /**
43  * Dummy Apex event producer for testing event properties.
44  *
45  * @author Liam Fallon (liam.fallon@est.tech)
46  */
47 public class DummyApexEventProducer implements ApexEventProducer {
48     // Get a reference to the logger
49     private static final Logger LOGGER = LoggerFactory.getLogger(DummyApexEventProducer.class);
50
51     // The parameters read from the parameter service
52     private DummyCarrierTechnologyParameters dummyProducerProperties;
53
54     // The name for this producer
55     @Getter
56     private String name = null;
57
58     // The peer references for this event handler
59     private final Map<EventHandlerPeeredMode, PeeredReference> peerReferenceMap =
60         new EnumMap<>(EventHandlerPeeredMode.class);
61
62     @Override
63     public void init(final String producerName, final EventHandlerParameters producerParameters)
64         throws ApexEventException {
65         this.name = producerName;
66
67         // Check and get the Properties
68         if (!(producerParameters.getCarrierTechnologyParameters() instanceof DummyCarrierTechnologyParameters)) {
69             String message = "specified producer properties are not applicable to a dummy producer (" + this.name + ")";
70             LOGGER.warn(message);
71             throw new ApexEventException(message);
72         }
73         dummyProducerProperties =
74             (DummyCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
75
76         new File(dummyProducerProperties.getPropertyFileName()).delete();
77     }
78
79     /**
80      * {@inheritDoc}.
81      */
82     @Override
83     public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) {
84         return peerReferenceMap.get(peeredMode);
85     }
86
87     /**
88      * {@inheritDoc}.
89      */
90     @Override
91     public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {
92         peerReferenceMap.put(peeredMode, peeredReference);
93     }
94
95     /**
96      * {@inheritDoc}.
97      */
98     @Override
99     public void sendEvent(final long executionId, final Properties executionProperties, final String eventName,
100                           final Object eventAsJsonString) {
101         // Check if this is a synchronized event, if so we have received a reply
102         final SynchronousEventCache synchronousEventCache =
103             (SynchronousEventCache) peerReferenceMap.get(EventHandlerPeeredMode.SYNCHRONOUS);
104         if (synchronousEventCache != null) {
105             synchronousEventCache.removeCachedEventToApexIfExists(executionId);
106         }
107
108         RunTestEvent testEvent = new RunTestEvent();
109         try {
110             testEvent.fromJson((String) eventAsJsonString);
111         } catch (CoderException ce) {
112             String message = "could not decode event from JSON";
113             LOGGER.warn(message, ce);
114             throw new ApexEventRuntimeException(message, ce);
115         }
116         if (!dummyProducerProperties.getTestToRun().equals(testEvent.getTestToRun())) {
117             String message = "tests in received test event and parameters do not match " + testEvent.getTestToRun()
118                 + ":" + dummyProducerProperties.getTestToRun();
119             LOGGER.warn(message);
120             throw new ApexEventRuntimeException(message);
121         }
122
123         try {
124             executionProperties.store(new FileOutputStream(dummyProducerProperties.getPropertyFileName()),
125                 "");
126         } catch (IOException ioe) {
127             String message = "writing of executor properties for testing failed from file: "
128                 + dummyProducerProperties.getPropertyFileName();
129             LOGGER.warn(message, ioe);
130             throw new ApexEventRuntimeException(message, ioe);
131         }
132     }
133
134     /**
135      * {@inheritDoc}.
136      */
137     @Override
138     public void stop() {
139         // Not used
140     }
141 }