Replace old yang parser usages in restconf
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / RestDeleteOperationTest.java
1 /*
2  * Copyright (c) 2014 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.controller.sal.restconf.impl.test;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Mockito.doThrow;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
16 import com.google.common.util.concurrent.CheckedFuture;
17 import java.io.FileNotFoundException;
18 import java.io.UnsupportedEncodingException;
19 import java.util.Set;
20 import javax.ws.rs.core.Application;
21 import javax.ws.rs.core.MediaType;
22 import javax.ws.rs.core.Response;
23 import org.glassfish.jersey.server.ResourceConfig;
24 import org.glassfish.jersey.test.JerseyTest;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
28 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
29 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter;
30 import org.opendaylight.netconf.sal.rest.impl.RestconfDocumentedExceptionMapper;
31 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
32 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
33 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
34 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
35 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
37 import org.opendaylight.yangtools.yang.model.api.Module;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
40
41 public class RestDeleteOperationTest extends JerseyTest {
42
43     private static ControllerContext controllerContext;
44     private static BrokerFacade brokerFacade;
45     private static RestconfImpl restconfImpl;
46
47     @BeforeClass
48     public static void init() throws FileNotFoundException, ReactorException {
49         final SchemaContext schemaContext = TestUtils.loadSchemaContext("/test-config-data/yang1");
50         final Set<Module> allModules = schemaContext.getModules();
51         assertNotNull(allModules);
52
53         controllerContext = ControllerContext.getInstance();
54         controllerContext.setSchemas(schemaContext);
55         brokerFacade = mock(BrokerFacade.class);
56         restconfImpl = RestconfImpl.getInstance();
57         restconfImpl.setBroker(brokerFacade);
58         restconfImpl.setControllerContext(controllerContext);
59     }
60
61     @Override
62     protected Application configure() {
63         /* enable/disable Jersey logs to console */
64         // enable(TestProperties.LOG_TRAFFIC);
65         // enable(TestProperties.DUMP_ENTITY);
66         // enable(TestProperties.RECORD_LOG_LEVEL);
67         // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
68         ResourceConfig resourceConfig = new ResourceConfig();
69         resourceConfig = resourceConfig.registerInstances(restconfImpl, new NormalizedNodeJsonBodyWriter(),
70                 new NormalizedNodeXmlBodyWriter(), new XmlNormalizedNodeBodyReader(), new JsonNormalizedNodeBodyReader());
71         resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class);
72         return resourceConfig;
73     }
74
75     @SuppressWarnings("unchecked")
76     @Test
77     public void deleteConfigStatusCodes() throws UnsupportedEncodingException {
78         final String uri = "/config/test-interface:interfaces";
79         when(brokerFacade.commitConfigurationDataDelete(any(YangInstanceIdentifier.class))).thenReturn(
80                 mock(CheckedFuture.class));
81         Response response = target(uri).request(MediaType.APPLICATION_XML).delete();
82         assertEquals(200, response.getStatus());
83
84         doThrow(RestconfDocumentedException.class).when(brokerFacade).commitConfigurationDataDelete(
85                 any(YangInstanceIdentifier.class));
86         response = target(uri).request(MediaType.APPLICATION_XML).delete();
87         assertEquals(500, response.getStatus());
88     }
89 }