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