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