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.assertNotNull;
14 import static org.junit.Assert.assertTrue;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.Optional;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.Convertor;
22 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ConvertorData;
23 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ParametrizedConvertor;
25 public class ConvertorManagerTest {
26 private static final String CONVERT_INPUT = "10";
27 private static final Integer CONVERT_EXPECTED_RESULT = 10;
28 private static final Short P_CONVERT_INPUT = 0x01;
29 private static final String P_CONVERT_RESULT = "12";
30 private static final Short P_CONVERT_VERSION = 0x02;
32 private Convertor<CharSequence, Integer> convertor;
33 private ParametrizedConvertor<Number, String, TestConvertorData> parametrizedConvertor;
36 public void setUp() throws Exception {
37 convertor = new Convertor<CharSequence, Integer>() {
39 public Class<?> getType() {
40 return CharSequence.class;
44 public Integer convert(CharSequence source) {
45 return Integer.valueOf(source.toString());
49 parametrizedConvertor = new ParametrizedConvertor<Number, String, TestConvertorData>() {
51 public Class<?> getType() {
56 public String convert(Number source, TestConvertorData testConvertorData) {
57 return String.valueOf(source) + String.valueOf(testConvertorData.getVersion());
61 ConvertorManager.getInstance().registerConvertor(convertor);
62 ConvertorManager.getInstance().registerConvertor(parametrizedConvertor);
66 public void testRegisterConvertor() throws Exception {
67 Convertor result = ConvertorManager.getInstance().registerConvertor(convertor);
68 assertNotNull("Convertor should be already registered", result);
72 public void testRegisterParametrizedConvertor() throws Exception {
73 ParametrizedConvertor result = ConvertorManager.getInstance().registerConvertor(parametrizedConvertor);
74 assertNotNull("Parametrized convertor should be already registered", result);
78 public void testConvert() throws Exception {
79 final Optional<Integer> result = ConvertorManager.getInstance().convert(CONVERT_INPUT);
81 assertTrue("Failed to convert string to integer", result.isPresent());
82 assertEquals("Wrong conversion between string and integer", CONVERT_EXPECTED_RESULT, result.get());
86 public void testCollectionConvert() throws Exception {
87 final Optional<List<Boolean>> result = ConvertorManager.getInstance().convert(
88 Collections.singletonList(Boolean.TRUE));
90 assertFalse("Convertor result should be empty on wrong convertor", result.isPresent());
94 public void testEmptyCollectionConvert() throws Exception {
95 final Optional<List<Boolean>> result = ConvertorManager.getInstance().convert(Collections.emptyList());
97 assertFalse("Convertor result should be empty on empty collection", result.isPresent());
101 public void testFailedConvert() throws Exception {
102 final Optional<Integer> result = ConvertorManager.getInstance().convert(null);
104 assertFalse("Convertor result should be empty on null input", result.isPresent());
108 public void testNotFoundConvert() throws Exception {
109 final Optional<Boolean> result = ConvertorManager.getInstance().convert(Boolean.TRUE);
111 assertFalse("Convertor result should be empty on wrong input", result.isPresent());
115 public void testParametrizedConvert() throws Exception {
116 final TestConvertorData data = new TestConvertorData(P_CONVERT_VERSION);
117 final Optional<String> result = ConvertorManager.getInstance().convert(P_CONVERT_INPUT, data);
119 assertTrue("Failed to convert short with data to string", result.isPresent());
120 assertEquals("Wrong conversion between short with data and string", P_CONVERT_RESULT, result.get());
124 public void testCollectionParametrizedConvert() throws Exception {
125 final TestConvertorData data = new TestConvertorData(P_CONVERT_VERSION);
126 final Optional<List<Boolean>> result = ConvertorManager.getInstance().convert(
127 Collections.singletonList(Boolean.TRUE), data);
129 assertFalse("Convertor result should be empty on wrong convertor", result.isPresent());
133 public void testEmptyCollectionParametrizedConvert() throws Exception {
134 final TestConvertorData data = new TestConvertorData(P_CONVERT_VERSION);
135 final Optional<List<Boolean>> result = ConvertorManager.getInstance().convert(Collections.emptyList(), data);
137 assertFalse("Convertor result should be empty on empty collection", result.isPresent());
141 public void testFailedParametrizedConvert() throws Exception {
142 final TestConvertorData data = new TestConvertorData(P_CONVERT_VERSION);
143 final Optional<String> result = ConvertorManager.getInstance().convert(null, data);
145 assertFalse("Parametrized convertor result should be empty on null input", result.isPresent());
149 public void testNotFoundParametrizedConvert() throws Exception {
150 final TestConvertorData data = new TestConvertorData(P_CONVERT_VERSION);
151 final Optional<Boolean> result = ConvertorManager.getInstance().convert(Boolean.TRUE, data);
153 assertFalse("Parametrized convertor result should be empty on wrong input", result.isPresent());
156 private class TestConvertorData extends ConvertorData {
157 TestConvertorData(short version) {