Merge "Fix SwitchIdleEvent echo request-reply xid"
[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.assertTrue;
13
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.Optional;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.mockito.runners.MockitoJUnitRunner;
21 import org.opendaylight.openflowplugin.api.OFConstants;
22 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.Convertor;
23 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionConvertorData;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
26 import org.opendaylight.yangtools.yang.binding.DataContainer;
27
28 /**
29  * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager}
30  */
31 @RunWith(MockitoJUnitRunner.class)
32 public class ConvertorManagerTest {
33     /**
34      * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager#registerConvertor(short, org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.Convertor)}
35      * @throws Exception
36      */
37     @Test
38     public void testRegisterConvertor() throws Exception {
39         final ConvertorManager convertorManager = new ConvertorManager(OFConstants.OFP_VERSION_1_3)
40                 .registerConvertor(OFConstants.OFP_VERSION_1_3, new Convertor<Action, String, VersionConvertorData>() {
41                     @Override
42                     public Collection<Class<? extends DataContainer>> getTypes() {
43                         return Collections.singleton(Action.class);
44                     }
45
46                     @Override
47                     public String convert(Action source, VersionConvertorData data) {
48                         return null;
49                     }
50                 });
51
52         final Optional<Convertor> convertor = convertorManager.findConvertor(OFConstants.OFP_VERSION_1_3, Action.class);
53         assertTrue("Failed to find convertor for action", convertor.isPresent());
54     }
55
56     /**
57      * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager#convert(org.opendaylight.yangtools.yang.binding.DataContainer, org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ConvertorData)}
58      * @throws Exception
59      */
60     @Test
61     public void testConvert() throws Exception {
62         final ConvertorManager convertorManager = new ConvertorManager(OFConstants.OFP_VERSION_1_3)
63                 .registerConvertor(OFConstants.OFP_VERSION_1_3, new Convertor<Action, String, VersionConvertorData>() {
64                     @Override
65                     public Collection<Class<? extends DataContainer>> getTypes() {
66                         return Collections.singleton(Action.class);
67                     }
68
69                     @Override
70                     public String convert(Action source, VersionConvertorData data) {
71                         return String.valueOf(source) + String.valueOf(data);
72                     }
73                 });
74
75         final Action source = new ActionBuilder().build();
76         final VersionConvertorData data = new VersionConvertorData(OFConstants.OFP_VERSION_1_3);
77         final String expectedResult = String.valueOf(source) + String.valueOf(data);
78         final Optional<String> result = convertorManager.convert(source, data);
79
80         assertTrue("Failed to convert action to string", result.isPresent());
81         assertEquals("Result and expected result do not match", result.get(), expectedResult);
82     }
83
84     /**
85      * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager#convert(java.util.Collection, org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ConvertorData)}
86      * @throws Exception
87      */
88     @Test
89     public void testConvert1() throws Exception {
90         final ConvertorManager convertorManager = new ConvertorManager(OFConstants.OFP_VERSION_1_3)
91                 .registerConvertor(OFConstants.OFP_VERSION_1_3, new Convertor<List<Action>, String, VersionConvertorData>() {
92                     @Override
93                     public Collection<Class<? extends DataContainer>> getTypes() {
94                         return Collections.singleton(Action.class);
95                     }
96
97                     @Override
98                     public String convert(List<Action> source, VersionConvertorData data) {
99                         return String.valueOf(source) + String.valueOf(data);
100                     }
101                 });
102
103         final List<Action> source = Collections.singletonList(new ActionBuilder().build());
104         final VersionConvertorData data = new VersionConvertorData(OFConstants.OFP_VERSION_1_3);
105         final String expectedResult = String.valueOf(source) + String.valueOf(data);
106         final Optional<String> result = convertorManager.convert(source, data);
107
108         assertTrue("Failed to convert action to string", result.isPresent());
109         assertEquals("Result and expected result do not match", result.get(), expectedResult);
110     }
111 }