Remove RestconfError.ErrorTag
[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.nb.rfc8040.rests.transactions.MdsalRestconfStrategy;
37 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.NetconfRestconfStrategy;
38 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
39 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
40 import org.opendaylight.yangtools.yang.common.ErrorTag;
41 import org.opendaylight.yangtools.yang.common.ErrorType;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
43
44 @RunWith(MockitoJUnitRunner.StrictStubs.class)
45 public class DeleteDataTransactionUtilTest {
46     @Mock
47     private InstanceIdentifierContext<?> context;
48     @Mock
49     private DOMDataTreeReadWriteTransaction readWrite;
50     @Mock
51     private DOMDataBroker mockDataBroker;
52     @Mock
53     private NetconfDataTreeService netconfService;
54
55     @Before
56     public void init() {
57         doReturn(CommitInfo.emptyFluentFuture()).when(this.readWrite).commit();
58         doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(this.netconfService).commit();
59         doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(this.netconfService).discardChanges();
60         doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(this.netconfService).unlock();
61         doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(this.netconfService).lock();
62         doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(this.netconfService)
63             .delete(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.empty());
64         doReturn(YangInstanceIdentifier.empty()).when(this.context).getInstanceIdentifier();
65         doReturn(readWrite).when(mockDataBroker).newReadWriteTransaction();
66     }
67
68     /**
69      * Test of successful DELETE operation.
70      */
71     @Test
72     public void deleteData() {
73         // assert that data to delete exists
74         when(readWrite.exists(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.empty()))
75             .thenReturn(immediateTrueFluentFuture());
76         // test
77         delete(new MdsalRestconfStrategy(mockDataBroker));
78         delete(new NetconfRestconfStrategy(netconfService));
79     }
80
81     /**
82      * Negative test for DELETE operation when data to delete does not exist. Error DATA_MISSING is expected.
83      */
84     @Test
85     public void deleteDataNegativeTest() {
86         // assert that data to delete does NOT exist
87         when(readWrite.exists(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.empty()))
88             .thenReturn(immediateFalseFluentFuture());
89         final NetconfDocumentedException exception = new NetconfDocumentedException("id",
90             ErrorType.RPC, ErrorTag.DATA_MISSING, ErrorSeverity.ERROR);
91         final SettableFuture<? extends CommitInfo> ret = SettableFuture.create();
92         ret.setException(new TransactionCommitFailedException(
93             String.format("Commit of transaction %s failed", this), exception));
94
95         doReturn(ret).when(this.netconfService).commit();
96
97         // test and assert error
98         deleteFail(new MdsalRestconfStrategy(mockDataBroker));
99         deleteFail(new NetconfRestconfStrategy(netconfService));
100     }
101
102     private void delete(final RestconfStrategy strategy) {
103         final Response response = DeleteDataTransactionUtil.deleteData(strategy, context.getInstanceIdentifier());
104         // assert success
105         assertEquals("Not expected response received", Status.NO_CONTENT.getStatusCode(), response.getStatus());
106     }
107
108     private void deleteFail(final RestconfStrategy strategy) {
109         try {
110             DeleteDataTransactionUtil.deleteData(strategy, context.getInstanceIdentifier());
111             fail("Delete operation should fail due to missing data");
112         } catch (final RestconfDocumentedException e) {
113             assertEquals(ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
114             assertEquals(ErrorTag.DATA_MISSING, e.getErrors().get(0).getErrorTag());
115         }
116     }
117 }