import static org.mockito.Mockito.verify;
import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFailedFluentFuture;
import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFalseFluentFuture;
-import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
+import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Optional;
import javax.ws.rs.core.Response;
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.yangtools.yang.test.util.YangParserTestUtils;
import org.w3c.dom.DOMException;
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class PostDataTransactionUtilTest {
private static final String PATH_FOR_NEW_SCHEMA_CONTEXT = "/jukebox";
@Before
public void setUp() throws Exception {
- MockitoAnnotations.initMocks(this);
this.schema =
YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles(PATH_FOR_NEW_SCHEMA_CONTEXT));
doReturn(UriBuilder.fromUri("http://localhost:8181/restconf/16/")).when(this.uriInfo).getBaseUriBuilder();
doReturn(this.readWrite).when(this.transactionChain).newReadWriteTransaction();
- doReturn(this.read).when(this.transactionChain).newReadOnlyTransaction();
Mockito.doReturn(transactionChain).when(mockDataBroker).createTransactionChain(Mockito.any());
transactionChainHandler = new TransactionChainHandler(mockDataBroker);
doReturn(immediateFalseFluentFuture()).when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION,
this.iid2);
- doReturn(immediateFluentFuture(Optional.empty())).when(this.netconfService).getConfig(this.iid2);
final NodeIdentifier identifier =
((ContainerNode) ((Collection<?>) payload.getData().getValue()).iterator().next()).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());
+ doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, node.getParent(), payload.getData());
doReturn(CommitInfo.emptyFluentFuture()).when(this.readWrite).commit();
doReturn(CommitInfo.emptyFluentFuture()).when(this.netconfService).commit(Mockito.any());
response = PostDataTransactionUtil.postData(this.uriInfo, payload,
new NetconfRestconfStrategy(netconfService), this.schema, null, null);
assertEquals(201, response.getStatus());
- verify(this.netconfService).getConfig(this.iid2);
verify(this.netconfService).create(LogicalDatastoreType.CONFIGURATION,
payload.getInstanceIdentifierContext().getInstanceIdentifier(), payload.getData(), Optional.empty());
}
final NormalizedNodeContext payload = new NormalizedNodeContext(iidContext, this.buildList);
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);
- doReturn(immediateFluentFuture(Optional.empty())).when(this.netconfService).getConfig(node);
- doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, node, payload.getData());
+ doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, node, entryNode);
doReturn(CommitInfo.emptyFluentFuture()).when(this.readWrite).commit();
doReturn(CommitInfo.emptyFluentFuture()).when(this.netconfService).commit(Mockito.any());
Response response = PostDataTransactionUtil.postData(this.uriInfo, payload,
new MdsalRestconfStrategy(transactionChainHandler), this.schema, null, null);
assertEquals(201, response.getStatus());
- assertThat(URLDecoder.decode(response.getLocation().toString(), "UTF-8"),
+ assertThat(URLDecoder.decode(response.getLocation().toString(), StandardCharsets.UTF_8),
containsString(identifier.getValue(identifier.keySet().iterator().next()).toString()));
verify(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, node);
- verify(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, node, data.getValue().iterator().next());
+ verify(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, node, entryNode);
response = PostDataTransactionUtil.postData(this.uriInfo, payload,
new NetconfRestconfStrategy(netconfService), this.schema, null, null);
assertEquals(201, response.getStatus());
- assertThat(URLDecoder.decode(response.getLocation().toString(), "UTF-8"),
+ assertThat(URLDecoder.decode(response.getLocation().toString(), StandardCharsets.UTF_8),
containsString(identifier.getValue(identifier.keySet().iterator().next()).toString()));
- verify(this.netconfService).getConfig(node);
- verify(this.netconfService).create(LogicalDatastoreType.CONFIGURATION, node, data.getValue().iterator().next(),
+ verify(this.netconfService).create(LogicalDatastoreType.CONFIGURATION, node, entryNode,
Optional.empty());
}
doReturn(immediateFalseFluentFuture()).when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION,
this.iid2);
- doReturn(immediateFluentFuture(Optional.empty())).when(this.netconfService).getConfig(this.iid2);
final NodeIdentifier identifier =
((ContainerNode) ((Collection<?>) payload.getData().getValue()).iterator().next()).getIdentifier();
final YangInstanceIdentifier node =
payload.getInstanceIdentifierContext().getInstanceIdentifier().node(identifier);
- doReturn(immediateFalseFluentFuture()).when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, node);
- doReturn(immediateFluentFuture(Optional.empty())).when(this.netconfService).getConfig(node);
- doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, node, payload.getData());
+ doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, node.getParent(), payload.getData());
final DOMException domException = new DOMException((short) 414, "Post request failed");
doReturn(immediateFailedFluentFuture(domException)).when(this.readWrite).commit();
doReturn(immediateFailedFluentFuture(domException)).when(this.netconfService).commit(Mockito.any());
assertTrue(e.getErrors().get(0).getErrorInfo().contains(domException.getMessage()));
}
- verify(this.netconfService).getConfig(this.iid2);
verify(this.netconfService).create(LogicalDatastoreType.CONFIGURATION,
payload.getInstanceIdentifierContext().getInstanceIdentifier(), payload.getData(), Optional.empty());
}