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