4d9d872fa05b5588cc6282a3273e0a87af7d8df7
[transportpce.git] / servicehandler / src / test / java / org / opendaylight / transportpce / servicehandler / utils / 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.transportpce.servicehandler.utils;
9
10 import static org.junit.Assert.assertTrue;
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.Mockito.doAnswer;
13 import static org.mockito.Mockito.mock;
14
15 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
16 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.RendererRpcResultSp;
17 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.RendererRpcResultSpBuilder;
18 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.renderer.rpc.result.sp.PathTopologyBuilder;
19 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.RpcStatusEx;
20 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.ServicePathNotificationTypes;
21 import org.opendaylight.yangtools.yang.binding.Notification;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public final class MockedNotificationServiceWrapper {
26
27     private static final Logger LOG = LoggerFactory.getLogger(MockedNotificationServiceWrapper.class);
28     private final NotificationPublishService notificationPublishService;
29     private Boolean rendererFailed = false;
30
31     public MockedNotificationServiceWrapper(NotificationPublishService notificationPublishService) {
32         this.notificationPublishService = notificationPublishService;
33     }
34
35     public NotificationPublishService getMockedNotificationService() throws InterruptedException {
36         final NotificationPublishService mockedNotificationService = mock(NotificationPublishService.class);
37         doAnswer(invocation -> {
38             final Object notif = invocation.getArguments()[0];
39             LOG.info("notif received : {}", notif);
40             assertTrue(Notification.class.isAssignableFrom(notif.getClass()));
41             if (this.rendererFailed) {
42                 LOG.info("putting failed renderer notification");
43                 RendererRpcResultSp serviceRpcResultSp = new RendererRpcResultSpBuilder()
44                         .setNotificationType(ServicePathNotificationTypes.ServiceImplementationRequest)
45                         .setServiceName("service 1").setStatus(RpcStatusEx.Failed).setStatusMessage("Renderer Failed")
46                         .setPathTopology(new PathTopologyBuilder().build()).build();
47                 MockedNotificationServiceWrapper.this.notificationPublishService.putNotification(serviceRpcResultSp);
48                 this.rendererFailed = false;
49             } else {
50                 MockedNotificationServiceWrapper.this.notificationPublishService.putNotification((Notification) notif);
51             }
52             return null;
53         }).when(mockedNotificationService).putNotification(any(Notification.class));
54         return mockedNotificationService;
55     }
56
57     public void setRendererFailed() {
58         this.rendererFailed = true;
59     }
60 }