4dd6a81922ab9f897f1b65e89848b8d466a34736
[unimgr.git] / cli / src / test / java / org / opendaylight / unimgr / cli / EvcAddShellCommandTest.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 import static org.powermock.api.support.membermodification.MemberMatcher.method;
14
15 import java.util.ArrayList;
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.unimgr.impl.UnimgrConstants;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentation;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentationBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.EgressBw;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.IngressBw;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDest;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDestBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDestKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSource;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSourceBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSourceKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed100M;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed100MBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10G;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10GBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10M;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10MBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed1G;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed1GBuilder;
43 import org.powermock.api.mockito.PowerMockito;
44 import org.powermock.api.support.membermodification.MemberModifier;
45 import org.powermock.core.classloader.annotations.PrepareForTest;
46 import org.powermock.modules.junit4.PowerMockRunner;
47 import org.powermock.reflect.Whitebox;
48
49 @RunWith(PowerMockRunner.class)
50 @PrepareForTest(EvcAddShellCommand.class)
51 public class EvcAddShellCommandTest {
52
53     private final String IPs = "192.168.1.1";
54     private final String IPd = "192.168.1.2";
55     private final String egress = "100M";
56     private final String ingress = "10M";
57
58     private final UnimgrConsoleProviderTest provider = new UnimgrConsoleProviderTest();
59
60     @Mock EvcAddShellCommand evcAddShellCommand;
61
62     @Before
63     public void setUp() throws IllegalArgumentException, IllegalAccessException{
64         evcAddShellCommand = mock(EvcAddShellCommand.class, Mockito.CALLS_REAL_METHODS);
65         MemberModifier.field(EvcAddShellCommand.class, "provider").set(evcAddShellCommand, provider);
66
67     }
68
69     /**
70      * Test method for {@link org.opendaylight.unimgr.cli.EvcAddShellCommand#getSpeed()}.
71      * @throws Exception
72      */
73     @Test
74     public void testgetSpeed() throws Exception {
75         // Test 10M
76         String speed = "10M";
77         final Speed10M speed10M = mock(Speed10M.class);
78         final Speed10MBuilder speed10MBuilder = mock(Speed10MBuilder.class);
79         when(speed10MBuilder.build()).thenReturn(speed10M);
80         PowerMockito.whenNew(Speed10MBuilder.class).withNoArguments().thenReturn(speed10MBuilder);
81         Object getSpeed = Whitebox.invokeMethod(evcAddShellCommand, "getSpeed", speed);
82         assertEquals(speed10M, getSpeed);
83
84         // Test 100M
85         speed = "100M";
86         final Speed100M speedObject100M = mock(Speed100M.class);
87         final Speed100MBuilder speed100M = mock(Speed100MBuilder.class);
88         PowerMockito.whenNew(Speed100MBuilder.class).withNoArguments().thenReturn(speed100M);
89         when(speed100M.build()).thenReturn(speedObject100M);
90         getSpeed = Whitebox.invokeMethod(evcAddShellCommand, "getSpeed", speed);
91         assertEquals(speedObject100M, getSpeed);
92
93         // Test 1G
94         speed = "1G";
95         final Speed1G speedObject1G = mock(Speed1G.class);
96         final Speed1GBuilder speed1G = mock(Speed1GBuilder.class);
97         PowerMockito.whenNew(Speed1GBuilder.class).withNoArguments().thenReturn(speed1G);
98         when(speed1G.build()).thenReturn(speedObject1G);
99         getSpeed = Whitebox.invokeMethod(evcAddShellCommand, "getSpeed", speed);
100         assertEquals(speedObject1G, getSpeed);
101
102         // Test 10G
103         speed = "10G";
104         final Speed10G speedObject10G = mock(Speed10G.class);
105         final Speed10GBuilder speed10G = mock(Speed10GBuilder.class);
106         PowerMockito.whenNew(Speed10GBuilder.class).withNoArguments().thenReturn(speed10G);
107         when(speed10G.build()).thenReturn(speedObject10G);
108         getSpeed = Whitebox.invokeMethod(evcAddShellCommand, "getSpeed", speed);
109         assertEquals(speedObject10G, getSpeed);
110
111         // Test other
112         speed = "other";
113         getSpeed = Whitebox.invokeMethod(evcAddShellCommand, "getSpeed", speed);
114         assertEquals(null, getSpeed);
115     }
116
117     /**
118      * Test method for {@link org.opendaylight.unimgr.cli.EvcAddShellCommand#doExecute()}.
119      * @throws Exception
120      */
121     @Test
122     public void testDoExecute() throws Exception {
123         final EgressBw egressBw100M = mock(EgressBw.class);
124         final IngressBw ingressBw10M = mock(IngressBw.class);
125         final EvcAddShellCommand spyEvc = PowerMockito.spy(new EvcAddShellCommand(provider));
126         PowerMockito.when(spyEvc, method(EvcAddShellCommand.class, "getSpeed", String.class))
127             .withArguments("100M").thenReturn(egressBw100M);
128         PowerMockito.when(spyEvc, method(EvcAddShellCommand.class, "getSpeed", String.class))
129             .withArguments("10M").thenReturn(ingressBw10M);
130         Whitebox.setInternalState(spyEvc, "IPs", IPs);
131         Whitebox.setInternalState(spyEvc, "IPd", IPd);
132         Whitebox.setInternalState(spyEvc, "egress", egress);
133         Whitebox.setInternalState(spyEvc, "ingress", ingress);
134         final Short order = new Short("0");
135         final IpAddress ipAddreSource = new IpAddress(IPs.toCharArray());
136         final UniSource uniSource = new UniSourceBuilder()
137                                   .setIpAddress(ipAddreSource)
138                                   .setKey(new UniSourceKey(order))
139                                   .setOrder(order)
140                                   .build();
141         final List<UniSource> uniSourceList = new ArrayList<UniSource>();
142         uniSourceList.add(uniSource);
143         final IpAddress ipAddreDest = new IpAddress(IPd.toCharArray());
144         final UniDest uniDest = new UniDestBuilder()
145                           .setOrder(order)
146                           .setKey(new UniDestKey(order))
147                           .setIpAddress(ipAddreDest)
148                           .build();
149         final List<UniDest> uniDestList = new ArrayList<UniDest>();
150         uniDestList.add(uniDest);
151         final EvcAugmentation evcAug = new EvcAugmentationBuilder()
152                                      .setCosId(UnimgrConstants.EVC_PREFIX + 1)
153                                      .setEgressBw((EgressBw) egressBw100M)
154                                      .setIngressBw((IngressBw) ingressBw10M)
155                                      .setUniDest(uniDestList)
156                                      .setUniSource(uniSourceList)
157                                      .build();
158         final Object success = new String("Evc with Source Uni " +IPs+" and destenation Uni " +IPd+" created");
159         final Object error = new String("Error creating new Evc");
160         Object stringReturn = spyEvc.doExecute();
161         assertEquals(success, stringReturn);
162         assertEquals(evcAug, provider.getEvc(null));
163         stringReturn = spyEvc.doExecute();
164         assertEquals(error, stringReturn);
165     }
166
167 }