Split Restconf implementations (draft02 and RFC) - move
[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
9 package org.opendaylight.restconf.nb.rfc8040.rests.utils;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.fail;
13 import static org.mockito.Mockito.mock;
14
15 import com.google.common.util.concurrent.Futures;
16 import java.lang.reflect.Field;
17 import javax.ws.rs.core.Response;
18 import javax.ws.rs.core.Response.Status;
19 import org.junit.AfterClass;
20 import org.junit.Before;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.mockito.Mock;
24 import org.mockito.Mockito;
25 import org.mockito.MockitoAnnotations;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
29 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
30 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
31 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
32 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
33 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
34 import org.opendaylight.restconf.nb.rfc8040.RestConnectorProvider;
35 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
36 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.TransactionVarsWrapper;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
38
39 public class DeleteDataTransactionUtilTest {
40     @Mock
41     private DOMTransactionChain transactionChain;
42     @Mock
43     private InstanceIdentifierContext<?> context;
44     @Mock
45     private DOMDataReadWriteTransaction readWrite;
46
47     // Fields used when delete operation fails to reset transaction chain
48     private static Field handler;
49     private static Field broker;
50
51     @BeforeClass
52     public static void setup() throws Exception {
53         DeleteDataTransactionUtilTest.handler = RestConnectorProvider.class.getDeclaredField("transactionChainHandler");
54         DeleteDataTransactionUtilTest.broker = RestConnectorProvider.class.getDeclaredField("dataBroker");
55
56         DeleteDataTransactionUtilTest.handler.setAccessible(true);
57         DeleteDataTransactionUtilTest.handler.set(RestConnectorProvider.class, mock(TransactionChainHandler.class));
58
59         DeleteDataTransactionUtilTest.broker.setAccessible(true);
60         DeleteDataTransactionUtilTest.broker.set(RestConnectorProvider.class, mock(DOMDataBroker.class));
61     }
62
63     @AfterClass
64     public static void clean() throws Exception {
65         DeleteDataTransactionUtilTest.handler.set(RestConnectorProvider.class, null);
66         DeleteDataTransactionUtilTest.handler.setAccessible(false);
67
68         DeleteDataTransactionUtilTest.broker.set(RestConnectorProvider.class, null);
69         DeleteDataTransactionUtilTest.broker.setAccessible(false);
70     }
71
72     @Before
73     public void init() throws Exception {
74         MockitoAnnotations.initMocks(this);
75         Mockito.when(this.transactionChain.newReadWriteTransaction()).thenReturn(this.readWrite);
76         Mockito.when(this.readWrite.submit()).thenReturn(Futures.immediateCheckedFuture(null));
77         Mockito.when(this.context.getInstanceIdentifier()).thenReturn(YangInstanceIdentifier.EMPTY);
78     }
79
80     /**
81      * Test of successful DELETE operation.
82      */
83     @Test
84     public void deleteData() throws Exception {
85         // assert that data to delete exists
86         Mockito.when(this.transactionChain.newReadWriteTransaction().exists(LogicalDatastoreType.CONFIGURATION,
87                 YangInstanceIdentifier.EMPTY))
88                 .thenReturn(Futures.immediateCheckedFuture(Boolean.TRUE));
89
90         // test
91         final Response response = DeleteDataTransactionUtil.deleteData(
92                 new TransactionVarsWrapper(this.context, null, this.transactionChain));
93
94         // assert success
95         assertEquals("Not expected response received", Status.OK.getStatusCode(), response.getStatus());
96     }
97
98     /**
99      * Negative test for DELETE operation when data to delete does not exist. Error 404 is expected.
100      */
101     @Test
102     public void deleteDataNegativeTest() throws Exception {
103         // assert that data to delete does NOT exist
104         Mockito.when(this.transactionChain.newReadWriteTransaction().exists(LogicalDatastoreType.CONFIGURATION,
105                 YangInstanceIdentifier.EMPTY))
106                 .thenReturn(Futures.immediateCheckedFuture(Boolean.FALSE));
107
108         // test and assert error
109         try {
110             DeleteDataTransactionUtil.deleteData(new TransactionVarsWrapper(this.context, null, this.transactionChain));
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             assertEquals(404, e.getErrors().get(0).getErrorTag().getStatusCode());
116         }
117     }
118 }