Changes for checkstyle 8.32
[policy/apex-pdp.git] / examples / examples-adaptive / src / test / java / org / onap / policy / apex / examples / adaptive / TestApexActionListener.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation.
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.examples.adaptive;
23
24 import static org.awaitility.Awaitility.await;
25
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.concurrent.TimeUnit;
29 import org.onap.policy.apex.core.engine.engine.EnEventListener;
30 import org.onap.policy.apex.core.engine.event.EnEvent;
31
32 /**
33  * The listener interface for receiving testApexAction events. The class that is interested in processing a
34  * testApexAction event implements this interface, and the object created with that class is registered with a component
35  * using the component's <code>addTestApexActionListener</code> method. When the testApexAction event occurs, that
36  * object's appropriate method is invoked.
37  *
38  * @author Liam Fallon (liam.fallon@ericsson.com)
39  */
40 public class TestApexActionListener implements EnEventListener {
41     private List<EnEvent> resultEvents = new ArrayList<>();
42
43     private final String id;
44
45     /**
46      * Instantiates a new test apex action listener.
47      *
48      * @param id the id
49      */
50     public TestApexActionListener(final String id) {
51         this.id = id;
52     }
53
54     /**
55      * Gets the result.
56      *
57      * @return the result
58      */
59     public EnEvent getResult() {
60         await().atLeast(100, TimeUnit.MILLISECONDS).until(() -> !resultEvents.isEmpty());
61         return resultEvents.remove(0);
62     }
63
64     /**
65      * {@inheritDoc}.
66      */
67     @Override
68     public void onEnEvent(final EnEvent actionEvent) {
69         if (actionEvent != null) {
70             System.out.println("Action event from engine:" + actionEvent.getName());
71             resultEvents.add(actionEvent);
72         }
73     }
74
75     /**
76      * Gets the id.
77      *
78      * @return the id
79      */
80     public String getId() {
81         return id;
82     }
83 }