c286c13c1d4f0ccd0df22ea7759450641bac3c8a
[bgpcep.git] / programming / impl / src / test / java / org / opendaylight / bgpcep / programming / impl / MockedNotificationServiceWrapper.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.bgpcep.programming.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Mockito.doAnswer;
14 import static org.mockito.Mockito.mock;
15
16 import com.google.common.collect.Lists;
17 import java.util.List;
18 import org.junit.Assert;
19 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.InstructionId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.InstructionStatus;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.InstructionStatusChanged;
23 import org.opendaylight.yangtools.yang.binding.Notification;
24
25 final class MockedNotificationServiceWrapper {
26     private final List<Notification> publishedNotifications;
27
28     MockedNotificationServiceWrapper() {
29         this.publishedNotifications = Lists.newArrayList();
30     }
31
32     NotificationPublishService getMockedNotificationService() throws InterruptedException {
33         final NotificationPublishService mockedNotificationService = mock(NotificationPublishService.class);
34
35         doAnswer(invocation -> {
36             final Object notif = invocation.getArguments()[0];
37             assertTrue(Notification.class.isAssignableFrom(notif.getClass()));
38             MockedNotificationServiceWrapper.this.publishedNotifications.add((Notification) notif);
39             return null;
40         }).when(mockedNotificationService).putNotification(any(Notification.class));
41         return mockedNotificationService;
42     }
43
44     void assertNotificationsCount(final int count) {
45         assertEquals(count, this.publishedNotifications.size());
46     }
47
48     void assertInstructionStatusChangedNotification(final int idx, final InstructionId id, final InstructionStatus status) {
49         assertTrue(InstructionStatusChanged.class.isAssignableFrom(this.publishedNotifications.get(idx).getClass()));
50         final InstructionStatusChanged firstNotification = (InstructionStatusChanged) this.publishedNotifications.get(idx);
51         assertInstructionStatusChangedNotification(id, status, firstNotification);
52     }
53
54     private void assertInstructionStatusChangedNotification(final InstructionId id, final InstructionStatus status,
55             final InstructionStatusChanged firstNotification) {
56         Assert.assertEquals(id, firstNotification.getId());
57         Assert.assertEquals(status, firstNotification.getStatus());
58     }
59 }