3aa6a9c8cbff7276aae39c69579f091110a37cc1
[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.ArgumentMatchers.any;
12 import static org.mockito.ArgumentMatchers.isNull;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
18 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.XML;
19
20 import com.google.common.collect.ImmutableList;
21 import com.google.common.util.concurrent.FluentFuture;
22 import java.io.IOException;
23 import java.io.UnsupportedEncodingException;
24 import java.net.URISyntaxException;
25 import java.text.ParseException;
26 import java.util.Optional;
27 import javax.ws.rs.client.Entity;
28 import javax.ws.rs.core.Application;
29 import javax.ws.rs.core.MediaType;
30 import org.glassfish.jersey.server.ResourceConfig;
31 import org.glassfish.jersey.test.JerseyTest;
32 import org.junit.BeforeClass;
33 import org.junit.Ignore;
34 import org.junit.Test;
35 import org.mockito.ArgumentCaptor;
36 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
37 import org.opendaylight.mdsal.common.api.CommitInfo;
38 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
39 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
40 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
41 import org.opendaylight.netconf.sal.rest.api.Draft02;
42 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
43 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
44 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter;
45 import org.opendaylight.netconf.sal.rest.impl.RestconfDocumentedExceptionMapper;
46 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
47 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
48 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
49 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
50 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
51 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
52 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
53
54 public class RestPostOperationTest extends JerseyTest {
55
56     private static String xmlBlockData;
57     private static String xmlData3;
58     private static String xmlData4;
59
60     private static EffectiveModelContext schemaContextYangsIetf;
61     private static EffectiveModelContext schemaContextTestModule;
62     private static EffectiveModelContext schemaContext;
63
64     private BrokerFacade brokerFacade;
65     private RestconfImpl restconfImpl;
66     private ControllerContext controllerContext;
67     private DOMMountPoint mountInstance;
68
69     @BeforeClass
70     public static void init() throws URISyntaxException, IOException {
71         schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
72         schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
73         schemaContext = TestUtils.loadSchemaContext("/test-config-data/yang1");
74         loadData();
75     }
76
77     @Override
78     protected Application configure() {
79         /* enable/disable Jersey logs to console */
80         // enable(TestProperties.LOG_TRAFFIC);
81         // enable(TestProperties.DUMP_ENTITY);
82         // enable(TestProperties.RECORD_LOG_LEVEL);
83         // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
84
85         mountInstance = mock(DOMMountPoint.class);
86         controllerContext = TestRestconfUtils.newControllerContext(schemaContext, mountInstance);
87         brokerFacade = mock(BrokerFacade.class);
88         restconfImpl = RestconfImpl.newInstance(brokerFacade, 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 EffectiveModelContext 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(FluentFuture.class));
109
110         when(mountInstance.getService(DOMSchemaService.class))
111             .thenReturn(Optional.of(FixedDOMSchemaService.of(schemaContextTestModule)));
112
113         String uri = "/config/ietf-interfaces:interfaces/interface/0/";
114         assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData4));
115         uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
116         assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData3));
117
118         assertEquals(400, post(uri, MediaType.APPLICATION_JSON, ""));
119     }
120
121     @SuppressWarnings("unchecked")
122     @Test
123     @Ignore //jenkins has problem with JerseyTest
124     // - we expecting problems with singletons ControllerContext as schemaContext holder
125     public void createConfigurationDataTest() throws UnsupportedEncodingException, ParseException {
126         when(brokerFacade.commitConfigurationDataPost((EffectiveModelContext) null, any(YangInstanceIdentifier.class),
127                 any(NormalizedNode.class), null, null))
128                 .thenReturn(mock(FluentFuture.class));
129
130         final ArgumentCaptor<YangInstanceIdentifier> instanceIdCaptor =
131                 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
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((EffectiveModelContext) null, instanceIdCaptor.capture(),
148                         compNodeCaptor.capture(), 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         doReturn(CommitInfo.emptyFluentFuture()).when(brokerFacade).commitConfigurationDataPost(
157             any(EffectiveModelContext.class), any(YangInstanceIdentifier.class), any(NormalizedNode.class), isNull(),
158             isNull());
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 }