2860f17368c33dfe024e6cc15aa33f73fa1829e6
[policy/apex-pdp.git] /
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      * (non-Javadoc)
79      * 
80      * @see
81      * org.onap.policy.apex.core.engine.engine.EnEventListener#onEnEvent(org.onap.policy.apex.core.
82      * engine.event.EnEvent)
83      */
84     @Override
85     public void onEnEvent(final EnEvent actionEvent) {
86         ThreadUtilities.sleep(100);
87         if (actionEvent != null) {
88             logger.info("Action event from engine: {}", actionEvent.getName());
89         }
90         resultEvents.add(actionEvent);
91     }
92
93     /**
94      * Gets the id.
95      *
96      * @return the id
97      */
98     public String getId() {
99         return id;
100     }
101 }