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