Provide IntentServiceManager and MplsLabelManagerService classes
[vpnservice.git] / vpnintent / impl / src / test / java / org / opendaylight / vpnservice / impl / IntentServiceManagerTest.java
1 /*
2  * Copyright (c) 2016 Inocybe Technologies 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
9 package org.opendaylight.vpnservice.impl;
10
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.verify;
14 import static org.mockito.Mockito.when;
15
16 import java.util.List;
17
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.Mock;
22 import org.mockito.Mockito;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.nic.utils.MdsalUtils;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.Intents;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.IntentsBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intents.Intent;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intents.IntentBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.intent.types.rev150122.Uuid;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.powermock.api.mockito.PowerMockito;
32 import org.powermock.api.support.membermodification.MemberModifier;
33 import org.powermock.core.classloader.annotations.PrepareForTest;
34 import org.powermock.modules.junit4.PowerMockRunner;
35
36 @PrepareForTest({IntentServiceManager.class})
37 @RunWith(PowerMockRunner.class)
38 public class IntentServiceManagerTest {
39
40     private static final String SRC_SITE_NAME = "site a";
41     private static final String DST_SITE_NAME = "site b";
42     private static final String INTENT_ALLOW_ACTION = "ALLOW";
43     private IntentServiceManager intentServiceManager;
44     @Mock private MdsalUtils mdsal;
45
46     @Before
47     public void setUp() throws Exception {
48         intentServiceManager = mock(IntentServiceManager.class, Mockito.CALLS_REAL_METHODS);
49         MemberModifier.field(IntentServiceManager.class, "mdsal").set(intentServiceManager, mdsal);
50     }
51
52     @SuppressWarnings("unchecked")
53     @Test
54     public void testAddIntent() throws Exception {
55         IntentBuilder intentBldr = mock(IntentBuilder.class);
56         PowerMockito.whenNew(IntentBuilder.class).withNoArguments().thenReturn(intentBldr);
57         when(intentBldr.setId(any(Uuid.class))).thenReturn(intentBldr);
58         when(intentBldr.setSubjects(any(List.class))).thenReturn(intentBldr);
59         when(intentBldr.setActions(any(List.class))).thenReturn(intentBldr);
60         when(intentBldr.setConstraints(any(List.class))).thenReturn(intentBldr);
61         when(intentBldr.build()).thenReturn(mock(Intent.class));
62
63         Intents currentIntents = mock(Intents.class);
64         PowerMockito.when(mdsal.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(currentIntents);
65         IntentsBuilder intentsBldr = mock(IntentsBuilder.class);
66         PowerMockito.whenNew(IntentsBuilder.class).withNoArguments().thenReturn(intentsBldr);
67         when(intentsBldr.setIntent(any(List.class))).thenReturn(intentsBldr);
68         PowerMockito.when(mdsal.put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Intents.class))).thenReturn(true);
69
70         intentServiceManager.addIntent(SRC_SITE_NAME, DST_SITE_NAME, INTENT_ALLOW_ACTION, "fast-reroute");
71         verify(intentBldr).setId(any(Uuid.class));
72         verify(mdsal).read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
73         verify(mdsal).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Intents.class));
74     }
75
76     @SuppressWarnings("unchecked")
77     @Test
78     public void testRemoveIntents() {
79         Uuid id = mock(Uuid.class);
80         when(mdsal.delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(true);
81
82         intentServiceManager.removeIntent(id);
83         verify(mdsal).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
84     }
85 }