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