summaryrefslogtreecommitdiffstats
path: root/netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestPutOperationTest.java
blob: 8c04e50f09712ed151d7086712810c533be2e924 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * 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.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.Optional;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
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.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
import org.opendaylight.mdsal.common.api.CommitInfo;
import org.opendaylight.mdsal.common.api.OptimisticLockFailedException;
import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
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.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.PutResult;
import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
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;
import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;

//TODO UNSTABLE TESTS - FIX ME
@Ignore
public class RestPutOperationTest extends JerseyTest {

    private static String xmlData;
    private static String xmlData2;
    private static String xmlData3;

    private static EffectiveModelContext schemaContextYangsIetf;
    private static EffectiveModelContext schemaContextTestModule;

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

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

    private static void loadData() throws IOException {
        final InputStream xmlStream =
                RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
        xmlData = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
        final InputStream xmlStream2 =
                RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data2.xml");
        xmlData2 = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream2));
        final InputStream xmlStream3 =
                RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data7.xml");
        xmlData3 = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream3));
    }

    @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);
        final ControllerContext controllerContext =
                TestRestconfUtils.newControllerContext(schemaContextYangsIetf, 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;
    }

    /**
     * Tests of status codes for "/config/{identifier}".
     */
    @Test
    public void putConfigStatusCodes() throws UnsupportedEncodingException {
        final String uri = "/config/ietf-interfaces:interfaces/interface/eth0";
        mockCommitConfigurationDataPutMethod(true);
        assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));

        mockCommitConfigurationDataPutMethod(false);
        assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));

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

    @Test
    public void putConfigStatusCodesEmptyBody() throws UnsupportedEncodingException {
        final String uri = "/config/ietf-interfaces:interfaces/interface/eth0";
        @SuppressWarnings("unused")
        final Response resp = target(uri).request(MediaType.APPLICATION_JSON).put(
                Entity.entity("", MediaType.APPLICATION_JSON));
        assertEquals(400, put(uri, MediaType.APPLICATION_JSON, ""));
    }

    @Test
    public void testRpcResultCommitedToStatusCodesWithMountPoint() throws UnsupportedEncodingException,
            FileNotFoundException, URISyntaxException {
        final PutResult result = mock(PutResult.class);
        when(brokerFacade.commitMountPointDataPut(any(DOMMountPoint.class), any(YangInstanceIdentifier.class),
            any(NormalizedNode.class), null, null)).thenReturn(result);
        doReturn(CommitInfo.emptyFluentFuture()).when(result).getFutureOfPutData();
        when(result.getStatus()).thenReturn(Status.OK);

        mockMountPoint();

        String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
        assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2));

        uri = "/config/ietf-interfaces:interfaces/yang-ext:mount/test-module:cont";
        assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2));
    }

    @Test
    public void putDataMountPointIntoHighestElement() throws UnsupportedEncodingException, URISyntaxException {
        final PutResult result = mock(PutResult.class);
        doReturn(result).when(brokerFacade).commitMountPointDataPut(any(DOMMountPoint.class),
                any(YangInstanceIdentifier.class), any(NormalizedNode.class), null, null);
        doReturn(CommitInfo.emptyFluentFuture()).when(result).getFutureOfPutData();
        when(result.getStatus()).thenReturn(Status.OK);

        mockMountPoint();

        final String uri = "/config/ietf-interfaces:interfaces/yang-ext:mount";
        assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData3));
    }

    @Test
    public void putWithOptimisticLockFailedException() throws UnsupportedEncodingException {

        final String uri = "/config/ietf-interfaces:interfaces/interface/eth0";

        doThrow(OptimisticLockFailedException.class).when(brokerFacade).commitConfigurationDataPut(
                any(EffectiveModelContext.class), any(YangInstanceIdentifier.class), any(NormalizedNode.class), null,
                null);

        assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));

        doThrow(OptimisticLockFailedException.class).doReturn(mock(PutResult.class)).when(brokerFacade)
                .commitConfigurationDataPut(any(EffectiveModelContext.class), any(YangInstanceIdentifier.class),
                        any(NormalizedNode.class), null, null);

        assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));
    }

    @Test
    public void putWithTransactionCommitFailedException() throws UnsupportedEncodingException {

        final String uri = "/config/ietf-interfaces:interfaces/interface/eth0";

        doThrow(TransactionCommitFailedException.class)
                .when(brokerFacade).commitConfigurationDataPut(
                    any(EffectiveModelContext.class), any(YangInstanceIdentifier.class), any(NormalizedNode.class),
                    null, null);

        assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));
    }

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

    private void mockMountPoint() {
        when(mountInstance.getService(DOMSchemaService.class))
            .thenReturn(Optional.of(FixedDOMSchemaService.of(schemaContextTestModule)));
    }

    private void mockCommitConfigurationDataPutMethod(final boolean noErrors) {
        final PutResult putResMock = mock(PutResult.class);
        if (noErrors) {
            doReturn(putResMock).when(brokerFacade).commitConfigurationDataPut(
                    any(EffectiveModelContext.class), any(YangInstanceIdentifier.class), any(NormalizedNode.class),
                    null, null);
        } else {
            doThrow(RestconfDocumentedException.class).when(brokerFacade).commitConfigurationDataPut(
                    any(EffectiveModelContext.class), any(YangInstanceIdentifier.class), any(NormalizedNode.class),
                    null, null);
        }
    }

}