* 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;
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.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;
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;
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 {
@Mock
private InstanceIdentifierContext<ContainerSchemaNode> context;
@Mock
- private DOMDataReadOnlyTransaction read;
+ private DOMDataTreeReadTransaction read;
@Mock
- private SchemaContext schemaContext;
+ private EffectiveModelContext schemaContext;
@Mock
private ContainerSchemaNode containerSchemaNode;
@Mock
when(containerChildNode.getQName()).thenReturn(containerChildQName);
when(containerSchemaNode.getDataChildByName(containerChildQName)).thenReturn(containerChildNode);
- wrapper = new TransactionVarsWrapper(this.context, null, this.transactionChain);
+ DOMDataBroker mockDataBroker = Mockito.mock(DOMDataBroker.class);
+ Mockito.doReturn(transactionChain).when(mockDataBroker).createTransactionChain(Mockito.any());
+ wrapper = new TransactionVarsWrapper(this.context, null, new TransactionChainHandler(mockDataBroker));
}
@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;
@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;
@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;
@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;
@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;
@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(
@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;
@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();
@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();
@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();
@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();
@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;
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());
+ }
}