import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.opendaylight.restconf.common.patch.PatchEditOperation.CREATE;
import javax.ws.rs.core.UriInfo;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
-import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnitRunner;
import org.opendaylight.mdsal.common.api.CommitInfo;
import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
import org.opendaylight.mdsal.dom.api.DOMDataBroker;
import org.opendaylight.mdsal.dom.api.DOMSchemaService;
import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
import org.opendaylight.mdsal.dom.api.DOMTransactionChainListener;
+import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
import org.opendaylight.restconf.common.context.NormalizedNodeContext;
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.restconf.nb.rfc8040.streams.Configuration;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
import org.opendaylight.yangtools.yang.model.api.SchemaNode;
import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class RestconfDataServiceImplTest {
private static final String PATH_FOR_NEW_SCHEMA_CONTEXT = "/jukebox";
private DOMTransactionChain mountTransactionChain;
@Mock
private RestconfStreamsSubscriptionService delegRestconfSubscrService;
+ @Mock
+ private Configuration configuration;
@Before
public void setUp() throws Exception {
- MockitoAnnotations.initMocks(this);
-
final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
Mockito.when(this.uriInfo.getQueryParameters()).thenReturn(value);
this.contextRef =
YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles(PATH_FOR_NEW_SCHEMA_CONTEXT));
- this.schemaNode = DataSchemaContextTree.from(this.contextRef).getChild(this.iidBase).getDataSchemaNode();
+ this.schemaNode = DataSchemaContextTree.from(this.contextRef).findChild(this.iidBase).orElseThrow(
+ ).getDataSchemaNode();
doReturn(CommitInfo.emptyFluentFuture()).when(this.write).commit();
doReturn(CommitInfo.emptyFluentFuture()).when(this.readWrite).commit();
schemaContextHandler.onModelContextUpdated(this.contextRef);
this.dataService = new RestconfDataServiceImpl(schemaContextHandler, this.transactionChainHandler,
new DOMMountPointServiceHandler(mountPointService), this.delegRestconfSubscrService,
- this.actionServiceHandler);
+ this.actionServiceHandler, configuration);
doReturn(Optional.of(this.mountPoint)).when(this.mountPointService)
.getMountPoint(any(YangInstanceIdentifier.class));
- doCallRealMethod().when(this.mountPoint).getSchemaContext();
- doReturn(this.contextRef).when(this.mountPoint).getEffectiveModelContext();
+ doReturn(Optional.of(FixedDOMSchemaService.of(this.contextRef))).when(this.mountPoint)
+ .getService(DOMSchemaService.class);
doReturn(Optional.of(this.mountDataBroker)).when(this.mountPoint).getService(DOMDataBroker.class);
+ doReturn(Optional.empty()).when(this.mountPoint).getService(NetconfDataTreeService.class);
doReturn(this.mountTransactionChain).when(this.mountDataBroker)
.createTransactionChain(any(DOMTransactionChainListener.class));
doReturn(this.read).when(this.mountTransactionChain).newReadOnlyTransaction();
doReturn(parameters).when(this.uriInfo).getQueryParameters();
doReturn(immediateFluentFuture(Optional.of(this.buildBaseContConfig))).when(this.read)
.read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
- doReturn(immediateFluentFuture(Optional.of(this.buildBaseContOperational))).when(this.read)
- .read(LogicalDatastoreType.OPERATIONAL, this.iidBase);
final Response response = this.dataService.readData("example-jukebox:jukebox", this.uriInfo);
parameters.put("content", Collections.singletonList("nonconfig"));
doReturn(parameters).when(this.uriInfo).getQueryParameters();
- doReturn(immediateFluentFuture(Optional.of(this.buildBaseContConfig))).when(this.read)
- .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
doReturn(immediateFluentFuture(Optional.of(this.buildBaseContOperational))).when(this.read)
.read(LogicalDatastoreType.OPERATIONAL, this.iidBase);
new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, null, this.contextRef);
final NormalizedNodeContext payload = new NormalizedNodeContext(iidContext, this.buildBaseCont);
- doReturn(immediateTrueFluentFuture()).when(this.readWrite)
+ doReturn(immediateTrueFluentFuture()).when(this.read)
.exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, payload.getData());
final Response response = this.dataService.putData(null, payload, this.uriInfo);
new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, mountPoint, this.contextRef);
final NormalizedNodeContext payload = new NormalizedNodeContext(iidContext, this.buildBaseCont);
- doReturn(immediateTrueFluentFuture()).when(this.readWrite)
+ doReturn(immediateTrueFluentFuture()).when(this.read)
.exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, payload.getData());
final Response response = this.dataService.putData(null, payload, this.uriInfo);
final InstanceIdentifierContext<? extends SchemaNode> iidContext =
new InstanceIdentifierContext<>(this.iidBase, null, null, this.contextRef);
final NormalizedNodeContext payload = new NormalizedNodeContext(iidContext, buildList);
- doReturn(immediateFluentFuture(Optional.empty()))
- .when(this.read).read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
final MapNode data = (MapNode) payload.getData();
- final NodeIdentifierWithPredicates identifier =
- data.getValue().iterator().next().getIdentifier();
+ final MapEntryNode entryNode = data.getValue().iterator().next();
+ final NodeIdentifierWithPredicates identifier = entryNode.getIdentifier();
final YangInstanceIdentifier node =
payload.getInstanceIdentifierContext().getInstanceIdentifier().node(identifier);
doReturn(immediateFalseFluentFuture())
- .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, node);
- doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, node, payload.getData());
+ .when(this.read).exists(LogicalDatastoreType.CONFIGURATION, node);
+ doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, node, entryNode);
doReturn(UriBuilder.fromUri("http://localhost:8181/restconf/15/")).when(this.uriInfo).getBaseUriBuilder();
final Response response = this.dataService.postData(null, payload, this.uriInfo);
}
@Test
- public void testPatchData() throws Exception {
+ public void testPatchData() {
final InstanceIdentifierContext<? extends SchemaNode> iidContext =
new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, null, this.contextRef);
final List<PatchEntity> entity = new ArrayList<>();
entity.add(new PatchEntity("delete data", DELETE, iidleaf));
final PatchContext patch = new PatchContext(iidContext, entity, "test patch id");
- doReturn(immediateFluentFuture(Optional.of(this.buildBaseCont))).when(this.read)
- .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
- doNothing().when(this.write).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, this.buildBaseCont);
doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, iidleaf);
doReturn(immediateFalseFluentFuture())
.when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
entity.add(new PatchEntity("delete data", DELETE, iidleaf));
final PatchContext patch = new PatchContext(iidContext, entity, "test patch id");
- doReturn(immediateFluentFuture(Optional.of(this.buildBaseCont))).when(this.read)
- .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
- doNothing().when(this.write).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, this.buildBaseCont);
doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, iidleaf);
doReturn(immediateFalseFluentFuture())
.when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
}
@Test
- public void testPatchDataDeleteNotExist() throws Exception {
+ public void testPatchDataDeleteNotExist() {
final InstanceIdentifierContext<? extends SchemaNode> iidContext =
new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, null, this.contextRef);
final List<PatchEntity> entity = new ArrayList<>();
entity.add(new PatchEntity("delete data", DELETE, iidleaf));
final PatchContext patch = new PatchContext(iidContext, entity, "test patch id");
- doReturn(immediateFluentFuture(Optional.of(this.buildBaseCont))).when(this.read)
- .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
- doNothing().when(this.write).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, this.buildBaseCont);
doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, iidleaf);
doReturn(immediateFalseFluentFuture())
.when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
final InstanceIdentifierContext<? extends SchemaNode> iidContext = new InstanceIdentifierContext<>(
this.iidBase, this.schemaNode, this.mountPoint, this.contextRef);
- RestconfStrategy restconfStrategy = this.dataService.getRestconfStrategy(iidContext, this.mountPoint);
+ RestconfStrategy restconfStrategy = this.dataService.getRestconfStrategy(this.mountPoint);
assertTrue(restconfStrategy instanceof MdsalRestconfStrategy);
doReturn(Optional.of(this.netconfService)).when(this.mountPoint).getService(NetconfDataTreeService.class);
- restconfStrategy = this.dataService.getRestconfStrategy(iidContext, this.mountPoint);
+ restconfStrategy = this.dataService.getRestconfStrategy(this.mountPoint);
assertTrue(restconfStrategy instanceof NetconfRestconfStrategy);
}
}