Implement CLI configuration generation.
[nemo.git] / nemo-impl / src / test / java / org / opendaylight / nemo / user / vnspacemanager / syntaxcheck / ConnectionDefinitionCheckTest.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.syntaxcheck;
9
10 import org.junit.runner.RunWith;
11 import static org.powermock.api.mockito.PowerMockito.verifyPrivate;
12 import static org.powermock.api.mockito.PowerMockito.doNothing;
13 import static org.powermock.api.mockito.PowerMockito.spy;
14 import org.powermock.core.classloader.annotations.PrepareForTest;
15 import org.powermock.modules.junit4.PowerMockRunner;
16
17 import junit.framework.TestCase;
18 import org.junit.Assert;
19 import org.junit.Before;
20 import org.junit.Test;
21
22 import static org.junit.Assert.*;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.nemo.user.tenantmanager.TenantManage;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.PropertyName;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.Connection;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.ConnectionDefinitions;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.connection.definitions.ConnectionDefinition;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.connection.instance.Property;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.definitions.PropertyDefinition;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.instance.PropertyValues;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.definitions.PropertyDefinition.PropertyValueType;
33 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
34 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
35 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
38 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import com.google.common.base.Optional;
41 import com.google.common.base.Function;
42 import com.google.common.util.concurrent.CheckedFuture;
43 import com.google.common.util.concurrent.FutureCallback;
44 import com.google.common.util.concurrent.Futures;
45 import com.google.common.util.concurrent.ListenableFuture;
46
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 import java.util.*;
51 import java.util.Iterator;
52 import java.util.LinkedList;
53 import java.util.List;
54 import java.util.ListIterator;
55
56 import java.lang.reflect.Method; 
57 import static org.mockito.Mockito.*;
58
59 @RunWith(PowerMockRunner.class)
60 @PrepareForTest(ConnectionDefinitionCheck.class)
61 public class ConnectionDefinitionCheckTest {
62     private ConnectionDefinitionCheck connectionDefinitionCheck;
63     private DataBroker dataBroker;
64         private ConnectionDefinition connectionDefinition;
65     private List<ConnectionDefinition> connectionDefinitionList;
66     private Connection connection;
67         private List<Property> connectionProperty;
68         private List<PropertyDefinition> propertyDefinitionList;
69         private Property property;
70         private PropertyDefinition propertyDefinition;
71                 
72     // private static final Logger LOG;
73     @org.junit.Before
74     public void setUp() throws Exception {
75                 ConnectionDefinition connectionDefinition = mock(ConnectionDefinition.class);
76                 connectionDefinitionList=new ArrayList<ConnectionDefinition>(3);
77                 property = mock(Property.class);
78                 propertyDefinition = mock(PropertyDefinition.class);
79                 connectionProperty = new ArrayList<Property>(3);
80                 propertyDefinitionList = new ArrayList<PropertyDefinition>(3);
81                 connectionDefinitionList.add(connectionDefinition);
82         connection = mock(Connection.class);
83         // LOG = mock(Logger.class);
84                 dataBroker = mock(DataBroker.class);
85         connectionDefinitionCheck =new ConnectionDefinitionCheck(dataBroker);
86     }
87
88     @org.junit.Test
89     public void testcheckConnectionDefinition() throws Exception {
90         CheckedFuture connectiondefinitionFuture = mock(CheckedFuture.class);
91         ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class);
92         when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
93         when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(connectiondefinitionFuture);
94                 
95                 //branch  connectionDefinitionList null
96                 Assert.assertEquals(connectionDefinitionCheck.CheckConnectionDefinition(connection),"This type of connection has not been defined.");
97                 
98                 //use reflact to test private CheckProperty
99                 Method testCheckProperty = connectionDefinitionCheck.getClass().getDeclaredMethod("CheckProperty",List.class,List.class);  
100                 testCheckProperty.setAccessible(true);
101                 //branch1 connectionProperty null,propertyDefinitionList null
102                 Assert.assertNull(testCheckProperty.invoke(connectionDefinitionCheck,connectionProperty,propertyDefinitionList));
103                 //branch2 property.getPropertyName() not equals(propertyDefinition.getPropertyName())
104                 connectionProperty.add(property);
105                 propertyDefinitionList.add(propertyDefinition); 
106                 PropertyName propertyName1 = mock(PropertyName.class);
107                 PropertyName propertyName2 = mock(PropertyName.class);
108                 when(property.getPropertyName()).thenReturn(propertyName1);
109                 when(propertyDefinition.getPropertyName()).thenReturn(propertyName2);
110                 Assert.assertEquals(testCheckProperty.invoke(connectionDefinitionCheck,connectionProperty,propertyDefinitionList),"This type of property" + property.getPropertyName().toString() + " has not been defined.");
111                 //branch3  property.getPropertyName()equals(propertyDefinition.getPropertyName()),but can not mock PropertyValueType
112                 when(property.getPropertyName()).thenReturn(propertyName1);
113                 PropertyName propertyName3 = propertyName1;
114                 when(propertyDefinition.getPropertyName()).thenReturn(propertyName3);
115                 PropertyValues propertyValues = mock(PropertyValues.class);
116                 when(property.getPropertyValues()).thenReturn(propertyValues);
117                 Assert.assertNotNull(property.getPropertyValues());
118                 Assert.assertNull(propertyDefinition.getPropertyValueType());
119                 
120                 //dataBroker test
121                 verify(dataBroker).newReadOnlyTransaction();
122         verify(readOnlyTransaction).read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
123                 verifyPrivate(connectionDefinitionCheck).invoke("fetchConnectionDefinitionList");
124                 
125     }
126 }