Move simple QueryParams tests
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / rests / utils / ReadDataTransactionUtilTest.java
index 3761fdaeb0be0c5907905948c6ba562d9fd25da2..4ab11afa72cb7e2494fc3eb665ef2f46f31ea658 100644 (file)
@@ -8,23 +8,12 @@
 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.assertSame;
-import static org.junit.Assert.assertThrows;
-import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
 
 import com.google.common.collect.ImmutableList;
-import java.util.List;
 import java.util.Optional;
-import java.util.Set;
-import javax.ws.rs.core.MultivaluedHashMap;
-import javax.ws.rs.core.UriInfo;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.junit.Before;
@@ -37,17 +26,10 @@ import org.opendaylight.mdsal.dom.api.DOMDataBroker;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
-import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
-import org.opendaylight.restconf.common.errors.RestconfError;
 import org.opendaylight.restconf.nb.rfc8040.ContentParameter;
-import org.opendaylight.restconf.nb.rfc8040.DepthParameter;
-import org.opendaylight.restconf.nb.rfc8040.WithDefaultsParameter;
-import org.opendaylight.restconf.nb.rfc8040.legacy.QueryParameters;
 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy;
 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.NetconfRestconfStrategy;
 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
-import org.opendaylight.yangtools.yang.common.ErrorTag;
-import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
@@ -60,11 +42,9 @@ 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;
 
 @RunWith(MockitoJUnitRunner.StrictStubs.class)
 public class ReadDataTransactionUtilTest {
-
     private static final TestData DATA = new TestData();
     private static final NodeIdentifier NODE_IDENTIFIER =
         new NodeIdentifier(QName.create("ns", "2016-02-28", "container"));
@@ -80,22 +60,12 @@ public class ReadDataTransactionUtilTest {
     @Mock
     private EffectiveModelContext schemaContext;
     @Mock
-    private ContainerSchemaNode containerSchemaNode;
-    @Mock
-    private LeafSchemaNode containerChildNode;
-    private QName containerChildQName;
+    private DOMDataBroker mockDataBroker;
 
     @Before
     public void setUp() {
-        containerChildQName = QName.create("ns", "2016-02-28", "container-child");
-
-        when(context.getSchemaContext()).thenReturn(schemaContext);
-        when(context.getSchemaNode()).thenReturn(containerSchemaNode);
-        when(containerSchemaNode.getQName()).thenReturn(NODE_IDENTIFIER.getNodeType());
-        when(containerChildNode.getQName()).thenReturn(containerChildQName);
-        when(containerSchemaNode.dataChildByName(containerChildQName)).thenReturn(containerChildNode);
-
-        DOMDataBroker mockDataBroker = mock(DOMDataBroker.class);
+        // FIXME: these tests need to be parameterized somehow. The trouble is we need mocking before we invoke
+        //        the strategy. This needs some more thought.
         doReturn(read).when(mockDataBroker).newReadOnlyTransaction();
         mdsalStrategy = new MdsalRestconfStrategy(mockDataBroker);
         netconfStrategy = new NetconfRestconfStrategy(netconfService);
@@ -322,251 +292,6 @@ public class ReadDataTransactionUtilTest {
         assertNull(normalizedNode);
     }
 
-    /**
-     * Test of parsing default parameters from URI request.
-     */
-    @Test
-    public void parseUriParametersDefaultTest() {
-        final UriInfo uriInfo = mock(UriInfo.class);
-        final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
-
-        // no parameters, default values should be used
-        when(uriInfo.getQueryParameters()).thenReturn(parameters);
-
-        final QueryParameters parsedParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
-
-        assertEquals(ContentParameter.ALL, parsedParameters.getContent());
-        assertNull(parsedParameters.getDepth());
-        assertNull(parsedParameters.getFields());
-    }
-
-    /**
-     * Test of parsing user defined parameters from URI request.
-     */
-    @Test
-    public void parseUriParametersUserDefinedTest() {
-        final UriInfo uriInfo = mock(UriInfo.class);
-        final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
-
-        final String fields = containerChildQName.getLocalName();
-
-        parameters.put("content", List.of("config"));
-        parameters.put("depth", List.of("10"));
-        parameters.put("fields", List.of(fields));
-
-        when(uriInfo.getQueryParameters()).thenReturn(parameters);
-
-        final QueryParameters parsedParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
-
-        // content
-        assertEquals(ContentParameter.CONFIG, parsedParameters.getContent());
-
-        // depth
-        final DepthParameter depth = parsedParameters.getDepth();
-        assertNotNull(depth);
-        assertEquals(10, depth.value());
-
-        // fields
-        assertNotNull(parsedParameters.getFields());
-        assertEquals(1, parsedParameters.getFields().size());
-        assertEquals(1, parsedParameters.getFields().get(0).size());
-        assertEquals(containerChildQName, parsedParameters.getFields().get(0).iterator().next());
-    }
-
-    /**
-     * Negative test of parsing request URI parameters when content parameter has not allowed value.
-     */
-    @Test
-    public void parseUriParametersContentParameterNegativeTest() {
-        final UriInfo uriInfo = mock(UriInfo.class);
-        final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
-
-        parameters.put("content", List.of("not-allowed-parameter-value"));
-        when(uriInfo.getQueryParameters()).thenReturn(parameters);
-
-        final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
-            () -> ReadDataTransactionUtil.parseUriParameters(context, uriInfo));
-        // Bad request
-        assertEquals("Error type is not correct", ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
-        assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
-    }
-
-    /**
-     * Negative test of parsing request URI parameters when depth parameter has not allowed value.
-     */
-    @Test
-    public void parseUriParametersDepthParameterNegativeTest() {
-        final UriInfo uriInfo = mock(UriInfo.class);
-        final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
-
-        // inserted value is not allowed
-        parameters.put("depth", List.of("bounded"));
-        when(uriInfo.getQueryParameters()).thenReturn(parameters);
-
-        RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
-            () -> ReadDataTransactionUtil.parseUriParameters(context, uriInfo));
-        // Bad request
-        assertEquals("Error type is not correct", ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
-        assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
-    }
-
-    /**
-     * Negative test of parsing request URI parameters when depth parameter has not allowed value (less than minimum).
-     */
-    @Test
-    public void parseUriParametersDepthMinimalParameterNegativeTest() {
-        final UriInfo uriInfo = mock(UriInfo.class);
-        final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
-
-        // inserted value is too low
-        parameters.put("depth", List.of("0"));
-        when(uriInfo.getQueryParameters()).thenReturn(parameters);
-
-        RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
-            () -> ReadDataTransactionUtil.parseUriParameters(context, uriInfo));
-        // Bad request
-        assertEquals("Error type is not correct", ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
-        assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
-    }
-
-    /**
-     * Negative test of parsing request URI parameters when depth parameter has not allowed value (more than maximum).
-     */
-    @Test
-    public void parseUriParametersDepthMaximalParameterNegativeTest() {
-        final UriInfo uriInfo = mock(UriInfo.class);
-        final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
-
-        // inserted value is too high
-        parameters.put("depth", List.of("65536"));
-        when(uriInfo.getQueryParameters()).thenReturn(parameters);
-
-        RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
-            () -> ReadDataTransactionUtil.parseUriParameters(context, uriInfo));
-        // Bad request
-        assertEquals("Error type is not correct", ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
-        assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
-    }
-
-    /**
-     * 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 = mock(UriInfo.class);
-        final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
-        parameters.put("with-defaults", List.of("explicit"));
-        when(uriInfo.getQueryParameters()).thenReturn(parameters);
-
-        final QueryParameters writerParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
-        assertSame(WithDefaultsParameter.EXPLICIT, writerParameters.getWithDefault());
-        assertFalse(writerParameters.isTagged());
-    }
-
-    /**
-     * Testing parsing of with-defaults parameter which value which is not supported.
-     */
-    @Test
-    public void parseUriParametersWithDefaultInvalidTest() {
-        // preparation of input data
-        final UriInfo uriInfo = mock(UriInfo.class);
-        final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
-        parameters.put("with-defaults", List.of("invalid"));
-        when(uriInfo.getQueryParameters()).thenReturn(parameters);
-
-        final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
-            () -> ReadDataTransactionUtil.parseUriParameters(context, uriInfo));
-        final List<RestconfError> errors = ex.getErrors();
-        assertEquals(1, errors.size());
-        assertEquals(ErrorTag.INVALID_VALUE, errors.get(0).getErrorTag());
-    }
-
-    /**
-     * 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 = mock(UriInfo.class);
-        final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
-        parameters.put("with-defaults", List.of("report-all-tagged"));
-        when(uriInfo.getQueryParameters()).thenReturn(parameters);
-
-        final QueryParameters 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 = mock(UriInfo.class);
-        final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
-        parameters.put("with-defaults", List.of("report-all"));
-        when(uriInfo.getQueryParameters()).thenReturn(parameters);
-
-        final QueryParameters writerParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
-        assertNull(writerParameters.getWithDefault());
-        assertFalse(writerParameters.isTagged());
-    }
-
-    /**
-     * Test when parameter is present at most once.
-     */
-    @Test
-    public void checkParameterCountTest() {
-        ReadDataTransactionUtil.checkParameterCount(List.of("all"), ContentParameter.uriName());
-    }
-
-    /**
-     * Test when parameter is present more than once.
-     */
-    @Test
-    public void checkParameterCountNegativeTest() {
-        final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
-            () -> ReadDataTransactionUtil.checkParameterCount(List.of("config", "nonconfig", "all"),
-                    ContentParameter.uriName()));
-        final List<RestconfError> errors = ex.getErrors();
-        assertEquals(1, errors.size());
-
-        final RestconfError error = errors.get(0);
-        assertEquals("Error type is not correct", ErrorType.PROTOCOL, error.getErrorType());
-        assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, error.getErrorTag());
-    }
-
-
-    /**
-     * Test when all parameters are allowed.
-     */
-    @Test
-    public void checkParametersTypesTest() {
-        ReadDataTransactionUtil.checkParametersTypes(Set.of("content"),
-            Set.of(ContentParameter.uriName(), DepthParameter.uriName()));
-    }
-
-    /**
-     * Test when not allowed parameter type is used.
-     */
-    @Test
-    public void checkParametersTypesNegativeTest() {
-        final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
-            () -> ReadDataTransactionUtil.checkParametersTypes(Set.of("not-allowed-parameter"),
-                Set.of(ContentParameter.uriName(), DepthParameter.uriName())));
-        final List<RestconfError> errors = ex.getErrors();
-        assertEquals(1, errors.size());
-
-        final RestconfError error = errors.get(0);
-        assertEquals("Error type is not correct", ErrorType.PROTOCOL, error.getErrorType());
-        assertEquals("Error tag is not correct", ErrorTag.INVALID_VALUE, error.getErrorTag());
-    }
-
     /**
      * Read specific type of data from data store via transaction.
      *