Migrate MockitoJUnitRunner
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / ConvertorManagerTest.java
index a35f08516ef7c4f3cd8c8e53c928ee76da9b1071..e6167f2966b4cade27b933916a5319ef93ea54a5 100644 (file)
 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
+import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 import java.util.Optional;
-import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.opendaylight.openflowplugin.api.OFConstants;
 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.Convertor;
 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ConvertorData;
-import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ParametrizedConvertor;
+import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionConvertorData;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
 
+/**
+ * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager}.
+ */
+@RunWith(MockitoJUnitRunner.class)
 public class ConvertorManagerTest {
-    private static final String CONVERT_INPUT = "10";
-    private static final Integer CONVERT_EXPECTED_RESULT = 10;
-    private static final Short P_CONVERT_INPUT = 0x01;
-    private static final String P_CONVERT_RESULT = "12";
-    private static final Short P_CONVERT_VERSION = 0x02;
-
-    private Convertor<CharSequence, Integer> convertor;
-    private ParametrizedConvertor<Number, String, TestConvertorData> parametrizedConvertor;
-
-    @Before
-    public void setUp() throws Exception {
-        convertor = new Convertor<CharSequence, Integer>() {
-            @Override
-            public Class<?> getType() {
-                return CharSequence.class;
-            }
-
-            @Override
-            public Integer convert(CharSequence source) {
-                return Integer.valueOf(source.toString());
-            }
-        };
-
-        parametrizedConvertor = new ParametrizedConvertor<Number, String, TestConvertorData>() {
-            @Override
-            public Class<?> getType() {
-                return Number.class;
-            }
-
-            @Override
-            public String convert(Number source, TestConvertorData testConvertorData) {
-                return String.valueOf(source) + String.valueOf(testConvertorData.getVersion());
-            }
-        };
-
-        ConvertorManager.getInstance().registerConvertor(convertor);
-        ConvertorManager.getInstance().registerConvertor(parametrizedConvertor);
-    }
-
-    @Test
-    public void testRegisterConvertor() throws Exception {
-        Convertor result = ConvertorManager.getInstance().registerConvertor(convertor);
-        assertNotNull("Convertor should be already registered", result);
-    }
-
-    @Test
-    public void testRegisterParametrizedConvertor() throws Exception {
-        ParametrizedConvertor result = ConvertorManager.getInstance().registerConvertor(parametrizedConvertor);
-        assertNotNull("Parametrized convertor should be already registered", result);
-    }
-
-    @Test
-    public void testConvert() throws Exception {
-        final Optional<Integer> result = ConvertorManager.getInstance().convert(CONVERT_INPUT);
-
-        assertTrue("Failed to convert string to integer", result.isPresent());
-        assertEquals("Wrong conversion between string and integer", CONVERT_EXPECTED_RESULT, result.get());
-    }
-
-    @Test
-    public void testCollectionConvert() throws Exception {
-        final Optional<List<Boolean>> result = ConvertorManager.getInstance().convert(
-                Collections.singletonList(Boolean.TRUE));
-
-        assertFalse("Convertor result should be empty on wrong convertor", result.isPresent());
-    }
-
-    @Test
-    public void testEmptyCollectionConvert() throws Exception {
-        final Optional<List<Boolean>> result = ConvertorManager.getInstance().convert(Collections.emptyList());
-
-        assertFalse("Convertor result should be empty on empty collection", result.isPresent());
-    }
-
-    @Test
-    public void testFailedConvert() throws Exception {
-        final Optional<Integer> result = ConvertorManager.getInstance().convert(null);
-
-        assertFalse("Convertor result should be empty on null input", result.isPresent());
-    }
-
-    @Test
-    public void testNotFoundConvert() throws Exception {
-        final Optional<Boolean> result = ConvertorManager.getInstance().convert(Boolean.TRUE);
-
-        assertFalse("Convertor result should be empty on wrong input", result.isPresent());
-    }
-
-    @Test
-    public void testParametrizedConvert() throws Exception {
-        final TestConvertorData data = new TestConvertorData(P_CONVERT_VERSION);
-        final Optional<String> result = ConvertorManager.getInstance().convert(P_CONVERT_INPUT, data);
-
-        assertTrue("Failed to convert short with data to string", result.isPresent());
-        assertEquals("Wrong conversion between short with data and string", P_CONVERT_RESULT, result.get());
-    }
-
-    @Test
-    public void testCollectionParametrizedConvert() throws Exception {
-        final TestConvertorData data = new TestConvertorData(P_CONVERT_VERSION);
-        final Optional<List<Boolean>> result = ConvertorManager.getInstance().convert(
-                Collections.singletonList(Boolean.TRUE), data);
-
-        assertFalse("Convertor result should be empty on wrong convertor", result.isPresent());
-    }
-
     @Test
-    public void testEmptyCollectionParametrizedConvert() throws Exception {
-        final TestConvertorData data = new TestConvertorData(P_CONVERT_VERSION);
-        final Optional<List<Boolean>> result = ConvertorManager.getInstance().convert(Collections.emptyList(), data);
-
-        assertFalse("Convertor result should be empty on empty collection", result.isPresent());
+    public void testRegisterConvertor() {
+        final ConvertorManager convertorManager = new ConvertorManager(OFConstants.OFP_VERSION_1_3)
+                .registerConvertor(OFConstants.OFP_VERSION_1_3, new Convertor<Action, String, VersionConvertorData>() {
+                    @Override
+                    public Collection<Class<?>> getTypes() {
+                        return Collections.singleton(Action.class);
+                    }
+
+                    @Override
+                    public String convert(Action source, VersionConvertorData data) {
+                        return null;
+                    }
+                });
+
+        final Optional<Convertor> convertor = convertorManager.findConvertor(OFConstants.OFP_VERSION_1_3, Action.class);
+        assertTrue("Failed to find convertor for action", convertor.isPresent());
     }
 
     @Test
-    public void testFailedParametrizedConvert() throws Exception {
-        final TestConvertorData data = new TestConvertorData(P_CONVERT_VERSION);
-        final Optional<String> result = ConvertorManager.getInstance().convert(null, data);
-
-        assertFalse("Parametrized convertor result should be empty on null input", result.isPresent());
+    public void testConvert() {
+        final ConvertorManager convertorManager = new ConvertorManager(OFConstants.OFP_VERSION_1_3)
+                .registerConvertor(OFConstants.OFP_VERSION_1_3, new Convertor<Action, String, VersionConvertorData>() {
+                    @Override
+                    public Collection<Class<?>> getTypes() {
+                        return Collections.singleton(Action.class);
+                    }
+
+                    @Override
+                    public String convert(Action source, VersionConvertorData data) {
+                        return String.valueOf(source) + String.valueOf(data);
+                    }
+                });
+
+        final Action source = new ActionBuilder().build();
+        final VersionConvertorData data = new VersionConvertorData(OFConstants.OFP_VERSION_1_3);
+        final String expectedResult = String.valueOf(source) + String.valueOf(data);
+        final Optional<String> result = convertorManager.convert(source, data);
+
+        assertTrue("Failed to convert action to string", result.isPresent());
+        assertEquals("Result and expected result do not match", result.get(), expectedResult);
     }
 
+    /**
+     * Test for {@link ConvertorManager#convert(Collection, ConvertorData)}.
+     */
     @Test
-    public void testNotFoundParametrizedConvert() throws Exception {
-        final TestConvertorData data = new TestConvertorData(P_CONVERT_VERSION);
-        final Optional<Boolean> result = ConvertorManager.getInstance().convert(Boolean.TRUE, data);
-
-        assertFalse("Parametrized convertor result should be empty on wrong input", result.isPresent());
-    }
-
-    private class TestConvertorData extends ConvertorData {
-        TestConvertorData(short version) {
-            super(version);
-        }
+    public void testConvert1() {
+        final ConvertorManager convertorManager = new ConvertorManager(OFConstants.OFP_VERSION_1_3)
+            .registerConvertor(OFConstants.OFP_VERSION_1_3,
+                new Convertor<List<Action>, String, VersionConvertorData>() {
+                    @Override
+                    public Collection<Class<?>> getTypes() {
+                        return Collections.singleton(Action.class);
+                    }
+
+                    @Override
+                    public String convert(List<Action> source, VersionConvertorData data) {
+                        return String.valueOf(source) + String.valueOf(data);
+                    }
+                });
+
+        final List<Action> source = Collections.singletonList(new ActionBuilder().build());
+        final VersionConvertorData data = new VersionConvertorData(OFConstants.OFP_VERSION_1_3);
+        final String expectedResult = String.valueOf(source) + String.valueOf(data);
+        final Optional<String> result = convertorManager.convert(source, data);
+
+        assertTrue("Failed to convert action to string", result.isPresent());
+        assertEquals("Result and expected result do not match", result.get(), expectedResult);
     }
-}
\ No newline at end of file
+}