policy/engine jdk11 upgrades
[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, 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
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.ArgumentMatchers.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.TypedQuery;
33
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.PowerMockIgnore;
42 import org.powermock.core.classloader.annotations.PrepareForTest;
43 import org.powermock.modules.junit4.PowerMockRunner;
44
45 @RunWith(PowerMockRunner.class)
46 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "jdk.internal.reflect.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})
47 @PrepareForTest({Persistence.class, BackUpMonitor.class})
48 public class BrmsHandlerTest {
49     @Test
50     public void testNegativeTestNotifications() throws Exception {
51         // Mock emf, persistence, and query
52         final EntityManagerFactory emf = Mockito.mock(EntityManagerFactory.class);
53         final EntityManager em = Mockito.mock(EntityManager.class);
54         Mockito.when(emf.createEntityManager()).thenReturn(em);
55         PowerMockito.mockStatic(Persistence.class);
56         PowerMockito.when(Persistence.createEntityManagerFactory(any(), any())).thenReturn(emf);
57         final EntityTransaction et = Mockito.mock(EntityTransaction.class);
58         Mockito.when(em.getTransaction()).thenReturn(et);
59         final TypedQuery<?> query = Mockito.mock(TypedQuery.class);
60         Mockito.when(em.createQuery(Mockito.anyString(), Mockito.any())).thenReturn((TypedQuery<Object>) query);
61
62         // Mock backup monitor
63         PowerMockito.mockStatic(BackUpMonitor.class);
64         final BackUpMonitor monitor = Mockito.mock(BackUpMonitor.class);
65         PowerMockito.when(BackUpMonitor.getInstance(any(), any(), any(), any())).thenReturn(monitor);
66
67         // Test constructor
68         final StdPDPNotification notification = new StdPDPNotification();
69         final String propFile = "config.properties";
70         final BrmsHandler handler = new BrmsHandler(propFile);
71         final BrmsPush brmsPush = new BrmsPush(propFile, handler);
72         handler.setBrmsPush(brmsPush);
73         assertNotNull(handler);
74         assertNotNull(brmsPush);
75         assertNull(handler.getPolicyEngine());
76
77         try {
78             // Test update
79             notification.setNotificationType(NotificationType.UPDATE);
80             handler.runOnNotification(notification);
81             handler.notificationReceived(notification);
82
83             // Test remove
84             notification.setNotificationType(NotificationType.REMOVE);
85             handler.runOnNotification(notification);
86             handler.notificationReceived(notification);
87
88             // Test both
89             notification.setNotificationType(NotificationType.BOTH);
90             handler.runOnNotification(notification);
91             handler.notificationReceived(notification);
92         } catch (final Exception ex) {
93             fail("Not expecting an exception: " + ex);
94         }
95     }
96 }