f024441e71a2da6211b439a1450bd3f0b7bffbdc
[unimgr.git] / cli / src / test / java / org / opendaylight / unimgr / cli / UniAddShellCommandTest.java
1 /*
2  * Copyright (c) 2016 CableLabs 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.unimgr.cli;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.when;
13
14 import java.math.BigInteger;
15
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentationBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed100M;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed100MBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10G;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10GBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10M;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10MBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed1G;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed1GBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.uni.Speed;
34 import org.powermock.api.mockito.PowerMockito;
35 import org.powermock.api.support.membermodification.MemberModifier;
36 import org.powermock.core.classloader.annotations.PrepareForTest;
37 import org.powermock.modules.junit4.PowerMockRunner;
38 import org.powermock.reflect.Whitebox;
39
40 @RunWith(PowerMockRunner.class)
41 @PrepareForTest(UniAddShellCommand.class)
42 public class UniAddShellCommandTest {
43
44     private final String physicalMedium = "UNI TypeFull Duplex 2 Physical Interface";
45     private final String macAddress = "00:00:00:00:00:00";
46     private final String mode = "Full Duplex";
47     private final String macLayer = "IEEE 802.3-2005";
48     private final String type = "";
49     private final String mtuSize = "0";
50     private final String ipAddress = "192.168.1.1";
51
52     private final UnimgrConsoleProviderTest provider = new UnimgrConsoleProviderTest();
53
54     @Mock UniAddShellCommand uniAddShellCommand;
55     //@Mock UnimgrProvider provider;
56
57     @Before
58     public void setUp() throws IllegalArgumentException, IllegalAccessException{
59         uniAddShellCommand = mock(UniAddShellCommand.class, Mockito.CALLS_REAL_METHODS);
60         MemberModifier.field(UniAddShellCommand.class, "provider").set(uniAddShellCommand, provider);
61         Whitebox.setInternalState(uniAddShellCommand, "macAddress", macAddress);
62         Whitebox.setInternalState(uniAddShellCommand, "macLayer", macLayer);
63         Whitebox.setInternalState(uniAddShellCommand, "mode", mode);
64         Whitebox.setInternalState(uniAddShellCommand, "mtuSize", mtuSize);
65         Whitebox.setInternalState(uniAddShellCommand, "physicalMedium", physicalMedium);
66         Whitebox.setInternalState(uniAddShellCommand, "type", type);
67         Whitebox.setInternalState(uniAddShellCommand, "ipAddress", ipAddress);
68     }
69
70     /**
71      * Test method for {@link org.opendaylight.unimgr.cli.UniAddShellCommand#getSpeed()}.
72      * @throws Exception
73      */
74     @Test
75     public void testgetSpeed() throws Exception {
76         // Test 10M
77         Whitebox.setInternalState(uniAddShellCommand, "speed", "10M");
78         final Speed10M speed10M = mock(Speed10M.class);
79         final Speed10MBuilder speed10MBuilder = mock(Speed10MBuilder.class);
80         when(speed10MBuilder.build()).thenReturn(speed10M);
81         PowerMockito.whenNew(Speed10MBuilder.class).withNoArguments().thenReturn(speed10MBuilder);
82         Object getSpeed = Whitebox.invokeMethod(uniAddShellCommand, "getSpeed");
83         assertEquals(speed10M, getSpeed);
84
85         // Test 100M
86         Whitebox.setInternalState(uniAddShellCommand, "speed", "100M");
87         final Speed100M speedObject100M = mock(Speed100M.class);
88         final Speed100MBuilder speed100M = mock(Speed100MBuilder.class);
89         PowerMockito.whenNew(Speed100MBuilder.class).withNoArguments().thenReturn(speed100M);
90         when(speed100M.build()).thenReturn(speedObject100M);
91         getSpeed = Whitebox.invokeMethod(uniAddShellCommand, "getSpeed");
92         assertEquals(speedObject100M, getSpeed);
93
94         // Test 1G
95         Whitebox.setInternalState(uniAddShellCommand, "speed", "1G");
96         final Speed1G speedObject1G = mock(Speed1G.class);
97         final Speed1GBuilder speed1G = mock(Speed1GBuilder.class);
98         PowerMockito.whenNew(Speed1GBuilder.class).withNoArguments().thenReturn(speed1G);
99         when(speed1G.build()).thenReturn(speedObject1G);
100         getSpeed = Whitebox.invokeMethod(uniAddShellCommand, "getSpeed");
101         assertEquals(speedObject1G, getSpeed);
102
103         // Test 10G
104         Whitebox.setInternalState(uniAddShellCommand, "speed", "10G");
105         final Speed10G speedObject10G = mock(Speed10G.class);
106         final Speed10GBuilder speed10G = mock(Speed10GBuilder.class);
107         PowerMockito.whenNew(Speed10GBuilder.class).withNoArguments().thenReturn(speed10G);
108         when(speed10G.build()).thenReturn(speedObject10G);
109         getSpeed = Whitebox.invokeMethod(uniAddShellCommand, "getSpeed");
110         assertEquals(speedObject10G, getSpeed);
111
112         // Test other
113         Whitebox.setInternalState(uniAddShellCommand, "speed", "other");
114         getSpeed = Whitebox.invokeMethod(uniAddShellCommand, "getSpeed");
115         assertEquals(null, getSpeed);
116     }
117
118     /**
119      * Test method for {@link org.opendaylight.unimgr.cli.UniAddShellCommand#doExecute()}.
120      * @throws Exception
121      */
122     @Test
123     public void testDoExecute() throws Exception {
124         Whitebox.setInternalState(uniAddShellCommand, "speed", "other");
125         final UniAugmentation uni = new UniAugmentationBuilder()
126                 .setMacAddress(new MacAddress(macAddress))
127                 .setMacLayer(macLayer)
128                 .setMode(mode)
129                 .setMtuSize(BigInteger.valueOf(Long.valueOf(mtuSize)))
130                 .setPhysicalMedium(physicalMedium)
131                 .setSpeed((Speed) null)
132                 .setType(type)
133                 .setIpAddress(new IpAddress(ipAddress.toCharArray()))
134                 .build();
135         final Object success = new String("Uni with ip " +ipAddress+" created");
136         final Object error = new String("Error creating new Uni");
137         Object stringReturn = uniAddShellCommand.doExecute();
138         assertEquals(success, stringReturn);
139         assertEquals(uni, provider.getUni(null));
140         stringReturn = uniAddShellCommand.doExecute();
141         assertEquals(error, stringReturn);
142     }
143
144 }