Post "Clustering optimization" updates
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / translator / NotificationPlainTranslatorTest.java
1 /*\r
2  * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 \r
9 package org.opendaylight.openflowplugin.openflow.md.core.translator;\r
10 \r
11 import java.math.BigInteger;\r
12 import java.util.List;\r
13 \r
14 import static org.mockito.Mockito.when;\r
15 \r
16 import org.junit.Assert;\r
17 import org.junit.Before;\r
18 import org.junit.Test;\r
19 import org.mockito.Mock;\r
20 import org.mockito.MockitoAnnotations;\r
21 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;\r
22 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;\r
23 import org.opendaylight.openflowplugin.api.openflow.md.core.NotificationQueueWrapper;\r
24 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessageBuilder;\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;\r
29 import org.opendaylight.yangtools.yang.binding.DataObject;\r
30 \r
31 /**\r
32  * @author michal.polkorab\r
33  *\r
34  */\r
35 public class NotificationPlainTranslatorTest {\r
36 \r
37     @Mock SwitchConnectionDistinguisher cookie;\r
38     @Mock SessionContext sc;\r
39     @Mock GetFeaturesOutput features;\r
40 \r
41     NotificationPlainTranslator translator = new NotificationPlainTranslator();\r
42 \r
43     /**\r
44      * Initializes mocks\r
45      */\r
46     @Before\r
47     public void startUp() {\r
48         MockitoAnnotations.initMocks(this);\r
49     }\r
50 \r
51     /**\r
52      * Tests {@link NotificationPlainTranslator#translate(SwitchConnectionDistinguisher, SessionContext, OfHeader)}\r
53      */\r
54     @Test\r
55     public void testIncorrectInput() {\r
56         HelloMessageBuilder helloBuilder = new HelloMessageBuilder();\r
57         HelloMessage message = helloBuilder.build();\r
58 \r
59         List<DataObject> list = translator.translate(cookie, sc, message);\r
60 \r
61         Assert.assertEquals("Wrong list size", 0, list.size());\r
62     }\r
63 \r
64     /**\r
65      * Tests {@link NotificationPlainTranslator#translate(SwitchConnectionDistinguisher, SessionContext, OfHeader)}\r
66      */\r
67     @Test\r
68     public void test() {\r
69         when(sc.getFeatures()).thenReturn(features);\r
70         when(features.getDatapathId()).thenReturn(new BigInteger("64"));\r
71         HelloMessageBuilder helloBuilder = new HelloMessageBuilder();\r
72         helloBuilder.setVersion((short) EncodeConstants.OF13_VERSION_ID);\r
73         helloBuilder.setXid(42L);\r
74         HelloMessage message = helloBuilder.build();\r
75         NotificationQueueWrapper wrapper = new NotificationQueueWrapper(message, message.getVersion());\r
76 \r
77         List<DataObject> list = translator.translate(cookie, sc, wrapper);\r
78 \r
79         Assert.assertEquals("Wrong list size", 1, list.size());\r
80         HelloMessage hello = (HelloMessage) list.get(0);\r
81         Assert.assertEquals("Wrong output", 4, hello.getVersion().intValue());\r
82         Assert.assertEquals("Wrong output", 42, hello.getXid().intValue());\r
83     }\r
84 }\r