725bea39986f3b62422a777b2d5d55f08b88ffea
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / translator / ErrorTranslatorTest.java
1 /**
2  * Copyright (c) 2013 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
12 import java.lang.reflect.Method;
13 import java.util.List;
14 import org.junit.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.mockito.MockitoAnnotations;
18 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
19 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.errors.rev131116.ErrorMessage;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.errors.rev131116.ErrorType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessageBuilder;
24 import org.opendaylight.yangtools.yang.binding.DataObject;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  *
30  */
31 public class ErrorTranslatorTest {
32
33     private final ErrorTranslator errorTranslator = new ErrorTranslator();
34     private final ErrorMessageBuilder builder = new ErrorMessageBuilder();;
35     private static Logger LOG = LoggerFactory
36             .getLogger(ErrorTranslatorTest.class);
37
38     @MockitoAnnotations.Mock
39     SwitchConnectionDistinguisher cookie;
40     @MockitoAnnotations.Mock
41     SessionContext sc;
42
43     /**
44      * startup method
45      */
46     @Before
47     public void setUp() {
48         builder.setCode(21);
49         builder.setXid(42L);
50         builder.setData(new byte[]{42});
51
52     }
53
54
55     @Test
56     public void testTranslate() {
57         builder.setType(1);
58         List<DataObject> data = errorTranslator.translate(cookie, sc, builder.build());
59         assertNotNull(data);
60     }
61
62     /**
63      * 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)}.
64      *
65      * @throws Exception
66      */
67     @Test
68     public void testGetGranularNodeErrors() throws Exception {
69         for (ErrorType eType : ErrorType.values()) {
70             builder.setType(eType.getIntValue());
71             ErrorMessage errorMessage = errorTranslator.getGranularNodeErrors(builder.build(), eType);
72             LOG.debug("translating errorMessage of type {}", eType);
73             assertNotNull("translated error is null", errorMessage);
74             Assert.assertEquals(21, errorMessage.getCode().intValue());
75             Assert.assertEquals(eType, errorMessage.getType());
76             Method getXid = errorMessage.getClass().getMethod("getTransactionId", new Class[0]);
77             getXid.setAccessible(true);
78             TransactionId xid = (TransactionId) getXid.invoke(errorMessage, new Object[0]);
79             Assert.assertEquals(42L, xid.getValue().longValue());
80             assertNotNull("data is null", errorMessage.getData());
81         }
82     }
83
84     /**
85      * Test method for {@link org.opendaylight.openflowplugin.openflow.md.core.translator.ErrorTranslator#decodeErrorType(int)}.
86      */
87     @Test
88     public void testDecodeErrorType() {
89         for (ErrorType eType : ErrorType.values()) {
90             ErrorType result = errorTranslator.decodeErrorType(eType.getIntValue());
91             Assert.assertEquals(eType, result);
92         }
93     }
94
95 }