df20423dc652186c4d739decc731ef5ff15ef340
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / ConvertorManagerTest.java
1 /*
2  * Copyright (c) 2016 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.sal.convertor;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertTrue;
14
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;
23
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;
30
31     private Convertor<CharSequence, Integer> convertor;
32     private ParametrizedConvertor<Number, String, TestConvertorData> parametrizedConvertor;
33
34     @Before
35     public void setUp() throws Exception {
36         convertor = new Convertor<CharSequence, Integer>() {
37             @Override
38             public Class<?> getType() {
39                 return CharSequence.class;
40             }
41
42             @Override
43             public Integer convert(CharSequence source) {
44                 return Integer.valueOf(source.toString());
45             }
46         };
47
48         parametrizedConvertor = new ParametrizedConvertor<Number, String, TestConvertorData>() {
49             @Override
50             public Class<?> getType() {
51                 return Number.class;
52             }
53
54             @Override
55             public String convert(Number source, TestConvertorData testConvertorData) {
56                 return String.valueOf(source) + String.valueOf(testConvertorData.getVersion());
57             }
58         };
59
60         ConvertorManager.getInstance().registerConvertor(convertor);
61         ConvertorManager.getInstance().registerConvertor(parametrizedConvertor);
62     }
63
64     @Test
65     public void testRegisterConvertor() throws Exception {
66         boolean success = ConvertorManager.getInstance().registerConvertor(convertor);
67         assertFalse("Convertor should be already registered", success);
68     }
69
70     @Test
71     public void testRegisterParametrizedConvertor() throws Exception {
72         boolean success = ConvertorManager.getInstance().registerConvertor(parametrizedConvertor);
73         assertFalse("Parametrized convertor should be already registered", success);
74     }
75
76     @Test
77     public void testConvert() throws Exception {
78         final Optional<Integer> result = ConvertorManager.getInstance().convert(CONVERT_INPUT);
79
80         assertTrue("Failed to convert string to integer", result.isPresent());
81         assertEquals("Wrong conversion between string and integer", CONVERT_EXPECTED_RESULT, result.get());
82     }
83
84     @Test
85     public void testCollectionConvert() throws Exception {
86         final Optional<List<Boolean>> result = ConvertorManager.getInstance().convert(
87                 Collections.singletonList(Boolean.TRUE));
88
89         assertFalse("Convertor result should be empty on wrong convertor", result.isPresent());
90     }
91
92     @Test
93     public void testEmptyCollectionConvert() throws Exception {
94         final Optional<List<Boolean>> result = ConvertorManager.getInstance().convert(Collections.emptyList());
95
96         assertFalse("Convertor result should be empty on empty collection", result.isPresent());
97     }
98
99     @Test
100     public void testFailedConvert() throws Exception {
101         final Optional<Integer> result = ConvertorManager.getInstance().convert(null);
102
103         assertFalse("Convertor result should be empty on null input", result.isPresent());
104     }
105
106     @Test
107     public void testNotFoundConvert() throws Exception {
108         final Optional<Boolean> result = ConvertorManager.getInstance().convert(Boolean.TRUE);
109
110         assertFalse("Convertor result should be empty on wrong input", result.isPresent());
111     }
112
113     @Test
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);
117
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());
120     }
121
122     @Test
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);
127
128         assertFalse("Convertor result should be empty on wrong convertor", result.isPresent());
129     }
130
131     @Test
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);
135
136         assertFalse("Convertor result should be empty on empty collection", result.isPresent());
137     }
138
139     @Test
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);
143
144         assertFalse("Parametrized convertor result should be empty on null input", result.isPresent());
145     }
146
147     @Test
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);
151
152         assertFalse("Parametrized convertor result should be empty on wrong input", result.isPresent());
153     }
154
155     private class TestConvertorData extends ConvertorData {
156         TestConvertorData(short version) {
157             super(version);
158         }
159     }
160 }