Added test for ProgrammingServiceImpl
[bgpcep.git] / programming / impl / src / test / java / org / opendaylight / bgpcep / programming / impl / MockedNotificationServiceWrapper.java
1 /**
2  * @author Maros Marsalek
3  *
4  * 01 2014
5  *
6  * Copyright (c) 2012 by Cisco Systems, Inc.
7  * All rights reserved.
8  */
9 package org.opendaylight.bgpcep.programming.impl;
10
11 import com.google.common.collect.Lists;
12 import junit.framework.Assert;
13 import org.mockito.invocation.InvocationOnMock;
14 import org.mockito.stubbing.Answer;
15 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.InstructionId;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.InstructionStatus;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.InstructionStatusChanged;
19 import org.opendaylight.yangtools.yang.binding.Notification;
20
21 import java.util.List;
22
23 import static junit.framework.Assert.assertTrue;
24 import static org.junit.Assert.assertEquals;
25 import static org.mockito.Matchers.any;
26 import static org.mockito.Mockito.doAnswer;
27 import static org.mockito.Mockito.mock;
28
29 final class MockedNotificationServiceWrapper {
30         private List<Notification> publishedNotifications;
31
32         MockedNotificationServiceWrapper() {
33                 publishedNotifications = Lists.newArrayList();
34         }
35
36         NotificationProviderService getMockedNotificationService() {
37                 NotificationProviderService mockedNotificationService = mock(NotificationProviderService.class);
38
39                 doAnswer(new Answer() {
40                         @Override
41                         public Object answer(InvocationOnMock invocation) throws Throwable {
42                                 Object notif = invocation.getArguments()[0];
43                                 assertTrue(Notification.class.isAssignableFrom(notif.getClass()));
44                                 publishedNotifications.add((Notification) notif);
45                                 return null;
46                         }
47                 }).when(mockedNotificationService).publish(any(Notification.class));
48                 return mockedNotificationService;
49         }
50
51         void assertNotificationsCount(int count) {
52                 assertEquals(count, publishedNotifications.size());
53         }
54
55         public void assertInstructionStatusChangedNotification(int idx, InstructionId id, InstructionStatus status) {
56                 assertTrue(InstructionStatusChanged.class.isAssignableFrom(publishedNotifications.get(idx).getClass()));
57                 InstructionStatusChanged firstNotification = (InstructionStatusChanged) publishedNotifications.get(idx);
58                 assertInstructionStatusChangedNotification(id, status, firstNotification);
59         }
60
61         private void assertInstructionStatusChangedNotification(InstructionId id, InstructionStatus status,
62                         InstructionStatusChanged firstNotification) {
63                 Assert.assertEquals(id, firstNotification.getId());
64                 Assert.assertEquals(status, firstNotification.getStatus());
65         }
66 }