44c4306b9a6cfcbf5d9cfb09406e7b25711fb252
[policy/apex-pdp.git] / examples / examples-myfirstpolicy / src / test / java / org / onap / policy / apex / examples / myfirstpolicy / TestSaleAuthListener.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.myfirstpolicy;
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
30 import org.onap.policy.apex.core.engine.engine.EnEventListener;
31 import org.onap.policy.apex.core.engine.event.EnEvent;
32
33 /**
34  * The listener interface for receiving SaleAuth events. The class that is interested in processing a SaleAuth event
35  * implements this interface, and the object created with that class is registered with a component using the
36  * component's <code>addTestApexActionListener</code> method. When the testApexAction event occurs, that object's
37  * appropriate method is invoked.
38  *
39  * @author Liam Fallon (liam.fallon@ericsson.com)
40  */
41 public class TestSaleAuthListener implements EnEventListener {
42     // CHECKSTYLE:OFF: MagicNumber
43
44     private final List<EnEvent> resultEvents = new ArrayList<>();
45
46     private final String id;
47
48     /**
49      * Instantiates a new action listener.
50      *
51      * @param id the id
52      */
53     public TestSaleAuthListener(final String id) {
54         this.id = id;
55     }
56
57     /**
58      * Gets the result.
59      *
60      * @return the result
61      */
62     public EnEvent getResult() {
63         await().atMost(200, TimeUnit.MILLISECONDS).until(() -> !resultEvents.isEmpty());
64         return resultEvents.remove(0);
65     }
66
67     /**
68      * {@inheritDoc}.
69      */
70     @Override
71     public void onEnEvent(final EnEvent saleauthEvent) {
72         if (saleauthEvent != null) {
73             System.out.println("SaleAuth event from engine:" + saleauthEvent.getName());
74             resultEvents.add(saleauthEvent);
75         }
76     }
77
78     /**
79      * Gets the id.
80      *
81      * @return the id
82      */
83     public String getId() {
84         return id;
85     }
86 }