Removed checkstyle warnings
[policy/engine.git] / BRMSGateway / src / test / java / org / onap / policy / brms / api / BrmsPushTest.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.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.fail;
26 import static org.mockito.Matchers.any;
27
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31
32 import javax.persistence.EntityManager;
33 import javax.persistence.EntityManagerFactory;
34 import javax.persistence.EntityTransaction;
35 import javax.persistence.Persistence;
36 import javax.persistence.Query;
37
38 import org.apache.maven.model.Dependency;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.rules.ExpectedException;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mockito;
44 import org.onap.policy.api.PolicyException;
45 import org.onap.policy.utils.BackUpHandler;
46 import org.onap.policy.utils.BackUpMonitor;
47 import org.onap.policy.utils.BackUpMonitorException;
48 import org.powermock.api.mockito.PowerMockito;
49 import org.powermock.core.classloader.annotations.PrepareForTest;
50 import org.powermock.modules.junit4.PowerMockRunner;
51
52 @RunWith(PowerMockRunner.class)
53 public class BrmsPushTest {
54     @Rule
55     public ExpectedException thrown = ExpectedException.none();
56
57     @PrepareForTest({Persistence.class, BackUpMonitor.class})
58     @Test
59     public void testPush() throws BackUpMonitorException, PolicyException {
60         // Mock emf, persistence, and query
61         final EntityManagerFactory emf = Mockito.mock(EntityManagerFactory.class);
62         final EntityManager em = Mockito.mock(EntityManager.class);
63         Mockito.when(emf.createEntityManager()).thenReturn(em);
64         PowerMockito.mockStatic(Persistence.class);
65         PowerMockito.when(Persistence.createEntityManagerFactory(Mockito.any(), Mockito.any())).thenReturn(emf);
66         final EntityTransaction et = Mockito.mock(EntityTransaction.class);
67         Mockito.when(em.getTransaction()).thenReturn(et);
68         final Query query = Mockito.mock(Query.class);
69         Mockito.when(em.createQuery(Mockito.anyString())).thenReturn(query);
70
71         // Mock backup monitor
72         PowerMockito.mockStatic(BackUpMonitor.class);
73         final BackUpMonitor monitor = Mockito.mock(BackUpMonitor.class);
74         PowerMockito.when(BackUpMonitor.getInstance(any(), any(), any(), any())).thenReturn(monitor);
75
76         // Test constructor
77         final String propFile = "config.properties";
78         final BackUpHandler handler = Mockito.mock(BackUpHandler.class);
79         final BrmsPush push = new BrmsPush(propFile, handler);
80         assertNotNull(push);
81
82         final String name = "testName";
83         try {
84             // Test initiate
85             push.initiate(true);
86
87             // Test reset
88             push.resetDs();
89
90             // Test add
91             final String rule = "testRule";
92             final Map<String, String> responseAttributes = new HashMap<String, String>();
93             responseAttributes.put("$controller:", "{\n\"testKey\": \"testVal\"\n}\n");
94             responseAttributes.put("$dependency$", "[a,b]");
95             push.addRule(name, rule, responseAttributes);
96         } catch (final Exception ex) {
97             fail("Not expecting an exception: " + ex);
98         }
99
100         try {
101             // Test remove
102             push.removeRule(name);
103         } catch (final Exception ex) {
104             fail("Not expecting an exception: " + ex);
105
106         }
107
108         // Test misc methods
109         final String controllerName = "testController";
110         final List<Dependency> deps = push.defaultDependencies(controllerName);
111         assertEquals(deps.size(), 7);
112         assertNotNull(BrmsPush.getBackUpMonitor());
113         assertEquals(push.urlListSize(), 1);
114
115         try {
116             push.rotateUrls();
117         } catch (final Exception ex) {
118             fail("Not expecting an exception: " + ex);
119         }
120
121         // Test push
122         thrown.expect(PolicyException.class);
123         push.pushRules();
124     }
125 }