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