0f5ef01fc9a85fd2f24774bfeacf9ed23024885c
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / RestPostOperationTest.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.mockito.Matchers.any;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.times;
14 import static org.mockito.Mockito.verify;
15 import static org.mockito.Mockito.when;
16 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.XML;
17
18 import com.google.common.collect.ImmutableList;
19 import com.google.common.util.concurrent.CheckedFuture;
20 import com.google.common.util.concurrent.Futures;
21 import java.io.IOException;
22 import java.io.UnsupportedEncodingException;
23 import java.net.URISyntaxException;
24 import java.text.ParseException;
25 import javax.ws.rs.client.Entity;
26 import javax.ws.rs.core.Application;
27 import javax.ws.rs.core.MediaType;
28 import org.glassfish.jersey.server.ResourceConfig;
29 import org.glassfish.jersey.test.JerseyTest;
30 import org.junit.BeforeClass;
31 import org.junit.Ignore;
32 import org.junit.Test;
33 import org.mockito.ArgumentCaptor;
34 import org.mockito.Mockito;
35 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
36 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
37 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
38 import org.opendaylight.netconf.sal.rest.api.Draft02;
39 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
40 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
41 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter;
42 import org.opendaylight.netconf.sal.rest.impl.RestconfDocumentedExceptionMapper;
43 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
44 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
45 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
46 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
48 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
49 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
50 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
51
52 public class RestPostOperationTest extends JerseyTest {
53
54     private static String xmlBlockData;
55     private static String xmlData3;
56     private static String xmlData4;
57
58     private static SchemaContext schemaContextYangsIetf;
59     private static SchemaContext schemaContextTestModule;
60     private static SchemaContext schemaContext;
61
62     private BrokerFacade brokerFacade;
63     private RestconfImpl restconfImpl;
64     private ControllerContext controllerContext;
65     private DOMMountPoint mountInstance;
66
67     @BeforeClass
68     public static void init() throws URISyntaxException, IOException, ReactorException {
69         schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
70         schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
71         schemaContext = TestUtils.loadSchemaContext("/test-config-data/yang1");
72         loadData();
73     }
74
75     @Override
76     protected Application configure() {
77         /* enable/disable Jersey logs to console */
78         // enable(TestProperties.LOG_TRAFFIC);
79         // enable(TestProperties.DUMP_ENTITY);
80         // enable(TestProperties.RECORD_LOG_LEVEL);
81         // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
82
83         mountInstance = mock(DOMMountPoint.class);
84         controllerContext = TestRestconfUtils.newControllerContext(schemaContext, mountInstance);
85         brokerFacade = mock(BrokerFacade.class);
86         restconfImpl = RestconfImpl.getInstance();
87         restconfImpl.setBroker(brokerFacade);
88         restconfImpl.setControllerContext(controllerContext);
89
90         ResourceConfig resourceConfig = new ResourceConfig();
91         resourceConfig = resourceConfig.registerInstances(restconfImpl,
92                 new XmlNormalizedNodeBodyReader(controllerContext), new NormalizedNodeXmlBodyWriter(),
93                 new JsonNormalizedNodeBodyReader(controllerContext), new NormalizedNodeJsonBodyWriter(),
94                 new RestconfDocumentedExceptionMapper(controllerContext));
95         return resourceConfig;
96     }
97
98     private void setSchemaControllerContext(final SchemaContext schema) {
99         controllerContext.setSchemas(schema);
100     }
101
102     @SuppressWarnings("unchecked")
103     @Test
104     @Ignore /// xmlData* need netconf-yang
105     public void postDataViaUrlMountPoint() throws UnsupportedEncodingException {
106         setSchemaControllerContext(schemaContextYangsIetf);
107         when(brokerFacade.commitConfigurationDataPost(any(DOMMountPoint.class), any(YangInstanceIdentifier.class),
108                 any(NormalizedNode.class), null, null)).thenReturn(mock(CheckedFuture.class));
109
110         when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
111
112         String uri = "/config/ietf-interfaces:interfaces/interface/0/";
113         assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData4));
114         uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
115         assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData3));
116
117         assertEquals(400, post(uri, MediaType.APPLICATION_JSON, ""));
118     }
119
120     @SuppressWarnings("unchecked")
121     @Test
122     @Ignore //jenkins has problem with JerseyTest
123     // - we expecting problems with singletons ControllerContext as schemaContext holder
124     public void createConfigurationDataTest() throws UnsupportedEncodingException, ParseException {
125         when(brokerFacade.commitConfigurationDataPost((SchemaContext) null, any(YangInstanceIdentifier.class),
126                 any(NormalizedNode.class), null, null))
127                 .thenReturn(mock(CheckedFuture.class));
128
129         final ArgumentCaptor<YangInstanceIdentifier> instanceIdCaptor =
130                 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
131         @SuppressWarnings("rawtypes")
132         final ArgumentCaptor<NormalizedNode> compNodeCaptor = ArgumentCaptor.forClass(NormalizedNode.class);
133
134
135         // FIXME : identify who is set the schemaContext
136 //        final String URI_1 = "/config";
137 //        assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
138 //        verify(brokerFacade).commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
139         final String identifier = "[(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)interfaces]";
140 //        assertEquals(identifier, ImmutableList.copyOf(instanceIdCaptor.getValue().getPathArguments()).toString());
141
142         final String URI_2 = "/config/test-interface:interfaces";
143         assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
144         // FIXME : NEVER test a nr. of call some service in complex test suite
145 //        verify(brokerFacade, times(2))
146         verify(brokerFacade, times(1))
147                 .commitConfigurationDataPost((SchemaContext) null, instanceIdCaptor.capture(), compNodeCaptor.capture(),
148                         null, null);
149 //        identifier = "[(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)interfaces," +
150 //                "(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)block]";
151         assertEquals(identifier, ImmutableList.copyOf(instanceIdCaptor.getValue().getPathArguments()).toString());
152     }
153
154     @Test
155     public void createConfigurationDataNullTest() throws UnsupportedEncodingException {
156         when(brokerFacade.commitConfigurationDataPost(any(SchemaContext.class), any(YangInstanceIdentifier.class),
157                 any(NormalizedNode.class), Mockito.anyString(), Mockito.anyString()))
158                 .thenReturn(Futures.<Void, TransactionCommitFailedException>immediateCheckedFuture(null));
159
160         //FIXME : find who is set schemaContext
161 //        final String URI_1 = "/config";
162 //        assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
163
164         final String URI_2 = "/config/test-interface:interfaces";
165         assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
166     }
167
168     private int post(final String uri, final String mediaType, final String data) {
169         return target(uri).request(mediaType).post(Entity.entity(data, mediaType)).getStatus();
170     }
171
172     private static void loadData() throws IOException, URISyntaxException {
173         final String xmlPathBlockData =
174                 RestconfImplTest.class.getResource("/test-config-data/xml/block-data.xml").getPath();
175         xmlBlockData = TestUtils.loadTextFile(xmlPathBlockData);
176         final String data3Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data3.xml").getPath();
177         xmlData3 = TestUtils.loadTextFile(data3Input);
178         final String data4Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data7.xml").getPath();
179         xmlData4 = TestUtils.loadTextFile(data4Input);
180     }
181
182 }