upgrade models to OpenROADM service 5.1.0
[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.rev171017.ServiceRpcResultSp;
17 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017.ServiceRpcResultSpBuilder;
18 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017.service.rpc.result.sp.PathTopologyBuilder;
19 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev191009.RpcStatusEx;
20 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev191009.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 Notification publishedNotification;
29     private final NotificationPublishService notificationPublishService;
30     private Boolean rendererFailed = false;
31
32     public MockedNotificationServiceWrapper(NotificationPublishService notificationPublishService) {
33         this.publishedNotification = null;
34         this.notificationPublishService = notificationPublishService;
35     }
36
37     public NotificationPublishService getMockedNotificationService() throws InterruptedException {
38         final NotificationPublishService mockedNotificationService = mock(NotificationPublishService.class);
39         doAnswer(invocation -> {
40             final Object notif = invocation.getArguments()[0];
41             LOG.info("notif received : {}", notif);
42             assertTrue(Notification.class.isAssignableFrom(notif.getClass()));
43             if (this.rendererFailed) {
44                 LOG.info("putting failed renderer notification");
45                 ServiceRpcResultSp serviceRpcResultSp = new ServiceRpcResultSpBuilder()
46                         .setNotificationType(ServicePathNotificationTypes.ServiceImplementationRequest)
47                         .setServiceName("service 1").setStatus(RpcStatusEx.Failed).setStatusMessage("Renderer Failed")
48                         .setPathTopology(new PathTopologyBuilder().build()).build();
49                 MockedNotificationServiceWrapper.this.notificationPublishService.putNotification(serviceRpcResultSp);
50                 this.rendererFailed = false;
51             } else {
52                 MockedNotificationServiceWrapper.this.notificationPublishService.putNotification((Notification) notif);
53             }
54             return null;
55         }).when(mockedNotificationService).putNotification(any(Notification.class));
56         return mockedNotificationService;
57     }
58
59     public void setRendererFailed() {
60         this.rendererFailed = true;
61     }
62 }