fix add and update commands
[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.EgressBwBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.IngressBw;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.IngressBwBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDest;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDestBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDestKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSource;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSourceBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSourceKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed100M;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed100MBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10G;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10GBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10M;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10MBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed1G;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed1GBuilder;
45 import org.powermock.api.mockito.PowerMockito;
46 import org.powermock.api.support.membermodification.MemberModifier;
47 import org.powermock.core.classloader.annotations.PrepareForTest;
48 import org.powermock.modules.junit4.PowerMockRunner;
49 import org.powermock.reflect.Whitebox;
50
51 @RunWith(PowerMockRunner.class)
52 @PrepareForTest(EvcAddShellCommand.class)
53 public class EvcAddShellCommandTest {
54
55     private final String IPs = "192.168.1.1";
56     private final String IPd = "192.168.1.2";
57     private final String egress = "100M";
58     private final String ingress = "10M";
59
60     private final UnimgrConsoleProviderTest provider = new UnimgrConsoleProviderTest();
61
62     @Mock EvcAddShellCommand evcAddShellCommand;
63
64     @Before
65     public void setUp() throws IllegalArgumentException, IllegalAccessException{
66         evcAddShellCommand = mock(EvcAddShellCommand.class, Mockito.CALLS_REAL_METHODS);
67         MemberModifier.field(EvcAddShellCommand.class, "provider").set(evcAddShellCommand, provider);
68
69     }
70
71     /**
72      * Test method for {@link org.opendaylight.unimgr.cli.EvcAddShellCommand#doExecute()}.
73      * @throws Exception
74      */
75     @Test
76     public void testDoExecute() throws Exception {
77         final EvcAddShellCommand spyEvc = PowerMockito.spy(new EvcAddShellCommand(provider));
78         Whitebox.setInternalState(spyEvc, "IPs", IPs);
79         Whitebox.setInternalState(spyEvc, "IPd", IPd);
80         Whitebox.setInternalState(spyEvc, "egress", egress);
81         Whitebox.setInternalState(spyEvc, "ingress", ingress);
82         final Short order = new Short("0");
83         final IpAddress ipAddreSource = new IpAddress(IPs.toCharArray());
84         final UniSource uniSource = new UniSourceBuilder()
85                                   .setIpAddress(ipAddreSource)
86                                   .setKey(new UniSourceKey(order))
87                                   .setOrder(order)
88                                   .build();
89         final List<UniSource> uniSourceList = new ArrayList<UniSource>();
90         uniSourceList.add(uniSource);
91         final IpAddress ipAddreDest = new IpAddress(IPd.toCharArray());
92         final UniDest uniDest = new UniDestBuilder()
93                           .setOrder(order)
94                           .setKey(new UniDestKey(order))
95                           .setIpAddress(ipAddreDest)
96                           .build();
97         final List<UniDest> uniDestList = new ArrayList<UniDest>();
98         uniDestList.add(uniDest);
99         final EvcAugmentation evcAug = new EvcAugmentationBuilder()
100                                      .setCosId(UnimgrConstants.EVC_PREFIX + 1)
101                                      .setEgressBw(new EgressBwBuilder().setSpeed(Utils.getSpeed(egress)).build())
102                                      .setIngressBw(new IngressBwBuilder().setSpeed(Utils.getSpeed(ingress)).build())
103                                      .setUniDest(uniDestList)
104                                      .setUniSource(uniSourceList)
105                                      .build();
106         final Object success = new String("Evc with Source Uni " +IPs+" and destenation Uni " +IPd+" created");
107         final Object error = new String("Error creating new Evc");
108         Object stringReturn = spyEvc.doExecute();
109         assertEquals(success, stringReturn);
110         assertEquals(evcAug, provider.getEvc(null));
111         stringReturn = spyEvc.doExecute();
112         assertEquals(error, stringReturn);
113     }
114
115 }