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