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.openflow.md.core.sal.convertor;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertTrue;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.Optional;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.Convertor;
21 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ConvertorData;
22 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ParametrizedConvertor;
24 public class ConvertorManagerTest {
25 private static final String CONVERT_INPUT = "10";
26 private static final Integer CONVERT_EXPECTED_RESULT = 10;
27 private static final Short P_CONVERT_INPUT = 0x01;
28 private static final String P_CONVERT_RESULT = "12";
29 private static final Short P_CONVERT_VERSION = 0x02;
31 private Convertor<CharSequence, Integer> convertor;
32 private ParametrizedConvertor<Number, String, TestConvertorData> parametrizedConvertor;
35 public void setUp() throws Exception {
36 convertor = new Convertor<CharSequence, Integer>() {
38 public Class<?> getType() {
39 return CharSequence.class;
43 public Integer convert(CharSequence source) {
44 return Integer.valueOf(source.toString());
48 parametrizedConvertor = new ParametrizedConvertor<Number, String, TestConvertorData>() {
50 public Class<?> getType() {
55 public String convert(Number source, TestConvertorData testConvertorData) {
56 return String.valueOf(source) + String.valueOf(testConvertorData.getVersion());
60 ConvertorManager.getInstance().registerConvertor(convertor);
61 ConvertorManager.getInstance().registerConvertor(parametrizedConvertor);
65 public void testRegisterConvertor() throws Exception {
66 boolean success = ConvertorManager.getInstance().registerConvertor(convertor);
67 assertFalse("Convertor should be already registered", success);
71 public void testRegisterParametrizedConvertor() throws Exception {
72 boolean success = ConvertorManager.getInstance().registerConvertor(parametrizedConvertor);
73 assertFalse("Parametrized convertor should be already registered", success);
77 public void testConvert() throws Exception {
78 final Optional<Integer> result = ConvertorManager.getInstance().convert(CONVERT_INPUT);
80 assertTrue("Failed to convert string to integer", result.isPresent());
81 assertEquals("Wrong conversion between string and integer", CONVERT_EXPECTED_RESULT, result.get());
85 public void testCollectionConvert() throws Exception {
86 final Optional<List<Boolean>> result = ConvertorManager.getInstance().convert(
87 Collections.singletonList(Boolean.TRUE));
89 assertFalse("Convertor result should be empty on wrong convertor", result.isPresent());
93 public void testEmptyCollectionConvert() throws Exception {
94 final Optional<List<Boolean>> result = ConvertorManager.getInstance().convert(Collections.emptyList());
96 assertFalse("Convertor result should be empty on empty collection", result.isPresent());
100 public void testFailedConvert() throws Exception {
101 final Optional<Integer> result = ConvertorManager.getInstance().convert(null);
103 assertFalse("Convertor result should be empty on null input", result.isPresent());
107 public void testNotFoundConvert() throws Exception {
108 final Optional<Boolean> result = ConvertorManager.getInstance().convert(Boolean.TRUE);
110 assertFalse("Convertor result should be empty on wrong input", result.isPresent());
114 public void testParametrizedConvert() throws Exception {
115 final TestConvertorData data = new TestConvertorData(P_CONVERT_VERSION);
116 final Optional<String> result = ConvertorManager.getInstance().convert(P_CONVERT_INPUT, data);
118 assertTrue("Failed to convert short with data to string", result.isPresent());
119 assertEquals("Wrong conversion between short with data and string", P_CONVERT_RESULT, result.get());
123 public void testCollectionParametrizedConvert() throws Exception {
124 final TestConvertorData data = new TestConvertorData(P_CONVERT_VERSION);
125 final Optional<List<Boolean>> result = ConvertorManager.getInstance().convert(
126 Collections.singletonList(Boolean.TRUE), data);
128 assertFalse("Convertor result should be empty on wrong convertor", result.isPresent());
132 public void testEmptyCollectionParametrizedConvert() throws Exception {
133 final TestConvertorData data = new TestConvertorData(P_CONVERT_VERSION);
134 final Optional<List<Boolean>> result = ConvertorManager.getInstance().convert(Collections.emptyList(), data);
136 assertFalse("Convertor result should be empty on empty collection", result.isPresent());
140 public void testFailedParametrizedConvert() throws Exception {
141 final TestConvertorData data = new TestConvertorData(P_CONVERT_VERSION);
142 final Optional<String> result = ConvertorManager.getInstance().convert(null, data);
144 assertFalse("Parametrized convertor result should be empty on null input", result.isPresent());
148 public void testNotFoundParametrizedConvert() throws Exception {
149 final TestConvertorData data = new TestConvertorData(P_CONVERT_VERSION);
150 final Optional<Boolean> result = ConvertorManager.getInstance().convert(Boolean.TRUE, data);
152 assertFalse("Parametrized convertor result should be empty on wrong input", result.isPresent());
155 private class TestConvertorData extends ConvertorData {
156 TestConvertorData(short version) {