Remove DocumentedException.ErrorSeverity
[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.DocumentedException;
33 import org.opendaylight.netconf.api.NetconfDocumentedException;
34 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
35 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
36 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
37 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
38 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
39 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy;
40 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.NetconfRestconfStrategy;
41 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
42 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
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             DocumentedException.ErrorType.RPC, DocumentedException.ErrorTag.from("data-missing"),
92             ErrorSeverity.ERROR);
93         final SettableFuture<? extends CommitInfo> ret = SettableFuture.create();
94         ret.setException(new TransactionCommitFailedException(
95             String.format("Commit of transaction %s failed", this), exception));
96
97         doReturn(ret).when(this.netconfService).commit();
98
99         // test and assert error
100         deleteFail(new MdsalRestconfStrategy(mockDataBroker));
101         deleteFail(new NetconfRestconfStrategy(netconfService));
102     }
103
104     private void delete(final RestconfStrategy strategy) {
105         final Response response = DeleteDataTransactionUtil.deleteData(strategy, context.getInstanceIdentifier());
106         // assert success
107         assertEquals("Not expected response received", Status.NO_CONTENT.getStatusCode(), response.getStatus());
108     }
109
110     private void deleteFail(final RestconfStrategy strategy) {
111         try {
112             DeleteDataTransactionUtil.deleteData(strategy, context.getInstanceIdentifier());
113             fail("Delete operation should fail due to missing data");
114         } catch (final RestconfDocumentedException e) {
115             assertEquals(ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
116             assertEquals(ErrorTag.DATA_MISSING, e.getErrors().get(0).getErrorTag());
117         }
118     }
119 }