External api proposal
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / translator / ErrorTranslatorTest.java
1 /**
2  * Copyright (c) 2013-2014 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 package org.opendaylight.openflowplugin.openflow.md.core.translator;
9
10 import static org.junit.Assert.assertNotNull;
11 import static org.mockito.Mockito.when;
12
13 import java.lang.reflect.Method;
14 import java.math.BigInteger;
15 import java.util.List;
16
17 import org.junit.Assert;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.Mock;
21 import org.mockito.MockitoAnnotations;
22 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
23 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
24 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.errors.rev131116.ErrorMessage;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.errors.rev131116.ErrorType;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.BaseNodeErrorNotification;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessageBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
32 import org.opendaylight.yangtools.yang.binding.DataObject;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  *
38  */
39 public class ErrorTranslatorTest {
40
41     private final ErrorTranslator errorTranslator = new ErrorTranslator();
42     private final ErrorMessageBuilder builder = new ErrorMessageBuilder();;
43     private static final BigInteger  DATAPATH_ID = BigInteger.valueOf(0x7777L);
44     private static Logger LOG = LoggerFactory
45             .getLogger(ErrorTranslatorTest.class);
46
47     @Mock
48     SwitchConnectionDistinguisher cookie;
49     @Mock
50     SessionContext sc;
51     @Mock
52     GetFeaturesOutput features;
53
54     /**
55      * startup method
56      */
57     @Before
58     public void setUp() {
59         builder.setCode(21);
60         builder.setXid(42L);
61         builder.setData(new byte[]{42});
62
63         MockitoAnnotations.initMocks(this);
64         when(sc.getFeatures()).thenReturn(features);
65         when(features.getDatapathId()).thenReturn(DATAPATH_ID);
66     }
67
68
69     @Test
70     public void testTranslate() {
71         builder.setType(1);
72         List<DataObject> data = errorTranslator.translate(cookie, sc, builder.build());
73         assertNotNull(data);
74         Assert.assertEquals(1, data.size());
75         DataObject obj = data.get(0);
76         Assert.assertTrue(obj instanceof BaseNodeErrorNotification);
77         BaseNodeErrorNotification nodeError = (BaseNodeErrorNotification)obj;
78         NodeRef expectedNode = new NodeRef(
79             InventoryDataServiceUtil.identifierFromDatapathId(DATAPATH_ID));
80         Assert.assertEquals(expectedNode, nodeError.getNode());
81     }
82
83     /**
84      * Test method for {@link org.opendaylight.openflowplugin.openflow.md.core.translator.ErrorTranslator#getGranularNodeErrors(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage, org.opendaylight.yang.gen.v1.urn.opendaylight.flow.errors.rev131116.ErrorType, NodeRef)}.
85      *
86      * @throws Exception
87      */
88     @Test
89     public void testGetGranularNodeErrors() throws Exception {
90         BigInteger dpid = BigInteger.valueOf(0x1122334455667788L);
91         NodeRef node = new NodeRef(
92             InventoryDataServiceUtil.identifierFromDatapathId(dpid));
93         for (ErrorType eType : ErrorType.values()) {
94             builder.setType(eType.getIntValue());
95             ErrorMessage errorMessage = errorTranslator.getGranularNodeErrors(builder.build(), eType, node);
96             LOG.debug("translating errorMessage of type {}", eType);
97             assertNotNull("translated error is null", errorMessage);
98             Assert.assertEquals(21, errorMessage.getCode().intValue());
99             Assert.assertEquals(eType, errorMessage.getType());
100             Method getNode = errorMessage.getClass().getMethod("getNode");
101             getNode.setAccessible(true);
102             Assert.assertEquals(node, getNode.invoke(errorMessage));
103             Method getXid = errorMessage.getClass().getMethod("getTransactionId", new Class[0]);
104             getXid.setAccessible(true);
105             TransactionId xid = (TransactionId) getXid.invoke(errorMessage, new Object[0]);
106             Assert.assertEquals(42L, xid.getValue().longValue());
107             assertNotNull("data is null", errorMessage.getData());
108         }
109     }
110
111     /**
112      * Test method for {@link org.opendaylight.openflowplugin.openflow.md.core.translator.ErrorTranslator#decodeErrorType(int)}.
113      */
114     @Test
115     public void testDecodeErrorType() {
116         for (ErrorType eType : ErrorType.values()) {
117             ErrorType result = errorTranslator.decodeErrorType(eType.getIntValue());
118             Assert.assertEquals(eType, result);
119         }
120     }
121
122 }