2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.applications.frsync.impl;
11 import java.math.BigInteger;
12 import java.util.concurrent.Future;
13 import java.util.concurrent.TimeUnit;
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.ArgumentCaptor;
20 import org.mockito.Captor;
21 import org.mockito.Matchers;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.runners.MockitoJUnitRunner;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterOutput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterOutputBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterOutput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterOutputBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.SalMeterService;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterOutput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterOutputBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
47 import org.opendaylight.yangtools.yang.common.RpcResult;
48 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
51 * Test for {@link MeterForwarder}.
53 @RunWith(MockitoJUnitRunner.class)
54 public class MeterForwarderTest {
56 private final NodeKey s1Key = new NodeKey(new NodeId("S1"));
57 private final MeterId meterId = new MeterId(42L);
58 private final MeterKey meterKey = new MeterKey(meterId);
59 private final Meter meter = new MeterBuilder()
61 .setMeterName("test-meter")
64 private final KeyedInstanceIdentifier<Node, NodeKey> nodePath = InstanceIdentifier.create(Nodes.class)
65 .child(Node.class, s1Key);
66 private final InstanceIdentifier<FlowCapableNode> flowCapableNodePath = nodePath
67 .augmentation(FlowCapableNode.class);
68 private final InstanceIdentifier<Meter> meterPath = flowCapableNodePath.child(Meter.class, meterKey);
71 private SalMeterService salMeterService;
73 private ArgumentCaptor<AddMeterInput> addMeterInputCpt;
75 private ArgumentCaptor<RemoveMeterInput> removeMeterInputCpt;
77 private ArgumentCaptor<UpdateMeterInput> updateMeterInputCpt;
79 private TransactionId txId;
81 private MeterForwarder meterForwarder;
84 public void setUp() throws Exception {
85 meterForwarder = new MeterForwarder(salMeterService);
86 txId = new TransactionId(BigInteger.ONE);
90 public void testRemove() throws Exception {
91 Mockito.when(salMeterService.removeMeter(removeMeterInputCpt.capture())).thenReturn(
92 RpcResultBuilder.success(new RemoveMeterOutputBuilder()
93 .setTransactionId(txId)
94 .build()).buildFuture()
97 Meter removeMeter = new MeterBuilder(meter).build();
99 final Future<RpcResult<RemoveMeterOutput>> removeResult = meterForwarder.remove(meterPath, removeMeter, flowCapableNodePath);
100 Mockito.verify(salMeterService).removeMeter(Matchers.<RemoveMeterInput>any());
102 Assert.assertTrue(removeResult.isDone());
103 final RpcResult<RemoveMeterOutput> meterResult = removeResult.get(2, TimeUnit.SECONDS);
104 Assert.assertTrue(meterResult.isSuccessful());
106 Assert.assertEquals(1, meterResult.getResult().getTransactionId().getValue().intValue());
108 final RemoveMeterInput removeMeterInput = removeMeterInputCpt.getValue();
109 Assert.assertEquals(meterPath, removeMeterInput.getMeterRef().getValue());
110 Assert.assertEquals(nodePath, removeMeterInput.getNode().getValue());
111 Assert.assertEquals("test-meter", removeMeterInput.getMeterName());
115 public void testUpdate() throws Exception {
116 Mockito.when(salMeterService.updateMeter(updateMeterInputCpt.capture())).thenReturn(
117 RpcResultBuilder.success(new UpdateMeterOutputBuilder()
118 .setTransactionId(txId)
119 .build()).buildFuture()
122 Meter meterOriginal = new MeterBuilder(meter).build();
123 Meter meterUpdate = new MeterBuilder(meter)
124 .setMeterName("another-test")
127 final Future<RpcResult<UpdateMeterOutput>> updateResult = meterForwarder.update(meterPath, meterOriginal, meterUpdate,
128 flowCapableNodePath);
129 Mockito.verify(salMeterService).updateMeter(Matchers.<UpdateMeterInput>any());
131 Assert.assertTrue(updateResult.isDone());
132 final RpcResult<UpdateMeterOutput> meterResult = updateResult.get(2, TimeUnit.SECONDS);
133 Assert.assertTrue(meterResult.isSuccessful());
135 Assert.assertEquals(1, meterResult.getResult().getTransactionId().getValue().intValue());
137 final UpdateMeterInput updateMeterInput = updateMeterInputCpt.getValue();
138 Assert.assertEquals(meterPath, updateMeterInput.getMeterRef().getValue());
139 Assert.assertEquals(nodePath, updateMeterInput.getNode().getValue());
141 Assert.assertEquals("test-meter", updateMeterInput.getOriginalMeter().getMeterName());
142 Assert.assertEquals("another-test", updateMeterInput.getUpdatedMeter().getMeterName());
146 public void testAdd() throws Exception {
147 Mockito.when(salMeterService.addMeter(addMeterInputCpt.capture())).thenReturn(
148 RpcResultBuilder.success(new AddMeterOutputBuilder()
149 .setTransactionId(txId)
150 .build()).buildFuture()
153 final Future<RpcResult<AddMeterOutput>> addResult = meterForwarder.add(meterPath, meter, flowCapableNodePath);
154 Mockito.verify(salMeterService).addMeter(Matchers.<AddMeterInput>any());
156 Assert.assertTrue(addResult.isDone());
157 final RpcResult<AddMeterOutput> meterResult = addResult.get(2, TimeUnit.SECONDS);
158 Assert.assertTrue(meterResult.isSuccessful());
160 Assert.assertEquals(1, meterResult.getResult().getTransactionId().getValue().intValue());
162 final AddMeterInput addMeterInput = addMeterInputCpt.getValue();
163 Assert.assertEquals(meterPath, addMeterInput.getMeterRef().getValue());
164 Assert.assertEquals(nodePath, addMeterInput.getNode().getValue());
165 Assert.assertEquals("test-meter", addMeterInput.getMeterName());