Rework body formatting wiring
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / jaxrs / RestconfSchemaServiceTest.java
1 /*
2  * Copyright (c) 2016 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.restconf.nb.jaxrs;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertSame;
12 import static org.mockito.Mockito.doReturn;
13 import static org.opendaylight.restconf.nb.jaxrs.AbstractRestconfTest.assertEntity;
14 import static org.opendaylight.restconf.nb.jaxrs.AbstractRestconfTest.assertError;
15
16 import com.google.common.util.concurrent.Futures;
17 import java.io.IOException;
18 import java.io.Reader;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.Mock;
23 import org.mockito.junit.MockitoJUnitRunner;
24 import org.opendaylight.mdsal.dom.api.DOMActionService;
25 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
26 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
27 import org.opendaylight.mdsal.dom.api.DOMRpcService;
28 import org.opendaylight.mdsal.dom.api.DOMSchemaService.YangTextSourceExtension;
29 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
30 import org.opendaylight.restconf.api.query.PrettyPrintParam;
31 import org.opendaylight.restconf.server.mdsal.MdsalDatabindProvider;
32 import org.opendaylight.restconf.server.mdsal.MdsalRestconfServer;
33 import org.opendaylight.yangtools.yang.common.ErrorTag;
34 import org.opendaylight.yangtools.yang.common.ErrorType;
35 import org.opendaylight.yangtools.yang.common.Revision;
36 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
37 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
38 import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
39 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
40
41 /**
42  * Unit tests for {@code RestconfSchemaService}.
43  */
44 @RunWith(MockitoJUnitRunner.StrictStubs.class)
45 public class RestconfSchemaServiceTest {
46     // schema context with modules
47     private static final EffectiveModelContext MODEL_CONTEXT =
48         YangParserTestUtils.parseYangResourceDirectory("/modules");
49
50     @Mock
51     private YangTextSourceExtension sourceProvider;
52     @Mock
53     private DOMDataBroker dataBroker;
54     @Mock
55     private DOMActionService actionService;
56     @Mock
57     private DOMRpcService rpcService;
58     @Mock
59     private DOMMountPointService mountPointService;
60     @Mock
61     private YangTextSource yangSource;
62     @Mock
63     private Reader yangReader;
64
65     // service under test
66     private JaxRsRestconf restconf;
67
68     @Before
69     public void setup() throws Exception {
70         restconf = new JaxRsRestconf(
71             new MdsalRestconfServer(new MdsalDatabindProvider(
72                 new FixedDOMSchemaService(() -> MODEL_CONTEXT, sourceProvider)), dataBroker, rpcService, actionService,
73                 mountPointService),
74             PrettyPrintParam.FALSE);
75     }
76
77     /**
78      * Get schema with identifier of existing module and check if correct module was found.
79      */
80     @Test
81     public void getSchemaTest() throws Exception {
82         doReturn(Futures.immediateFuture(yangSource)).when(sourceProvider)
83             .getYangTexttSource(new SourceIdentifier("module1", Revision.of("2014-01-01")));
84         doReturn(yangReader).when(yangSource).openStream();
85
86         assertSame(yangReader, assertEntity(Reader.class, 200,
87             ar -> restconf.modulesYangGET("module1", "2014-01-01", ar)));
88     }
89
90     /**
91      * Get schema with identifier of not-existing module. Trying to create <code>SchemaExportContext</code> with
92      * not-existing module should result in error.
93      */
94     @Test
95     public void getSchemaForNotExistingModuleTest() {
96         final var error = assertError(ar -> restconf.modulesYinGET("not-existing", "2016-01-01", ar));
97         assertEquals("Source not-existing@2016-01-01 not found", error.getErrorMessage());
98         assertEquals(ErrorTag.DATA_MISSING, error.getErrorTag());
99         assertEquals(ErrorType.APPLICATION, error.getErrorType());
100     }
101
102     /**
103      * Try to get schema with empty (not valid) identifier catching <code>RestconfDocumentedException</code>. Error
104      * type, error tag and error status code are compared to expected values.
105      */
106     @Test
107     public void getSchemaWithEmptyIdentifierTest() {
108         final var error = assertError(ar -> restconf.modulesYangGET("", null, ar));
109         assertEquals("Identifier must start with character from set 'a-zA-Z_", error.getErrorMessage());
110         assertEquals(ErrorType.PROTOCOL, error.getErrorType());
111         assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
112     }
113
114     /**
115      * Try to get schema with not-parsable identifier catching <code>RestconfDocumentedException</code>. Error type,
116      * error tag and error status code are compared to expected values.
117      */
118     @Test
119     public void getSchemaWithNotParsableIdentifierTest() {
120         final var error = assertError(ar -> restconf.modulesYangGET("01_module", "2016-01-01", ar));
121         assertEquals("Identifier must start with character from set 'a-zA-Z_", error.getErrorMessage());
122         assertEquals(ErrorType.PROTOCOL, error.getErrorType());
123         assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
124     }
125
126     /**
127      * Try to get schema with wrong (not valid) identifier catching <code>RestconfDocumentedException</code>. Error
128      * type, error tag and error status code are compared to expected values.
129      *
130      * <p>
131      * Not valid identifier contains only revision without module name.
132      */
133     @Test
134     public void getSchemaWrongIdentifierTest() {
135         final var error = assertError(ar -> restconf.modulesYangGET("2014-01-01", null, ar));
136         assertEquals("Identifier must start with character from set 'a-zA-Z_", error.getErrorMessage());
137         assertEquals(ErrorType.PROTOCOL, error.getErrorType());
138         assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
139     }
140
141     /**
142      * Try to get schema with identifier which does not contain revision catching and check if the correct module
143      * was found.
144      */
145     @Test
146     public void getSchemaWithoutRevisionTest() throws IOException {
147         doReturn(Futures.immediateFuture(yangSource)).when(sourceProvider)
148             .getYangTexttSource(new SourceIdentifier("module-without-revision", (Revision) null));
149         doReturn(yangReader).when(yangSource).openStream();
150         assertSame(yangReader, assertEntity(Reader.class, 200,
151             ar -> restconf.modulesYangGET("module-without-revision", null, ar)));
152     }
153 }