Bug 1254 - added basic functionality test for MatchExtensionHelper
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / extension / MatchExtensionHelperTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, 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
9 package org.opendaylight.openflowplugin.openflow.md.core.extension;
10
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.Mockito.when;
13
14 import java.util.ArrayList;
15 import java.util.List;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.MockitoAnnotations.Mock;
20 import org.mockito.runners.MockitoJUnitRunner;
21 import org.opendaylight.openflowjava.protocol.api.keys.MatchEntrySerializerKey;
22 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
23 import org.opendaylight.openflowplugin.extension.api.AugmentTuple;
24 import org.opendaylight.openflowplugin.extension.api.ConvertorFromOFJava;
25 import org.opendaylight.openflowplugin.extension.api.ExtensionAugment;
26 import org.opendaylight.openflowplugin.extension.api.path.AugmentationPath;
27 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
28 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MatchField;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OxmClassBase;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntriesBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.Extension;
35 import org.opendaylight.yangtools.yang.binding.Augmentation;
36 import org.opendaylight.yangtools.yang.binding.DataContainer;
37
38 /**
39  * Created by Martin Bobak mbobak@cisco.com on 9/19/14.
40  */
41 @RunWith(MockitoJUnitRunner.class)
42 public class MatchExtensionHelperTest {
43
44     @Mock
45     private ExtensionConverterProvider extensionConverterProvider;
46     private static final int PRESET_COUNT = 7;
47     private static final MatchEntrySerializerKey<? extends OxmClassBase, ? extends MatchField> key = new MatchEntrySerializerKey<>(OpenflowVersion.OF13.getVersion(), MockOxmClassBase.class, MockMatchField.class);
48
49     @Before
50     public void setup() {
51         OFSessionUtil.getSessionManager().setExtensionConverterProvider(extensionConverterProvider);
52         when(extensionConverterProvider.getConverter(key)).thenReturn(new ConvertorFromOFJava<DataContainer, AugmentationPath>() {
53             @Override
54             public ExtensionAugment<? extends Augmentation<Extension>> convert(DataContainer input, AugmentationPath path) {
55                 MockAugmentation mockAugmentation  = new MockAugmentation();
56                 return new ExtensionAugment(MockAugmentation.class, mockAugmentation, MockExtensionKey.class);
57             }
58         });
59     }
60
61
62     @Test
63     /**
64      * Basic functionality test method for {@link org.opendaylight.openflowplugin.openflow.md.core.extension.MatchExtensionHelper#processAllExtensions(java.util.Collection, org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion, org.opendaylight.openflowplugin.extension.api.path.MatchPath)}
65      */
66     public void testProcessAllExtensions() {
67
68         List<MatchEntries> matchEntries = createMatchEntrieses();
69         AugmentTuple augmentTuple = MatchExtensionHelper.processAllExtensions(matchEntries, OpenflowVersion.OF13, MatchPath.FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_MATCH);
70         assertNotNull(augmentTuple);
71
72         augmentTuple = MatchExtensionHelper.processAllExtensions(matchEntries, OpenflowVersion.OF13, MatchPath.PACKETRECEIVED_MATCH);
73         assertNotNull(augmentTuple);
74
75         augmentTuple = MatchExtensionHelper.processAllExtensions(matchEntries, OpenflowVersion.OF13, MatchPath.SWITCHFLOWREMOVED_MATCH);
76         assertNotNull(augmentTuple);
77     }
78
79
80
81     private List<MatchEntries> createMatchEntrieses() {
82         List<MatchEntries> matchEntries = new ArrayList<>();
83         for (int i = 0; i < PRESET_COUNT; i++) {
84             MatchEntriesBuilder matchEntriesBuilder = new MatchEntriesBuilder();
85             matchEntriesBuilder.setHasMask(true);
86             matchEntriesBuilder.setOxmClass(MockOxmClassBase.class);
87             matchEntriesBuilder.setOxmMatchField(MockMatchField.class);
88             matchEntries.add(matchEntriesBuilder.build());
89         }
90         return matchEntries;
91     }
92
93     private static final class MockOxmClassBase extends OxmClassBase {
94
95     }
96
97     private static final class MockMatchField extends MatchField {
98
99     }
100
101     private final class MockExtensionKey extends ExtensionKey {
102
103     }
104
105     private final class MockAugmentation implements Augmentation<Extension> {
106
107     }
108 }