2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.brmsInterface;
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;
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;
49 @RunWith(PowerMockRunner.class)
50 public class BRMSPushTest {
52 public ExpectedException thrown = ExpectedException.none();
54 @PrepareForTest({Persistence.class, BackUpMonitor.class})
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);
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);
74 String propFile = "config.properties";
75 BackUpHandler handler = Mockito.mock(BackUpHandler.class);
76 BRMSPush push = new BRMSPush(propFile, handler);
79 String name = "testName";
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);
94 catch (Exception ex) {
95 fail("Not expecting an exception: " + ex);
100 push.removeRule(name);
102 catch (Exception ex) {
103 fail("Not expecting an exception: " + ex);
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);
117 catch (Exception ex) {
118 fail("Not expecting an exception: " + ex);
122 thrown.expect(PolicyException.class);