summaryrefslogtreecommitdiffstats
path: root/netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestPostOperationTest.java
blob: 3aa6a9c8cbff7276aae39c69579f091110a37cc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.controller.sal.restconf.impl.test;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.XML;

import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.FluentFuture;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.Optional;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
import org.opendaylight.mdsal.common.api.CommitInfo;
import org.opendaylight.mdsal.dom.api.DOMMountPoint;
import org.opendaylight.mdsal.dom.api.DOMSchemaService;
import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
import org.opendaylight.netconf.sal.rest.api.Draft02;
import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter;
import org.opendaylight.netconf.sal.rest.impl.RestconfDocumentedExceptionMapper;
import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;

public class RestPostOperationTest extends JerseyTest {

    private static String xmlBlockData;
    private static String xmlData3;
    private static String xmlData4;

    private static EffectiveModelContext schemaContextYangsIetf;
    private static EffectiveModelContext schemaContextTestModule;
    private static EffectiveModelContext schemaContext;

    private BrokerFacade brokerFacade;
    private RestconfImpl restconfImpl;
    private ControllerContext controllerContext;
    private DOMMountPoint mountInstance;

    @BeforeClass
    public static void init() throws URISyntaxException, IOException {
        schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
        schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
        schemaContext = TestUtils.loadSchemaContext("/test-config-data/yang1");
        loadData();
    }

    @Override
    protected Application configure() {
        /* enable/disable Jersey logs to console */
        // enable(TestProperties.LOG_TRAFFIC);
        // enable(TestProperties.DUMP_ENTITY);
        // enable(TestProperties.RECORD_LOG_LEVEL);
        // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());

        mountInstance = mock(DOMMountPoint.class);
        controllerContext = TestRestconfUtils.newControllerContext(schemaContext, mountInstance);
        brokerFacade = mock(BrokerFacade.class);
        restconfImpl = RestconfImpl.newInstance(brokerFacade, controllerContext);

        ResourceConfig resourceConfig = new ResourceConfig();
        resourceConfig = resourceConfig.registerInstances(restconfImpl,
                new XmlNormalizedNodeBodyReader(controllerContext), new NormalizedNodeXmlBodyWriter(),
                new JsonNormalizedNodeBodyReader(controllerContext), new NormalizedNodeJsonBodyWriter(),
                new RestconfDocumentedExceptionMapper(controllerContext));
        return resourceConfig;
    }

    private void setSchemaControllerContext(final EffectiveModelContext schema) {
        controllerContext.setSchemas(schema);
    }

    @SuppressWarnings("unchecked")
    @Test
    @Ignore /// xmlData* need netconf-yang
    public void postDataViaUrlMountPoint() throws UnsupportedEncodingException {
        setSchemaControllerContext(schemaContextYangsIetf);
        when(brokerFacade.commitConfigurationDataPost(any(DOMMountPoint.class), any(YangInstanceIdentifier.class),
                any(NormalizedNode.class), null, null)).thenReturn(mock(FluentFuture.class));

        when(mountInstance.getService(DOMSchemaService.class))
            .thenReturn(Optional.of(FixedDOMSchemaService.of(schemaContextTestModule)));

        String uri = "/config/ietf-interfaces:interfaces/interface/0/";
        assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData4));
        uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
        assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData3));

        assertEquals(400, post(uri, MediaType.APPLICATION_JSON, ""));
    }

    @SuppressWarnings("unchecked")
    @Test
    @Ignore //jenkins has problem with JerseyTest
    // - we expecting problems with singletons ControllerContext as schemaContext holder
    public void createConfigurationDataTest() throws UnsupportedEncodingException, ParseException {
        when(brokerFacade.commitConfigurationDataPost((EffectiveModelContext) null, any(YangInstanceIdentifier.class),
                any(NormalizedNode.class), null, null))
                .thenReturn(mock(FluentFuture.class));

        final ArgumentCaptor<YangInstanceIdentifier> instanceIdCaptor =
                ArgumentCaptor.forClass(YangInstanceIdentifier.class);
        final ArgumentCaptor<NormalizedNode> compNodeCaptor = ArgumentCaptor.forClass(NormalizedNode.class);


        // FIXME : identify who is set the schemaContext
//        final String URI_1 = "/config";
//        assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
//        verify(brokerFacade).commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
        final String identifier = "[(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)interfaces]";
//        assertEquals(identifier, ImmutableList.copyOf(instanceIdCaptor.getValue().getPathArguments()).toString());

        final String URI_2 = "/config/test-interface:interfaces";
        assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
        // FIXME : NEVER test a nr. of call some service in complex test suite
//        verify(brokerFacade, times(2))
        verify(brokerFacade, times(1))
                .commitConfigurationDataPost((EffectiveModelContext) null, instanceIdCaptor.capture(),
                        compNodeCaptor.capture(), null, null);
//        identifier = "[(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)interfaces," +
//                "(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)block]";
        assertEquals(identifier, ImmutableList.copyOf(instanceIdCaptor.getValue().getPathArguments()).toString());
    }

    @Test
    public void createConfigurationDataNullTest() throws UnsupportedEncodingException {
        doReturn(CommitInfo.emptyFluentFuture()).when(brokerFacade).commitConfigurationDataPost(
            any(EffectiveModelContext.class), any(YangInstanceIdentifier.class), any(NormalizedNode.class), isNull(),
            isNull());

        //FIXME : find who is set schemaContext
//        final String URI_1 = "/config";
//        assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));

        final String URI_2 = "/config/test-interface:interfaces";
        assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
    }

    private int post(final String uri, final String mediaType, final String data) {
        return target(uri).request(mediaType).post(Entity.entity(data, mediaType)).getStatus();
    }

    private static void loadData() throws IOException, URISyntaxException {
        final String xmlPathBlockData =
                RestconfImplTest.class.getResource("/test-config-data/xml/block-data.xml").getPath();
        xmlBlockData = TestUtils.loadTextFile(xmlPathBlockData);
        final String data3Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data3.xml").getPath();
        xmlData3 = TestUtils.loadTextFile(data3Input);
        final String data4Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data7.xml").getPath();
        xmlData4 = TestUtils.loadTextFile(data4Input);
    }

}