*/
package org.opendaylight.restconf.nb.rfc8040.rests.services.impl;
+import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;
import javax.ws.rs.Path;
import org.opendaylight.mdsal.dom.api.DOMMountPointService;
+import org.opendaylight.mdsal.dom.api.DOMSchemaService;
import org.opendaylight.mdsal.dom.api.DOMYangTextSourceProvider;
import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfSchemaService;
*/
@Path("/")
public class RestconfSchemaServiceImpl implements RestconfSchemaService {
- private final SchemaContextHandler schemaContextHandler;
+ private final DOMSchemaService schemaService;
private final DOMMountPointService mountPointService;
private final DOMYangTextSourceProvider sourceProvider;
/**
* Set {@link SchemaContextHandler} for getting actual {@link SchemaContext}.
*
- * @param schemaContextHandler handling schema context
- * @param mountPointService dom mount point service
+ * @param schemaService a {@link DOMSchemaService}
+ * @param mountPointService a {@link DOMMountPointService}
*/
- public RestconfSchemaServiceImpl(final SchemaContextHandler schemaContextHandler,
- final DOMMountPointService mountPointService,
- final DOMYangTextSourceProvider sourceProvider) {
- this.schemaContextHandler = requireNonNull(schemaContextHandler);
+ public RestconfSchemaServiceImpl(final DOMSchemaService schemaService,
+ final DOMMountPointService mountPointService) {
+ this.schemaService = requireNonNull(schemaService);
this.mountPointService = requireNonNull(mountPointService);
- this.sourceProvider = requireNonNull(sourceProvider);
+ sourceProvider = schemaService.getExtensions().getInstance(DOMYangTextSourceProvider.class);
+ checkArgument(sourceProvider != null, "No DOMYangTextSourceProvider available in %s", schemaService);
}
@Override
public SchemaExportContext getSchema(final String identifier) {
- return ParserIdentifier.toSchemaExportContextFromIdentifier(schemaContextHandler.get(), identifier,
+ return ParserIdentifier.toSchemaExportContextFromIdentifier(schemaService.getGlobalContext(), identifier,
mountPointService, sourceProvider);
}
}
import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.when;
+import com.google.common.collect.ImmutableClassToInstanceMap;
import java.io.FileNotFoundException;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
-import org.opendaylight.mdsal.dom.api.DOMMountPointService;
import org.opendaylight.mdsal.dom.api.DOMSchemaService;
import org.opendaylight.mdsal.dom.api.DOMYangTextSourceProvider;
import org.opendaylight.mdsal.dom.broker.DOMMountPointServiceImpl;
import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
-import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfSchemaService;
import org.opendaylight.restconf.nb.rfc8040.rests.services.api.SchemaExportContext;
import org.opendaylight.yangtools.yang.common.ErrorTag;
// handlers
@Mock
- private SchemaContextHandler mockContextHandler;
+ private DOMSchemaService mockSchemaService;
@Mock
- private DOMYangTextSourceProvider sourceProvider;
- // mount point service
- private DOMMountPointService mountPointService;
+ private DOMYangTextSourceProvider mockSourceProvider;
@BeforeClass
public static void beforeClass() throws FileNotFoundException {
@Before
public void setup() throws Exception {
- this.mountPointService = new DOMMountPointServiceImpl();
+ final var mountPointService = new DOMMountPointServiceImpl();
// create and register mount points
mountPointService
.createMountPoint(YangInstanceIdentifier.of(QName.create("mount:point:1", "2016-01-01", "cont")))
.createMountPoint(YangInstanceIdentifier.of(QName.create("mount:point:2", "2016-01-01", "cont")))
.register();
- this.schemaService = new RestconfSchemaServiceImpl(this.mockContextHandler, mountPointService, sourceProvider);
+ when(mockSchemaService.getExtensions())
+ .thenReturn(ImmutableClassToInstanceMap.of(DOMYangTextSourceProvider.class, mockSourceProvider));
+ schemaService = new RestconfSchemaServiceImpl(mockSchemaService, mountPointService);
}
/**
@Test
public void getSchemaTest() {
// prepare conditions - return not-mount point schema context
- when(this.mockContextHandler.get()).thenReturn(SCHEMA_CONTEXT);
+ when(mockSchemaService.getGlobalContext()).thenReturn(SCHEMA_CONTEXT);
// make test
final SchemaExportContext exportContext = schemaService.getSchema(TEST_MODULE);
@Test
public void getSchemaForNotExistingModuleTest() {
// prepare conditions - return not-mount point schema context
- when(this.mockContextHandler.get()).thenReturn(SCHEMA_CONTEXT);
+ when(mockSchemaService.getGlobalContext()).thenReturn(SCHEMA_CONTEXT);
// make test
final SchemaExportContext exportContext = schemaService.getSchema(NOT_EXISTING_MODULE);
@Test
public void getSchemaMountPointTest() {
// prepare conditions - return schema context with mount points
- when(this.mockContextHandler.get()).thenReturn(SCHEMA_CONTEXT_WITH_MOUNT_POINTS);
+ when(mockSchemaService.getGlobalContext()).thenReturn(SCHEMA_CONTEXT_WITH_MOUNT_POINTS);
// make test
final SchemaExportContext exportContext =
@Test
public void getSchemaForNotExistingModuleMountPointTest() {
// prepare conditions - return schema context with mount points
- when(this.mockContextHandler.get()).thenReturn(SCHEMA_CONTEXT_WITH_MOUNT_POINTS);
+ when(mockSchemaService.getGlobalContext()).thenReturn(SCHEMA_CONTEXT_WITH_MOUNT_POINTS);
// make test
final SchemaExportContext exportContext = schemaService.getSchema(MOUNT_POINT + NOT_EXISTING_MODULE);
@Test
public void getSchemaWithNullSchemaContextTest() {
// prepare conditions - returned schema context is null
- when(this.mockContextHandler.get()).thenReturn(null);
+ when(mockSchemaService.getGlobalContext()).thenReturn(null);
// make test
assertThrows(NullPointerException.class, () -> schemaService.getSchema(TEST_MODULE));
@Test
public void getSchemaWithNullSchemaContextMountPointTest() {
// prepare conditions - returned schema context for mount points is null
- when(this.mockContextHandler.get()).thenReturn(null);
+ when(mockSchemaService.getGlobalContext()).thenReturn(null);
// make test
assertThrows(NullPointerException.class,
@Test
public void getSchemaNullSchemaContextBehindMountPointTest() {
// prepare conditions - return correct schema context for mount points (this is not null)
- when(this.mockContextHandler.get()).thenReturn(SCHEMA_CONTEXT_WITH_MOUNT_POINTS);
+ when(mockSchemaService.getGlobalContext()).thenReturn(SCHEMA_CONTEXT_WITH_MOUNT_POINTS);
// make test - call service on mount point with null schema context
assertThrows(IllegalStateException.class,
// NULL_MOUNT_POINT contains null schema context
- () -> this.schemaService.getSchema(NULL_MOUNT_POINT + TEST_MODULE_BEHIND_MOUNT_POINT));
+ () -> schemaService.getSchema(NULL_MOUNT_POINT + TEST_MODULE_BEHIND_MOUNT_POINT));
}
/**
@Test
public void getSchemaWithNullIdentifierTest() {
// prepare conditions - return correct schema context
- when(this.mockContextHandler.get()).thenReturn(SCHEMA_CONTEXT);
+ when(mockSchemaService.getGlobalContext()).thenReturn(SCHEMA_CONTEXT);
// make test
- assertThrows(NullPointerException.class, () -> this.schemaService.getSchema(null));
+ assertThrows(NullPointerException.class, () -> schemaService.getSchema(null));
}
/**
@Test
public void getSchemaWithEmptyIdentifierTest() {
// prepare conditions - return correct schema context
- when(this.mockContextHandler.get()).thenReturn(SCHEMA_CONTEXT);
+ when(mockSchemaService.getGlobalContext()).thenReturn(SCHEMA_CONTEXT);
// make test and verify
final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
@Test
public void getSchemaWithEmptyIdentifierMountPointTest() {
// prepare conditions - return correct schema context with mount points
- when(this.mockContextHandler.get()).thenReturn(SCHEMA_CONTEXT_WITH_MOUNT_POINTS);
+ when(mockSchemaService.getGlobalContext()).thenReturn(SCHEMA_CONTEXT_WITH_MOUNT_POINTS);
// make test and verify
final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
@Test
public void getSchemaWithNotParsableIdentifierTest() {
// prepare conditions - return correct schema context without mount points
- when(this.mockContextHandler.get()).thenReturn(SCHEMA_CONTEXT);
+ when(mockSchemaService.getGlobalContext()).thenReturn(SCHEMA_CONTEXT);
// make test and verify
final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
@Test
public void getSchemaWithNotParsableIdentifierMountPointTest() {
// prepare conditions - return correct schema context with mount points
- when(this.mockContextHandler.get()).thenReturn(SCHEMA_CONTEXT_WITH_MOUNT_POINTS);
+ when(mockSchemaService.getGlobalContext()).thenReturn(SCHEMA_CONTEXT_WITH_MOUNT_POINTS);
// make test and verify
final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
@Test
public void getSchemaWrongIdentifierTest() {
// prepare conditions - return correct schema context without mount points
- when(this.mockContextHandler.get()).thenReturn(SCHEMA_CONTEXT);
+ when(mockSchemaService.getGlobalContext()).thenReturn(SCHEMA_CONTEXT);
// make test and verify
final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
@Test
public void getSchemaWrongIdentifierMountPointTest() {
// prepare conditions - return correct schema context with mount points
- when(this.mockContextHandler.get()).thenReturn(SCHEMA_CONTEXT_WITH_MOUNT_POINTS);
+ when(mockSchemaService.getGlobalContext()).thenReturn(SCHEMA_CONTEXT_WITH_MOUNT_POINTS);
// make test and verify
final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
@Test
public void getSchemaWithoutRevisionTest() {
// prepare conditions - return correct schema context without mount points
- when(this.mockContextHandler.get()).thenReturn(SCHEMA_CONTEXT);
+ when(mockSchemaService.getGlobalContext()).thenReturn(SCHEMA_CONTEXT);
// make test and verify
final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
@Test
public void getSchemaWithoutRevisionMountPointTest() {
// prepare conditions - return correct schema context with mount points
- when(this.mockContextHandler.get()).thenReturn(SCHEMA_CONTEXT_WITH_MOUNT_POINTS);
+ when(mockSchemaService.getGlobalContext()).thenReturn(SCHEMA_CONTEXT_WITH_MOUNT_POINTS);
// make test and verify
final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
@Test
public void getSchemaContextWithNotExistingMountPointTest() {
// prepare conditions - return schema context with mount points
- when(this.mockContextHandler.get()).thenReturn(SCHEMA_CONTEXT_WITH_MOUNT_POINTS);
+ when(mockSchemaService.getGlobalContext()).thenReturn(SCHEMA_CONTEXT_WITH_MOUNT_POINTS);
// make test
assertThrows(RestconfDocumentedException.class,