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