Factor out MdsalDatabindProvider
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / jaxrs / RestconfSchemaServiceTest.java
index 24ed46264f0e8fce7ff3949b28b01172d54e62ab..5bd3a28de41befa1545ae006a63efad55b555248 100644 (file)
@@ -13,8 +13,8 @@ import static org.mockito.Mockito.doReturn;
 import static org.opendaylight.restconf.nb.jaxrs.AbstractRestconfTest.assertEntity;
 import static org.opendaylight.restconf.nb.jaxrs.AbstractRestconfTest.assertError;
 
-import com.google.common.collect.ImmutableClassToInstanceMap;
 import com.google.common.util.concurrent.Futures;
+import java.io.IOException;
 import java.io.Reader;
 import org.junit.Before;
 import org.junit.Test;
@@ -25,15 +25,16 @@ import org.opendaylight.mdsal.dom.api.DOMActionService;
 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
 import org.opendaylight.mdsal.dom.api.DOMRpcService;
-import org.opendaylight.mdsal.dom.api.DOMSchemaService;
-import org.opendaylight.mdsal.dom.api.DOMYangTextSourceProvider;
+import org.opendaylight.mdsal.dom.api.DOMSchemaService.YangTextSourceExtension;
+import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
+import org.opendaylight.restconf.server.mdsal.MdsalDatabindProvider;
 import org.opendaylight.restconf.server.mdsal.MdsalRestconfServer;
 import org.opendaylight.yangtools.yang.common.ErrorTag;
 import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.opendaylight.yangtools.yang.common.Revision;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
-import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
+import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
 /**
@@ -42,13 +43,11 @@ import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 @RunWith(MockitoJUnitRunner.StrictStubs.class)
 public class RestconfSchemaServiceTest {
     // schema context with modules
-    private static final EffectiveModelContext SCHEMA_CONTEXT =
+    private static final EffectiveModelContext MODEL_CONTEXT =
         YangParserTestUtils.parseYangResourceDirectory("/modules");
 
     @Mock
-    private DOMSchemaService schemaService;
-    @Mock
-    private DOMYangTextSourceProvider sourceProvider;
+    private YangTextSourceExtension sourceProvider;
     @Mock
     private DOMDataBroker dataBroker;
     @Mock
@@ -58,7 +57,7 @@ public class RestconfSchemaServiceTest {
     @Mock
     private DOMMountPointService mountPointService;
     @Mock
-    private YangTextSchemaSource yangSource;
+    private YangTextSource yangSource;
     @Mock
     private Reader yangReader;
 
@@ -67,12 +66,9 @@ public class RestconfSchemaServiceTest {
 
     @Before
     public void setup() throws Exception {
-        doReturn(SCHEMA_CONTEXT).when(schemaService).getGlobalContext();
-        doReturn(ImmutableClassToInstanceMap.of(DOMYangTextSourceProvider.class, sourceProvider)).when(schemaService)
-            .getExtensions();
-
-        restconf = new JaxRsRestconf(new MdsalRestconfServer(schemaService, dataBroker, rpcService, actionService,
-            mountPointService));
+        restconf = new JaxRsRestconf(new MdsalRestconfServer(
+            new MdsalDatabindProvider(new FixedDOMSchemaService(() -> MODEL_CONTEXT, sourceProvider)), dataBroker,
+            rpcService, actionService, mountPointService));
     }
 
     /**
@@ -81,7 +77,7 @@ public class RestconfSchemaServiceTest {
     @Test
     public void getSchemaTest() throws Exception {
         doReturn(Futures.immediateFuture(yangSource)).when(sourceProvider)
-            .getSource(new SourceIdentifier("module1", Revision.of("2014-01-01")));
+            .getYangTexttSource(new SourceIdentifier("module1", Revision.of("2014-01-01")));
         doReturn(yangReader).when(yangSource).openStream();
 
         assertSame(yangReader, assertEntity(Reader.class, 200,
@@ -140,15 +136,15 @@ public class RestconfSchemaServiceTest {
     }
 
     /**
-     * Try to get schema with identifier which does not contain revision catching
-     * <code>RestconfDocumentedException</code>. Error type, error tag and error status code are compared to expected
-     * values.
+     * Try to get schema with identifier which does not contain revision catching and check if the correct module
+     * was found.
      */
     @Test
-    public void getSchemaWithoutRevisionTest() {
-        final var error = assertError(ar -> restconf.modulesYinGET("module", null, ar));
-        assertEquals("Source module not found", error.getErrorMessage());
-        assertEquals(ErrorType.APPLICATION, error.getErrorType());
-        assertEquals(ErrorTag.DATA_MISSING, error.getErrorTag());
+    public void getSchemaWithoutRevisionTest() throws IOException {
+        doReturn(Futures.immediateFuture(yangSource)).when(sourceProvider)
+            .getYangTexttSource(new SourceIdentifier("module-without-revision", (Revision) null));
+        doReturn(yangReader).when(yangSource).openStream();
+        assertSame(yangReader, assertEntity(Reader.class, 200,
+            ar -> restconf.modulesYangGET("module-without-revision", null, ar)));
     }
 }