b34b18b6dfcbb084416571d82a88f0bd917515ae
[policy/engine.git] / BRMSGateway / src / test / java / org / onap / policy / brms / api / BrmsGatewayTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.brms.api;
22
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.fail;
27
28 import org.junit.After;
29 import org.junit.AfterClass;
30 import org.junit.Before;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.mockito.Mockito;
34 import org.onap.policy.api.PolicyException;
35 import org.onap.policy.brms.api.BrmsGateway.Factory;
36 import org.powermock.reflect.Whitebox;
37
38 public class BrmsGatewayTest {
39     private static final String FACTORY_FIELD = "factory";
40
41     private static Factory saveFactory;
42
43     private Thread thread;
44
45     @BeforeClass
46     public static void setUpBeforeClass() {
47         saveFactory = Whitebox.getInternalState(BrmsGateway.class, FACTORY_FIELD);
48     }
49
50     @AfterClass
51     public static void tearDownAfterClass() {
52         Whitebox.setInternalState(BrmsGateway.class, FACTORY_FIELD, saveFactory);
53     }
54
55     /**
56      * Installs a factory.
57      */
58     @Before
59     public void setUp() {
60         thread = null;
61
62         Factory factory = new Factory() {
63             @Override
64             public BrmsHandler makeBrmsHandler(String configFile) throws PolicyException {
65                 // Mock handler
66                 return Mockito.mock(BrmsHandler.class);
67             }
68
69             @Override
70             public Thread makeThread(Runnable runnable) {
71                 thread = super.makeThread(runnable);
72                 return thread;
73             }
74
75         };
76
77         Whitebox.setInternalState(BrmsGateway.class, FACTORY_FIELD, factory);
78     }
79
80     /**
81      * Interrupts the thread, if there is one.
82      */
83     @After
84     public void tearDown() throws InterruptedException {
85         if (thread != null) {
86             thread.interrupt();
87             thread.join(5000L);
88             assertFalse(thread.isAlive());
89         }
90     }
91
92     @Test
93     public void testFactory() throws InterruptedException {
94         assertNotNull(saveFactory);
95         assertNotNull(saveFactory.makeThread(() -> { }));
96     }
97
98     @Test
99     public void testGet() {
100         assertNull(BrmsGateway.getPolicyEngine());
101     }
102
103     @Test
104     public void testMain() throws Exception {
105         try {
106             final String[] args = new String[0];
107             BrmsGateway.main(args);
108         } catch (final Exception ex) {
109             fail("Not expected an exception: " + ex);
110         }
111     }
112 }