2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2020 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.brms.api;
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.ArgumentMatchers.any;
28 import java.util.HashMap;
29 import java.util.List;
32 import javax.persistence.EntityManager;
33 import javax.persistence.EntityManagerFactory;
34 import javax.persistence.EntityTransaction;
35 import javax.persistence.Persistence;
36 import javax.persistence.TypedQuery;
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.PowerMockIgnore;
50 import org.powermock.core.classloader.annotations.PrepareForTest;
51 import org.powermock.modules.junit4.PowerMockRunner;
53 @RunWith(PowerMockRunner.class)
54 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "jdk.internal.reflect.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})
55 @PrepareForTest({Persistence.class, BackUpMonitor.class})
56 public class BrmsPushTest {
58 public ExpectedException thrown = ExpectedException.none();
61 public void testPush() throws BackUpMonitorException, PolicyException {
62 // Mock emf, persistence, and query
63 final EntityManagerFactory emf = Mockito.mock(EntityManagerFactory.class);
64 final EntityManager em = Mockito.mock(EntityManager.class);
65 Mockito.when(emf.createEntityManager()).thenReturn(em);
66 PowerMockito.mockStatic(Persistence.class);
67 PowerMockito.when(Persistence.createEntityManagerFactory(Mockito.any(), Mockito.any())).thenReturn(emf);
68 final EntityTransaction et = Mockito.mock(EntityTransaction.class);
69 Mockito.when(em.getTransaction()).thenReturn(et);
70 final TypedQuery<?> query = Mockito.mock(TypedQuery.class);
71 Mockito.when(em.createQuery(Mockito.anyString(), Mockito.any())).thenReturn((TypedQuery<Object>) query);
73 // Mock backup monitor
74 PowerMockito.mockStatic(BackUpMonitor.class);
75 final BackUpMonitor monitor = Mockito.mock(BackUpMonitor.class);
76 PowerMockito.when(BackUpMonitor.getInstance(any(), any(), any(), any())).thenReturn(monitor);
79 final String propFile = "config.properties";
80 final BackUpHandler handler = Mockito.mock(BackUpHandler.class);
81 final BrmsPush push = new BrmsPush(propFile, handler);
84 final String name = "testName";
93 final String rule = "testRule";
94 final Map<String, String> responseAttributes = new HashMap<String, String>();
95 responseAttributes.put("$controller:", "{\n\"testKey\": \"testVal\"\n}\n");
96 responseAttributes.put("$dependency$", "[a,b]");
97 push.addRule(name, rule, responseAttributes);
98 } catch (final Exception ex) {
99 fail("Not expecting an exception: " + ex);
104 push.removeRule(name);
105 } catch (final Exception ex) {
106 fail("Not expecting an exception: " + ex);
111 final String controllerName = "testController";
112 final List<Dependency> deps = push.defaultDependencies(controllerName);
113 assertEquals(deps.size(), 6);
114 assertNotNull(BrmsPush.getBackUpMonitor());
115 assertEquals(push.urlListSize(), 1);
119 } catch (final Exception ex) {
120 fail("Not expecting an exception: " + ex);
124 thrown.expect(PolicyException.class);