Use ControllerContext non-statically
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / JSONRestconfServiceImplTest.java
index 89d5412ef55d5e03f0d804d7bcfeeb658b657d08..390a87f719fa2f2b0dcd02216b6f75e83f28b2ef 100644 (file)
@@ -19,7 +19,6 @@ import static org.mockito.Matchers.notNull;
 import static org.mockito.Matchers.same;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.reset;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
@@ -29,6 +28,7 @@ import com.google.common.util.concurrent.Futures;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import javax.ws.rs.core.Response.Status;
@@ -40,16 +40,19 @@ import org.mockito.Mockito;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
-import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationNotAvailableException;
 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
+import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
 import org.opendaylight.netconf.sal.restconf.impl.JSONRestconfServiceImpl;
 import org.opendaylight.netconf.sal.restconf.impl.PutResult;
 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
+import org.opendaylight.restconf.common.patch.PatchContext;
+import org.opendaylight.restconf.common.patch.PatchStatusContext;
+import org.opendaylight.restconf.common.patch.PatchStatusEntity;
 import org.opendaylight.yangtools.yang.common.OperationFailedException;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -102,21 +105,27 @@ public class JSONRestconfServiceImplTest {
     static final QName TEST_OUTPUT_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "testOutput");
     static final QName TEXT_OUT_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "textOut");
 
-    private static BrokerFacade brokerFacade;
+    private static SchemaContext schemaContext;
 
-    private final JSONRestconfServiceImpl service = new JSONRestconfServiceImpl();
+    private final BrokerFacade brokerFacade = mock(BrokerFacade.class);
+    private final DOMMountPoint mockMountPoint = mock(DOMMountPoint.class);
+    private JSONRestconfServiceImpl service;
 
     @BeforeClass
     public static void init() throws IOException, ReactorException {
-        ControllerContext.getInstance().setSchemas(TestUtils.loadSchemaContext("/full-versions/yangs"));
-        brokerFacade = mock(BrokerFacade.class);
-        RestconfImpl.getInstance().setBroker(brokerFacade);
-        RestconfImpl.getInstance().setControllerContext(ControllerContext.getInstance());
+        schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
     }
 
     @Before
-    public void setup() {
-        reset(brokerFacade);
+    public void setup() throws FileNotFoundException {
+        final SchemaContext mountPointSchemaContext = TestUtils.loadSchemaContext("/full-versions/test-module");
+        final ControllerContext controllerContext =
+                TestRestconfUtils.newControllerContext(schemaContext, mockMountPoint);
+        doReturn(mountPointSchemaContext).when(mockMountPoint).getSchemaContext();
+
+        service = new JSONRestconfServiceImpl(controllerContext);
+        RestconfImpl.getInstance().setBroker(brokerFacade);
+        RestconfImpl.getInstance().setControllerContext(controllerContext);
     }
 
     private static String loadData(final String path) throws IOException {
@@ -160,7 +169,6 @@ public class JSONRestconfServiceImplTest {
     @SuppressWarnings("rawtypes")
     @Test
     public void testPutBehindMountPoint() throws Exception {
-        final DOMMountPoint mockMountPoint = setupTestMountPoint();
         final PutResult result = mock(PutResult.class);
         when(brokerFacade.commitMountPointDataPut(notNull(DOMMountPoint.class),
                 notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class), Mockito.anyString(),
@@ -229,14 +237,15 @@ public class JSONRestconfServiceImplTest {
         final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
         assertEquals("ContainerNode node type", INTERFACES_QNAME, actualNode.getNodeType());
 
-        final Optional<DataContainerChild<?, ?>> mapChild = actualNode.getChild(new NodeIdentifier(INTERFACE_QNAME));
+        final java.util.Optional<DataContainerChild<?, ?>> mapChild = actualNode.getChild(
+            new NodeIdentifier(INTERFACE_QNAME));
         assertEquals(INTERFACE_QNAME.toString() + " present", true, mapChild.isPresent());
         assertTrue("Expected MapNode. Actual " + mapChild.get().getClass(), mapChild.get() instanceof MapNode);
         final MapNode mapNode = (MapNode)mapChild.get();
 
         final NodeIdentifierWithPredicates entryNodeID = new NodeIdentifierWithPredicates(
                 INTERFACE_QNAME, NAME_QNAME, "eth0");
-        final Optional<MapEntryNode> entryChild = mapNode.getChild(entryNodeID);
+        final java.util.Optional<MapEntryNode> entryChild = mapNode.getChild(entryNodeID);
         assertEquals(entryNodeID.toString() + " present", true, entryChild.isPresent());
         final MapEntryNode entryNode = entryChild.get();
         verifyLeafNode(entryNode, NAME_QNAME, "eth0");
@@ -248,7 +257,6 @@ public class JSONRestconfServiceImplTest {
     @SuppressWarnings("rawtypes")
     @Test
     public void testPostBehindMountPoint() throws Exception {
-        final DOMMountPoint mockMountPoint = setupTestMountPoint();
         doReturn(Futures.immediateCheckedFuture(null)).when(brokerFacade).commitConfigurationDataPost(
                 notNull(DOMMountPoint.class), notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class),
                 Mockito.anyString(), Mockito.anyString());
@@ -274,7 +282,7 @@ public class JSONRestconfServiceImplTest {
     }
 
     @Test(expected = TransactionCommitFailedException.class)
-    @SuppressWarnings("checkstyle:IllegalThrows")
+    @SuppressWarnings({ "checkstyle:IllegalThrows", "checkstyle:avoidHidingCauseException" })
     public void testPostFailure() throws Throwable {
         doReturn(Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("mock"))).when(brokerFacade)
                 .commitConfigurationDataPost(any(SchemaContext.class), any(YangInstanceIdentifier.class),
@@ -291,6 +299,73 @@ public class JSONRestconfServiceImplTest {
         }
     }
 
+    @SuppressWarnings("rawtypes")
+    @Test
+    public void testPatch() throws Exception {
+        final PatchStatusContext result = mock(PatchStatusContext.class);
+        when(brokerFacade.patchConfigurationDataWithinTransaction(notNull(PatchContext.class)))
+            .thenReturn(result);
+
+        List<PatchStatusEntity> patchSTatus = new ArrayList<>();
+
+        PatchStatusEntity entity = new PatchStatusEntity("edit1", true, null);
+
+        patchSTatus.add(entity);
+
+        when(result.getEditCollection())
+                .thenReturn(patchSTatus);
+        when(result.getGlobalErrors()).thenReturn(new ArrayList<>());
+        when(result.getPatchId()).thenReturn("1");
+        final String uriPath = "ietf-interfaces:interfaces/interface/eth0";
+        final String payload = loadData("/parts/ietf-interfaces_interfaces_patch.json");
+        final Optional<String> patchResult = this.service.patch(uriPath, payload);
+
+        assertTrue(patchResult.get().contains("\"ok\":[null]"));
+    }
+
+    @SuppressWarnings("rawtypes")
+    @Test
+    public void testPatchBehindMountPoint() throws Exception {
+        final PatchStatusContext result = mock(PatchStatusContext.class);
+        when(brokerFacade.patchConfigurationDataWithinTransaction(notNull(PatchContext.class)))
+            .thenReturn(result);
+
+        List<PatchStatusEntity> patchSTatus = new ArrayList<>();
+
+        PatchStatusEntity entity = new PatchStatusEntity("edit1", true, null);
+
+        patchSTatus.add(entity);
+
+        when(result.getEditCollection())
+                .thenReturn(patchSTatus);
+        when(result.getGlobalErrors()).thenReturn(new ArrayList<>());
+        when(result.getPatchId()).thenReturn("1");
+
+        final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1";
+        final String payload = loadData("/full-versions/testCont1DataPatch.json");
+
+        final Optional<String> patchResult = this.service.patch(uriPath, payload);
+
+        assertTrue(patchResult.get().contains("\"ok\":[null]"));
+    }
+
+    @Test(expected = OperationFailedException.class)
+    @SuppressWarnings("checkstyle:IllegalThrows")
+    public void testPatchFailure() throws Throwable {
+        final PatchStatusContext result = mock(PatchStatusContext.class);
+        when(brokerFacade.patchConfigurationDataWithinTransaction(notNull(PatchContext.class)))
+            .thenThrow(new TransactionCommitFailedException("Transaction failed"));
+
+        final String uriPath = "ietf-interfaces:interfaces/interface/eth0";
+        final String payload = loadData("/parts/ietf-interfaces_interfaces_patch.json");
+
+        final Optional<String> patchResult = this.service.patch(uriPath, payload);
+
+        assertTrue("Patch output is not null", patchResult.isPresent());
+        String patch = patchResult.get();
+        assertTrue(patch.contains("TransactionCommitFailedException"));
+    }
+
     @Test
     public void testDelete() throws Exception {
         doReturn(Futures.immediateCheckedFuture(null)).when(brokerFacade)
@@ -454,21 +529,8 @@ public class JSONRestconfServiceImplTest {
                 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
     }
 
-    DOMMountPoint setupTestMountPoint() throws FileNotFoundException, ReactorException {
-        final SchemaContext schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
-        final DOMMountPoint mockMountPoint = mock(DOMMountPoint.class);
-        doReturn(schemaContextTestModule).when(mockMountPoint).getSchemaContext();
-
-        final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
-        doReturn(Optional.of(mockMountPoint))
-                .when(mockMountService).getMountPoint(notNull(YangInstanceIdentifier.class));
-
-        ControllerContext.getInstance().setMountService(mockMountService);
-        return mockMountPoint;
-    }
-
     void verifyLeafNode(final DataContainerNode<?> parent, final QName leafType, final Object leafValue) {
-        final Optional<DataContainerChild<?, ?>> leafChild = parent.getChild(new NodeIdentifier(leafType));
+        final java.util.Optional<DataContainerChild<?, ?>> leafChild = parent.getChild(new NodeIdentifier(leafType));
         assertEquals(leafType.toString() + " present", true, leafChild.isPresent());
         assertEquals(leafType.toString() + " value", leafValue, leafChild.get().getValue());
     }