Fixed the Policy GUI Editor tab right click issue.
[policy/engine.git] / BRMSGateway / src / test / java / org / onap / policy / brms / api / BrmsHandlerTest.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.brms.api;
22
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;
27
28 import javax.persistence.EntityManager;
29 import javax.persistence.EntityManagerFactory;
30 import javax.persistence.EntityTransaction;
31 import javax.persistence.Persistence;
32 import javax.persistence.Query;
33 import javax.persistence.TypedQuery;
34
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mockito;
38 import org.onap.policy.api.NotificationType;
39 import org.onap.policy.std.StdPDPNotification;
40 import org.onap.policy.utils.BackUpMonitor;
41 import org.powermock.api.mockito.PowerMockito;
42 import org.powermock.core.classloader.annotations.PrepareForTest;
43 import org.powermock.modules.junit4.PowerMockRunner;
44
45 @RunWith(PowerMockRunner.class)
46 public class BrmsHandlerTest {
47     @PrepareForTest({Persistence.class, BackUpMonitor.class})
48     @Test
49     public void negativeTestNotifications() throws Exception {
50         // Mock emf, persistence, and query
51         final EntityManagerFactory emf = Mockito.mock(EntityManagerFactory.class);
52         final EntityManager em = Mockito.mock(EntityManager.class);
53         Mockito.when(emf.createEntityManager()).thenReturn(em);
54         PowerMockito.mockStatic(Persistence.class);
55         PowerMockito.when(Persistence.createEntityManagerFactory(any(), any())).thenReturn(emf);
56         final EntityTransaction et = Mockito.mock(EntityTransaction.class);
57         Mockito.when(em.getTransaction()).thenReturn(et);
58         final TypedQuery<?> query = Mockito.mock(TypedQuery.class);
59         Mockito.when(em.createQuery(Mockito.anyString(), Mockito.any())).thenReturn((TypedQuery<Object>) query);
60
61         // Mock backup monitor
62         PowerMockito.mockStatic(BackUpMonitor.class);
63         final BackUpMonitor monitor = Mockito.mock(BackUpMonitor.class);
64         PowerMockito.when(BackUpMonitor.getInstance(any(), any(), any(), any())).thenReturn(monitor);
65
66         // Test constructor
67         final StdPDPNotification notification = new StdPDPNotification();
68         final String propFile = "config.properties";
69         final BrmsHandler handler = new BrmsHandler(propFile);
70         final BrmsPush brmsPush = new BrmsPush(propFile, handler);
71         handler.setBrmsPush(brmsPush);
72         assertNotNull(handler);
73         assertNotNull(brmsPush);
74         assertNull(handler.getPolicyEngine());
75
76         try {
77             // Test update
78             notification.setNotificationType(NotificationType.UPDATE);
79             handler.runOnNotification(notification);
80             handler.notificationReceived(notification);
81
82             // Test remove
83             notification.setNotificationType(NotificationType.REMOVE);
84             handler.runOnNotification(notification);
85             handler.notificationReceived(notification);
86
87             // Test both
88             notification.setNotificationType(NotificationType.BOTH);
89             handler.runOnNotification(notification);
90             handler.notificationReceived(notification);
91         } catch (final Exception ex) {
92             fail("Not expecting an exception: " + ex);
93         }
94     }
95 }