Update MRI projects for Aluminium
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / rests / utils / ReadDataTransactionUtilTest.java
index 718f050fe09ad993f045b64b85b3096a46712133..995073f8605142cef2143150fa8219c2deafe66e 100644 (file)
@@ -5,20 +5,21 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.restconf.nb.rfc8040.rests.utils;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.when;
+import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
 
-import com.google.common.base.Optional;
 import com.google.common.collect.ImmutableList;
-import com.google.common.util.concurrent.Futures;
 import java.util.Collections;
+import java.util.Optional;
 import javax.ws.rs.core.MultivaluedHashMap;
 import javax.ws.rs.core.UriInfo;
 import org.junit.Before;
@@ -26,10 +27,10 @@ import org.junit.Test;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
-import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+import org.opendaylight.mdsal.dom.api.DOMDataBroker;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
+import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
 import org.opendaylight.restconf.common.context.WriterParameters;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
@@ -37,6 +38,7 @@ import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.TransactionVarsWrapper;
+import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfDataServiceConstant.ReadData;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
@@ -47,8 +49,8 @@ import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 
 public class ReadDataTransactionUtilTest {
 
@@ -62,9 +64,9 @@ public class ReadDataTransactionUtilTest {
     @Mock
     private InstanceIdentifierContext<ContainerSchemaNode> context;
     @Mock
-    private DOMDataReadOnlyTransaction read;
+    private DOMDataTreeReadTransaction read;
     @Mock
-    private SchemaContext schemaContext;
+    private EffectiveModelContext schemaContext;
     @Mock
     private ContainerSchemaNode containerSchemaNode;
     @Mock
@@ -91,7 +93,7 @@ public class ReadDataTransactionUtilTest {
 
     @Test
     public void readDataConfigTest() {
-        doReturn(Futures.immediateCheckedFuture(Optional.of(DATA.data3))).when(read)
+        doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(read)
                 .read(LogicalDatastoreType.CONFIGURATION, DATA.path);
         doReturn(DATA.path).when(context).getInstanceIdentifier();
         final String valueOfContent = RestconfDataServiceConstant.ReadData.CONFIG;
@@ -102,9 +104,9 @@ public class ReadDataTransactionUtilTest {
 
     @Test
     public void readAllHavingOnlyConfigTest() {
-        doReturn(Futures.immediateCheckedFuture(Optional.of(DATA.data3))).when(read)
+        doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(read)
                 .read(LogicalDatastoreType.CONFIGURATION, DATA.path);
-        doReturn(Futures.immediateCheckedFuture(Optional.absent())).when(read)
+        doReturn(immediateFluentFuture(Optional.empty())).when(read)
                 .read(LogicalDatastoreType.OPERATIONAL, DATA.path);
         doReturn(DATA.path).when(context).getInstanceIdentifier();
         final String valueOfContent = RestconfDataServiceConstant.ReadData.ALL;
@@ -115,9 +117,9 @@ public class ReadDataTransactionUtilTest {
 
     @Test
     public void readAllHavingOnlyNonConfigTest() {
-        doReturn(Futures.immediateCheckedFuture(Optional.of(DATA.data2))).when(read)
+        doReturn(immediateFluentFuture(Optional.of(DATA.data2))).when(read)
                 .read(LogicalDatastoreType.OPERATIONAL, DATA.path2);
-        doReturn(Futures.immediateCheckedFuture(Optional.absent())).when(read)
+        doReturn(immediateFluentFuture(Optional.empty())).when(read)
                 .read(LogicalDatastoreType.CONFIGURATION, DATA.path2);
         doReturn(DATA.path2).when(context).getInstanceIdentifier();
         final String valueOfContent = RestconfDataServiceConstant.ReadData.ALL;
@@ -128,7 +130,7 @@ public class ReadDataTransactionUtilTest {
 
     @Test
     public void readDataNonConfigTest() {
-        doReturn(Futures.immediateCheckedFuture(Optional.of(DATA.data2))).when(read)
+        doReturn(immediateFluentFuture(Optional.of(DATA.data2))).when(read)
                 .read(LogicalDatastoreType.OPERATIONAL, DATA.path2);
         doReturn(DATA.path2).when(context).getInstanceIdentifier();
         final String valueOfContent = RestconfDataServiceConstant.ReadData.NONCONFIG;
@@ -139,9 +141,9 @@ public class ReadDataTransactionUtilTest {
 
     @Test
     public void readContainerDataAllTest() {
-        doReturn(Futures.immediateCheckedFuture(Optional.of(DATA.data3))).when(read)
+        doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(read)
                 .read(LogicalDatastoreType.CONFIGURATION, DATA.path);
-        doReturn(Futures.immediateCheckedFuture(Optional.of(DATA.data4))).when(read)
+        doReturn(immediateFluentFuture(Optional.of(DATA.data4))).when(read)
                 .read(LogicalDatastoreType.OPERATIONAL, DATA.path);
         doReturn(DATA.path).when(context).getInstanceIdentifier();
         final String valueOfContent = RestconfDataServiceConstant.ReadData.ALL;
@@ -158,9 +160,9 @@ public class ReadDataTransactionUtilTest {
 
     @Test
     public void readContainerDataConfigNoValueOfContentTest() {
-        doReturn(Futures.immediateCheckedFuture(Optional.of(DATA.data3))).when(read)
+        doReturn(immediateFluentFuture(Optional.of(DATA.data3))).when(read)
                 .read(LogicalDatastoreType.CONFIGURATION, DATA.path);
-        doReturn(Futures.immediateCheckedFuture(Optional.of(DATA.data4))).when(read)
+        doReturn(immediateFluentFuture(Optional.of(DATA.data4))).when(read)
                 .read(LogicalDatastoreType.OPERATIONAL, DATA.path);
         doReturn(DATA.path).when(context).getInstanceIdentifier();
         final NormalizedNode<?, ?> normalizedNode = ReadDataTransactionUtil.readData(
@@ -176,9 +178,9 @@ public class ReadDataTransactionUtilTest {
 
     @Test
     public void readListDataAllTest() {
-        doReturn(Futures.immediateCheckedFuture(Optional.of(DATA.listData))).when(read)
+        doReturn(immediateFluentFuture(Optional.of(DATA.listData))).when(read)
                 .read(LogicalDatastoreType.OPERATIONAL, DATA.path3);
-        doReturn(Futures.immediateCheckedFuture(Optional.of(DATA.listData2))).when(read)
+        doReturn(immediateFluentFuture(Optional.of(DATA.listData2))).when(read)
                 .read(LogicalDatastoreType.CONFIGURATION, DATA.path3);
         doReturn(DATA.path3).when(context).getInstanceIdentifier();
         final String valueOfContent = RestconfDataServiceConstant.ReadData.ALL;
@@ -194,9 +196,9 @@ public class ReadDataTransactionUtilTest {
 
     @Test
     public void readOrderedListDataAllTest() {
-        doReturn(Futures.immediateCheckedFuture(Optional.of(DATA.orderedMapNode1))).when(read)
+        doReturn(immediateFluentFuture(Optional.of(DATA.orderedMapNode1))).when(read)
                 .read(LogicalDatastoreType.OPERATIONAL, DATA.path3);
-        doReturn(Futures.immediateCheckedFuture(Optional.of(DATA.orderedMapNode2))).when(read)
+        doReturn(immediateFluentFuture(Optional.of(DATA.orderedMapNode2))).when(read)
                 .read(LogicalDatastoreType.CONFIGURATION, DATA.path3);
         doReturn(DATA.path3).when(context).getInstanceIdentifier();
 
@@ -211,9 +213,9 @@ public class ReadDataTransactionUtilTest {
 
     @Test
     public void readUnkeyedListDataAllTest() {
-        doReturn(Futures.immediateCheckedFuture(Optional.of(DATA.unkeyedListNode1))).when(read)
+        doReturn(immediateFluentFuture(Optional.of(DATA.unkeyedListNode1))).when(read)
                 .read(LogicalDatastoreType.OPERATIONAL, DATA.path3);
-        doReturn(Futures.immediateCheckedFuture(Optional.of(DATA.unkeyedListNode2))).when(read)
+        doReturn(immediateFluentFuture(Optional.of(DATA.unkeyedListNode2))).when(read)
                 .read(LogicalDatastoreType.CONFIGURATION, DATA.path3);
         doReturn(DATA.path3).when(context).getInstanceIdentifier();
 
@@ -231,9 +233,9 @@ public class ReadDataTransactionUtilTest {
 
     @Test
     public void readLeafListDataAllTest() {
-        doReturn(Futures.immediateCheckedFuture(Optional.of(DATA.leafSetNode1))).when(read)
+        doReturn(immediateFluentFuture(Optional.of(DATA.leafSetNode1))).when(read)
                 .read(LogicalDatastoreType.OPERATIONAL, DATA.leafSetNodePath);
-        doReturn(Futures.immediateCheckedFuture(Optional.of(DATA.leafSetNode2))).when(read)
+        doReturn(immediateFluentFuture(Optional.of(DATA.leafSetNode2))).when(read)
                 .read(LogicalDatastoreType.CONFIGURATION, DATA.leafSetNodePath);
         doReturn(DATA.leafSetNodePath).when(context).getInstanceIdentifier();
 
@@ -249,9 +251,9 @@ public class ReadDataTransactionUtilTest {
 
     @Test
     public void readOrderedLeafListDataAllTest() {
-        doReturn(Futures.immediateCheckedFuture(Optional.of(DATA.orderedLeafSetNode1))).when(read)
+        doReturn(immediateFluentFuture(Optional.of(DATA.orderedLeafSetNode1))).when(read)
                 .read(LogicalDatastoreType.OPERATIONAL, DATA.leafSetNodePath);
-        doReturn(Futures.immediateCheckedFuture(Optional.of(DATA.orderedLeafSetNode2))).when(read)
+        doReturn(immediateFluentFuture(Optional.of(DATA.orderedLeafSetNode2))).when(read)
                 .read(LogicalDatastoreType.CONFIGURATION, DATA.leafSetNodePath);
         doReturn(DATA.leafSetNodePath).when(context).getInstanceIdentifier();
 
@@ -267,7 +269,7 @@ public class ReadDataTransactionUtilTest {
 
     @Test
     public void readDataWrongPathOrNoContentTest() {
-        doReturn(Futures.immediateCheckedFuture(Optional.absent())).when(read)
+        doReturn(immediateFluentFuture(Optional.empty())).when(read)
                 .read(LogicalDatastoreType.CONFIGURATION, DATA.path2);
         doReturn(DATA.path2).when(context).getInstanceIdentifier();
         final String valueOfContent = RestconfDataServiceConstant.ReadData.CONFIG;
@@ -438,4 +440,59 @@ public class ReadDataTransactionUtilTest {
             assertEquals("Error status code is not correct", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
         }
     }
+
+    /**
+     * Testing parsing of with-defaults parameter which value doesn't match report-all or report-all-tagged patterns
+     * - non-reporting setting.
+     */
+    @Test
+    public void parseUriParametersWithDefaultAndNonTaggedTest() {
+        // preparation of input data
+        final UriInfo uriInfo = Mockito.mock(UriInfo.class);
+        final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
+        final String preparedDefaultValue = "sample-default";
+        parameters.put(RestconfDataServiceConstant.ReadData.WITH_DEFAULTS,
+                Collections.singletonList(preparedDefaultValue));
+        when(uriInfo.getQueryParameters()).thenReturn(parameters);
+
+        final WriterParameters writerParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
+        assertEquals(preparedDefaultValue, writerParameters.getWithDefault());
+        assertFalse(writerParameters.isTagged());
+    }
+
+    /**
+     * Testing parsing of with-defaults parameter which value matches 'report-all-tagged' setting - default value should
+     * be set to {@code null} and tagged flag should be set to {@code true}.
+     */
+    @Test
+    public void parseUriParametersWithDefaultAndTaggedTest() {
+        // preparation of input data
+        final UriInfo uriInfo = Mockito.mock(UriInfo.class);
+        final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
+        parameters.put(RestconfDataServiceConstant.ReadData.WITH_DEFAULTS,
+                Collections.singletonList(ReadData.REPORT_ALL_TAGGED_DEFAULT_VALUE));
+        when(uriInfo.getQueryParameters()).thenReturn(parameters);
+
+        final WriterParameters writerParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
+        assertNull(writerParameters.getWithDefault());
+        assertTrue(writerParameters.isTagged());
+    }
+
+    /**
+     * Testing parsing of with-defaults parameter which value matches 'report-all' setting - default value should
+     * be set to {@code null} and tagged flag should be set to {@code false}.
+     */
+    @Test
+    public void parseUriParametersWithDefaultAndReportAllTest() {
+        // preparation of input data
+        final UriInfo uriInfo = Mockito.mock(UriInfo.class);
+        final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
+        parameters.put(RestconfDataServiceConstant.ReadData.WITH_DEFAULTS,
+                Collections.singletonList(ReadData.REPORT_ALL_DEFAULT_VALUE));
+        when(uriInfo.getQueryParameters()).thenReturn(parameters);
+
+        final WriterParameters writerParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
+        assertNull(writerParameters.getWithDefault());
+        assertFalse(writerParameters.isTagged());
+    }
 }