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.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.Matchers.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.Query;
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;
52 @RunWith(PowerMockRunner.class)
53 public class BrmsPushTest {
55 public ExpectedException thrown = ExpectedException.none();
57 @PrepareForTest({Persistence.class, BackUpMonitor.class})
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);
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);
77 final String propFile = "config.properties";
78 final BackUpHandler handler = Mockito.mock(BackUpHandler.class);
79 final BrmsPush push = new BrmsPush(propFile, handler);
82 final String name = "testName";
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);
102 push.removeRule(name);
103 } catch (final Exception ex) {
104 fail("Not expecting an exception: " + ex);
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);
117 } catch (final Exception ex) {
118 fail("Not expecting an exception: " + ex);
122 thrown.expect(PolicyException.class);