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.assertNotNull;
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.fail;
26 import static org.mockito.Matchers.any;
28 import javax.persistence.EntityManager;
29 import javax.persistence.EntityManagerFactory;
30 import javax.persistence.EntityTransaction;
31 import javax.persistence.Persistence;
32 import javax.persistence.TypedQuery;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mockito;
37 import org.onap.policy.api.NotificationType;
38 import org.onap.policy.std.StdPDPNotification;
39 import org.onap.policy.utils.BackUpMonitor;
40 import org.powermock.api.mockito.PowerMockito;
41 import org.powermock.core.classloader.annotations.PrepareForTest;
42 import org.powermock.modules.junit4.PowerMockRunner;
44 @RunWith(PowerMockRunner.class)
45 public class BrmsHandlerTest {
46 @PrepareForTest({Persistence.class, BackUpMonitor.class})
48 public void negativeTestNotifications() throws Exception {
49 // Mock emf, persistence, and query
50 final EntityManagerFactory emf = Mockito.mock(EntityManagerFactory.class);
51 final EntityManager em = Mockito.mock(EntityManager.class);
52 Mockito.when(emf.createEntityManager()).thenReturn(em);
53 PowerMockito.mockStatic(Persistence.class);
54 PowerMockito.when(Persistence.createEntityManagerFactory(any(), any())).thenReturn(emf);
55 final EntityTransaction et = Mockito.mock(EntityTransaction.class);
56 Mockito.when(em.getTransaction()).thenReturn(et);
57 final TypedQuery<?> query = Mockito.mock(TypedQuery.class);
58 Mockito.when(em.createQuery(Mockito.anyString(), Mockito.any())).thenReturn((TypedQuery<Object>) query);
60 // Mock backup monitor
61 PowerMockito.mockStatic(BackUpMonitor.class);
62 final BackUpMonitor monitor = Mockito.mock(BackUpMonitor.class);
63 PowerMockito.when(BackUpMonitor.getInstance(any(), any(), any(), any())).thenReturn(monitor);
66 final StdPDPNotification notification = new StdPDPNotification();
67 final String propFile = "config.properties";
68 final BrmsHandler handler = new BrmsHandler(propFile);
69 final BrmsPush brmsPush = new BrmsPush(propFile, handler);
70 handler.setBrmsPush(brmsPush);
71 assertNotNull(handler);
72 assertNotNull(brmsPush);
73 assertNull(handler.getPolicyEngine());
77 notification.setNotificationType(NotificationType.UPDATE);
78 handler.runOnNotification(notification);
79 handler.notificationReceived(notification);
82 notification.setNotificationType(NotificationType.REMOVE);
83 handler.runOnNotification(notification);
84 handler.notificationReceived(notification);
87 notification.setNotificationType(NotificationType.BOTH);
88 handler.runOnNotification(notification);
89 handler.notificationReceived(notification);
90 } catch (final Exception ex) {
91 fail("Not expecting an exception: " + ex);