Use lombok in apex-pdp #5
[policy/apex-pdp.git] / testsuites / integration / integration-executor-test / src / test / java / org / onap / policy / apex / testsuites / integration / executor / engine / TestApexActionListener.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
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.executor.engine;
23
24 import java.util.ArrayList;
25 import java.util.List;
26 import lombok.Getter;
27 import org.onap.policy.apex.core.engine.engine.EnEventListener;
28 import org.onap.policy.apex.core.engine.event.EnEvent;
29 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
30 import org.slf4j.ext.XLogger;
31 import org.slf4j.ext.XLoggerFactory;
32
33 /**
34  * The listener interface for receiving testApexAction events. The class that is interested in
35  * processing a testApexAction event implements this interface, and the object created with that
36  * class is registered with a component using the component's <code>addTestApexActionListener</code>
37  * method. When the testApexAction event occurs, that object's appropriate method is invoked.
38  *
39  * @author Liam Fallon (liam.fallon@ericsson.com)
40  */
41 public class TestApexActionListener implements EnEventListener {
42     private static final XLogger logger = XLoggerFactory.getXLogger(TestApexActionListener.class);
43
44     private List<EnEvent> resultEvents = new ArrayList<>();
45
46     @Getter
47     private final String id;
48
49     /**
50      * Instantiates a new test apex action listener.
51      *
52      * @param id the id
53      */
54     public TestApexActionListener(final String id) {
55         this.id = id;
56     }
57
58     /**
59      * Gets the result.
60      *
61      * @param allowNulls if true and the returned event is null, then return, otherwise wait until
62      *        an event is returned.
63      * @return the result
64      */
65     public EnEvent getResult(final boolean allowNulls) {
66         EnEvent result = null;
67         while (true) {
68             while (resultEvents.isEmpty()) {
69                 ThreadUtilities.sleep(100);
70             }
71             result = resultEvents.remove(0);
72             if (result != null || allowNulls) {
73                 break;
74             }
75         }
76         return result;
77     }
78
79     /**
80      * {@inheritDoc}.
81      */
82     @Override
83     public void onEnEvent(final EnEvent actionEvent) {
84         ThreadUtilities.sleep(100);
85         if (actionEvent != null) {
86             logger.info("Action event from engine: {}", actionEvent.getName());
87         }
88         resultEvents.add(actionEvent);
89     }
90 }