c8ef1a24813063abd9caefec768c81e7ee891ae9
[nemo.git] / nemo-impl / src / test / java / org / opendaylight / nemo / user / vnspacemanager / structurestyle / deleteintent / DeleteConnectionTest.java
1 /*
2  * Copyright (c) 2015 Huawei, 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.nemo.user.vnspacemanager.structurestyle.deleteintent;
9 import org.opendaylight.nemo.user.vnspacemanager.structurestyle.*;
10 import static org.mockito.Mockito.mock;
11
12 import org.junit.Assert;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.nemo.user.tenantmanager.TenantManage;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.ConnectionId;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.UserId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.Users;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.Objects;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.Connection;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.ConnectionKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.users.User;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.users.UserKey;
25 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
29 import com.google.common.util.concurrent.CheckedFuture;
30 import com.google.common.util.concurrent.FutureCallback;
31 import com.google.common.util.concurrent.Futures;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import java.util.List;
35 import java.util.*;
36 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
37 import static org.mockito.Mockito.*;
38 public class  DeleteConnectionTest{
39             private DataBroker dataBroker;
40         private TenantManage tenantManage;
41         private DeleteConnection deleteconnection;
42         private UserId userId;
43         private ConnectionId connectionID;
44                 private User user;
45                 private Objects objects;
46                 private Connection connection;
47                 private List<Connection> connectionList;
48                 private ConnectionId NotconnectionID;
49                 private  ConnectionId connectionID2;
50                 private   WriteTransaction  writetransaction;
51 @org.junit.Before
52 public void setUp() throws Exception {
53             dataBroker=mock(DataBroker.class);
54                 tenantManage=mock(TenantManage.class);
55         deleteconnection=mock(DeleteConnection.class);
56         userId=mock(UserId.class);
57         connectionID=mock(ConnectionId.class);
58                 NotconnectionID=mock(ConnectionId.class);
59                 connectionID2=connectionID;
60                 user=mock(User.class);
61                 objects=mock(Objects.class);
62                 connection=mock(Connection.class);
63                 connectionList=new ArrayList<Connection>(3);
64                 connectionList.add(connection);
65                 deleteconnection=new DeleteConnection(dataBroker,tenantManage);
66                 writetransaction=mock(WriteTransaction.class);
67         }
68 @org.junit.Test
69 public void DeleteConnectionHandlingTest() throws Exception{
70         //no data test
71            CheckedFuture connectiondefinitionFuture = mock(CheckedFuture.class);
72        ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class);
73            when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
74           
75            when(tenantManage.getUser()).thenReturn(null);
76            Assert.assertEquals(deleteconnection.DeleteConnectionHandling(userId,connectionID),"There are no connection instances in data store.");
77            //data exists and other branches
78            // branch 1
79            when (tenantManage.getUser()).thenReturn(user);
80            when(user.getObjects()).thenReturn(null);
81            Assert.assertEquals(deleteconnection.DeleteConnectionHandling(userId,connectionID),null);
82            //branch 2
83            when(tenantManage.getUser()).thenReturn(user);
84            when(user.getObjects()).thenReturn(objects);
85            when(user.getObjects().getConnection()).thenReturn(null);
86            Assert.assertEquals(deleteconnection.DeleteConnectionHandling(userId,connectionID),"There are no connection instances in data store.");
87        //branch 3
88            when(tenantManage.getUser()).thenReturn(user);
89            when(user.getObjects()).thenReturn(objects);
90            when(objects.getConnection()).thenReturn(connectionList);
91            when(connection.getConnectionId()).thenReturn(NotconnectionID);
92            Assert.assertEquals(deleteconnection.DeleteConnectionHandling(userId,connectionID),"The connection instance " +connectionID.getValue()+" is not exit. Could not be deleted.");
93        //branch 4
94            when(tenantManage.getUser()).thenReturn(user);
95            when(user.getObjects()).thenReturn(objects);
96            when(objects.getConnection()).thenReturn(connectionList);
97            when(connection.getConnectionId()).thenReturn(connectionID);
98            when(dataBroker.newWriteOnlyTransaction()).thenReturn(writetransaction);
99            CheckedFuture<Void, TransactionCommitFailedException> f;
100            f=mock(CheckedFuture.class);
101            when(writetransaction.submit()).thenReturn(f);
102            Assert.assertEquals(deleteconnection.DeleteConnectionHandling(userId,connectionID),null);
103        //-------
104                 Assert.assertNotNull(deleteconnection);
105                 Assert.assertNotNull(tenantManage);
106         //Assert.assertNull(deleteconnection.DeleteConnectionHandling(userId,connectionID));
107         }
108  
109         }