Bug 2388: Set node reference into OFPT_ERROR notification.
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / translator / ErrorTranslatorTest.java
index 40e8269004110ba3eb816e744132fd387b9d6ee3..b63380cdc5377e3cbf9c58da4e25326d01d16003 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2013-2014 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -8,19 +8,26 @@
 package org.opendaylight.openflowplugin.openflow.md.core.translator;
 
 import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.when;
 
 import java.lang.reflect.Method;
+import java.math.BigInteger;
 import java.util.List;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
+import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
+import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.errors.rev131116.ErrorMessage;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.errors.rev131116.ErrorType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.BaseNodeErrorNotification;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessageBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -32,13 +39,16 @@ public class ErrorTranslatorTest {
 
     private final ErrorTranslator errorTranslator = new ErrorTranslator();
     private final ErrorMessageBuilder builder = new ErrorMessageBuilder();;
+    private static final BigInteger  DATAPATH_ID = BigInteger.valueOf(0x7777L);
     private static Logger LOG = LoggerFactory
             .getLogger(ErrorTranslatorTest.class);
 
-    @MockitoAnnotations.Mock
+    @Mock
     SwitchConnectionDistinguisher cookie;
-    @MockitoAnnotations.Mock
+    @Mock
     SessionContext sc;
+    @Mock
+    GetFeaturesOutput features;
 
     /**
      * startup method
@@ -49,6 +59,9 @@ public class ErrorTranslatorTest {
         builder.setXid(42L);
         builder.setData(new byte[]{42});
 
+        MockitoAnnotations.initMocks(this);
+        when(sc.getFeatures()).thenReturn(features);
+        when(features.getDatapathId()).thenReturn(DATAPATH_ID);
     }
 
 
@@ -57,22 +70,35 @@ public class ErrorTranslatorTest {
         builder.setType(1);
         List<DataObject> data = errorTranslator.translate(cookie, sc, builder.build());
         assertNotNull(data);
+        Assert.assertEquals(1, data.size());
+        DataObject obj = data.get(0);
+        Assert.assertTrue(obj instanceof BaseNodeErrorNotification);
+        BaseNodeErrorNotification nodeError = (BaseNodeErrorNotification)obj;
+        NodeRef expectedNode = new NodeRef(
+            InventoryDataServiceUtil.identifierFromDatapathId(DATAPATH_ID));
+        Assert.assertEquals(expectedNode, nodeError.getNode());
     }
 
     /**
-     * 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)}.
+     * 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)}.
      *
      * @throws Exception
      */
     @Test
     public void testGetGranularNodeErrors() throws Exception {
+        BigInteger dpid = BigInteger.valueOf(0x1122334455667788L);
+        NodeRef node = new NodeRef(
+            InventoryDataServiceUtil.identifierFromDatapathId(dpid));
         for (ErrorType eType : ErrorType.values()) {
             builder.setType(eType.getIntValue());
-            ErrorMessage errorMessage = errorTranslator.getGranularNodeErrors(builder.build(), eType);
+            ErrorMessage errorMessage = errorTranslator.getGranularNodeErrors(builder.build(), eType, node);
             LOG.debug("translating errorMessage of type {}", eType);
             assertNotNull("translated error is null", errorMessage);
             Assert.assertEquals(21, errorMessage.getCode().intValue());
             Assert.assertEquals(eType, errorMessage.getType());
+            Method getNode = errorMessage.getClass().getMethod("getNode");
+            getNode.setAccessible(true);
+            Assert.assertEquals(node, getNode.invoke(errorMessage));
             Method getXid = errorMessage.getClass().getMethod("getTransactionId", new Class[0]);
             getXid.setAccessible(true);
             TransactionId xid = (TransactionId) getXid.invoke(errorMessage, new Object[0]);