419853552d4f60249d67ddba3b17bdaa4b19f8a7
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / rests / utils / DeleteDataTransactionUtilTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.restconf.nb.rfc8040.rests.utils;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.fail;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.when;
14 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFalseFluentFuture;
15 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateTrueFluentFuture;
16
17 import com.google.common.util.concurrent.Futures;
18 import com.google.common.util.concurrent.SettableFuture;
19 import javax.ws.rs.core.Response;
20 import javax.ws.rs.core.Response.Status;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.Mock;
25 import org.mockito.junit.MockitoJUnitRunner;
26 import org.opendaylight.mdsal.common.api.CommitInfo;
27 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
28 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
29 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
30 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
31 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
32 import org.opendaylight.netconf.api.NetconfDocumentedException;
33 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
34 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
35 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
36 import org.opendaylight.restconf.common.errors.RestconfError;
37 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy;
38 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.NetconfRestconfStrategy;
39 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
40 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
41 import org.opendaylight.yangtools.yang.common.ErrorTag;
42 import org.opendaylight.yangtools.yang.common.ErrorType;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
44
45 @RunWith(MockitoJUnitRunner.StrictStubs.class)
46 public class DeleteDataTransactionUtilTest {
47     @Mock
48     private InstanceIdentifierContext<?> context;
49     @Mock
50     private DOMDataTreeReadWriteTransaction readWrite;
51     @Mock
52     private DOMDataBroker mockDataBroker;
53     @Mock
54     private NetconfDataTreeService netconfService;
55
56     @Before
57     public void init() {
58         doReturn(CommitInfo.emptyFluentFuture()).when(this.readWrite).commit();
59         doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(this.netconfService).commit();
60         doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(this.netconfService).discardChanges();
61         doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(this.netconfService).unlock();
62         doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(this.netconfService).lock();
63         doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(this.netconfService)
64             .delete(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.empty());
65         doReturn(YangInstanceIdentifier.empty()).when(this.context).getInstanceIdentifier();
66         doReturn(readWrite).when(mockDataBroker).newReadWriteTransaction();
67     }
68
69     /**
70      * Test of successful DELETE operation.
71      */
72     @Test
73     public void deleteData() {
74         // assert that data to delete exists
75         when(readWrite.exists(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.empty()))
76             .thenReturn(immediateTrueFluentFuture());
77         // test
78         delete(new MdsalRestconfStrategy(mockDataBroker));
79         delete(new NetconfRestconfStrategy(netconfService));
80     }
81
82     /**
83      * Negative test for DELETE operation when data to delete does not exist. Error DATA_MISSING is expected.
84      */
85     @Test
86     public void deleteDataNegativeTest() {
87         // assert that data to delete does NOT exist
88         when(readWrite.exists(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.empty()))
89             .thenReturn(immediateFalseFluentFuture());
90         final NetconfDocumentedException exception = new NetconfDocumentedException("id",
91             ErrorType.RPC, ErrorTag.DATA_MISSING, ErrorSeverity.ERROR);
92         final SettableFuture<? extends CommitInfo> ret = SettableFuture.create();
93         ret.setException(new TransactionCommitFailedException(
94             String.format("Commit of transaction %s failed", this), exception));
95
96         doReturn(ret).when(this.netconfService).commit();
97
98         // test and assert error
99         deleteFail(new MdsalRestconfStrategy(mockDataBroker));
100         deleteFail(new NetconfRestconfStrategy(netconfService));
101     }
102
103     private void delete(final RestconfStrategy strategy) {
104         final Response response = DeleteDataTransactionUtil.deleteData(strategy, context.getInstanceIdentifier());
105         // assert success
106         assertEquals("Not expected response received", Status.NO_CONTENT.getStatusCode(), response.getStatus());
107     }
108
109     private void deleteFail(final RestconfStrategy strategy) {
110         try {
111             DeleteDataTransactionUtil.deleteData(strategy, context.getInstanceIdentifier());
112             fail("Delete operation should fail due to missing data");
113         } catch (final RestconfDocumentedException e) {
114             assertEquals(ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
115             assertEquals(RestconfError.ErrorTag.DATA_MISSING, e.getErrors().get(0).getErrorTag());
116         }
117     }
118 }