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