From cb1d2f74b370c16357af975a136e687fba3303e6 Mon Sep 17 00:00:00 2001 From: Tom Pantelis Date: Fri, 13 Apr 2018 19:19:10 -0400 Subject: [PATCH] Use ControllerContext non-statically This is a preliminary patch for removing the web.xml and converting to the new programmtic web API which will allow us to remove the static instances that bridge between the web-instantiated and blueprint-instanriated components. First up is the ControllerContext - this patch keeps the static instance temporarily for the RestconfApplication but converts the rest of the code to inject the ControllerContext instance and reference it non-statically. This patch touches a lot of files but most are UTs. Change-Id: I08e2ee24e518c01bb1d5fdb5796abd9354dccfa2 Signed-off-by: Tom Pantelis --- .../AbstractIdentifierAwareJaxRsProvider.java | 12 +- .../impl/JsonNormalizedNodeBodyReader.java | 8 +- .../sal/rest/impl/JsonToPatchBodyReader.java | 23 ++-- .../sal/rest/impl/RestconfApplication.java | 10 +- .../RestconfDocumentedExceptionMapper.java | 12 +- .../impl/XmlNormalizedNodeBodyReader.java | 5 + .../sal/rest/impl/XmlToPatchBodyReader.java | 5 + .../sal/restconf/impl/BrokerFacade.java | 27 ++--- .../sal/restconf/impl/ControllerContext.java | 72 ++++++++---- .../impl/JSONRestconfServiceImpl.java | 16 ++- .../netconf/sal/restconf/impl/RestCodec.java | 54 +++++---- .../sal/restconf/impl/RestconfImpl.java | 13 +-- .../restconf/impl/RestconfProviderImpl.java | 35 +----- .../streams/listeners/ListenerAdapter.java | 24 ++-- .../NotificationListenerAdapter.java | 7 +- .../sal/streams/listeners/Notificator.java | 10 +- .../blueprint/restconf-config.xml | 15 ++- .../md/sal/rest/common/TestRestconfUtils.java | 35 +++++- .../impl/InstanceIdentifierTypeLeafTest.java | 4 +- .../providers/AbstractBodyReaderTest.java | 30 +++-- .../test/providers/TestJsonBodyReader.java | 6 +- .../TestJsonBodyReaderMountPoint.java | 20 +--- .../test/providers/TestJsonBodyWriter.java | 6 +- .../providers/TestJsonPatchBodyReader.java | 6 +- .../TestJsonPatchBodyReaderMountPoint.java | 19 +--- .../test/providers/TestXmlBodyReader.java | 6 +- .../TestXmlBodyReaderMountPoint.java | 19 +--- .../test/providers/TestXmlBodyWriter.java | 6 +- .../providers/TestXmlPatchBodyReader.java | 6 +- .../TestXmlPatchBodyReaderMountPoint.java | 19 +--- .../to/cnsn/test/RestPutListDataTest.java | 15 ++- .../to/nn/test/JsonIdentityrefToNnTest.java | 10 +- .../json/to/nn/test/JsonLeafrefToNnTest.java | 9 +- .../impl/json/to/nn/test/JsonToNnTest.java | 66 ++++------- .../nn/to/json/test/NnJsonChoiceCaseTest.java | 7 +- .../nn/to/json/test/NnToJsonLeafrefType.java | 9 +- .../to/json/test/NnToJsonWithAugmentTest.java | 10 +- .../test/NnInstanceIdentifierToXmlTest.java | 8 +- .../impl/nn/to/xml/test/NnToXmlTest.java | 4 +- .../nn/to/xml/test/NnToXmlWithChoiceTest.java | 5 +- ...NnToXmlWithDataFromSeveralModulesTest.java | 8 +- .../restconf/impl/test/BrokerFacadeTest.java | 16 ++- .../sal/restconf/impl/test/Bug3595Test.java | 9 +- .../sal/restconf/impl/test/Bug8072Test.java | 45 ++++---- .../test/CodecsExceptionsCatchingTest.java | 19 ++-- .../impl/test/CutDataToCorrectDepthTest.java | 8 +- .../impl/test/ExpressionParserTest.java | 3 +- .../impl/test/InvokeRpcMethodTest.java | 37 +++--- .../test/JSONRestconfServiceImplTest.java | 41 +++---- .../restconf/impl/test/MediaTypesTest.java | 17 ++- .../impl/test/RestCodecExceptionsTest.java | 4 +- .../impl/test/RestDeleteOperationTest.java | 29 ++--- ...GetAugmentedElementWhenEqualNamesTest.java | 11 +- .../impl/test/RestGetOperationTest.java | 107 +++++------------- .../impl/test/RestPostOperationTest.java | 81 ++++--------- .../restconf/impl/test/RestPutConfigTest.java | 15 ++- .../impl/test/RestPutOperationTest.java | 43 ++++--- ...RestconfDocumentedExceptionMapperTest.java | 14 ++- ...stconfImplNotificationSubscribingTest.java | 24 +++- .../restconf/impl/test/RestconfImplTest.java | 15 ++- .../impl/test/URIParametersParsing.java | 7 +- .../sal/restconf/impl/test/URITest.java | 70 +++++------- .../impl/websockets/test/RestStreamTest.java | 24 ++-- .../impl/InstanceIdentifierCodecImplTest.java | 19 +++- .../listeners/ListenerAdapterTest.java | 17 ++- .../listeners/NotificationListenerTest.java | 20 +++- .../test/resources/json-to-nn/empty-data.json | 10 +- .../invalid-uri-character-in-value.json | 10 +- .../json-to-nn/multiple-items-in-list.json | 50 ++++---- .../json-to-nn/multiple-leaflist-items.json | 8 +- .../json-to-nn/simple-container.json | 28 ++--- .../resources/json-to-nn/simple-list.json | 32 +++--- .../json-to-nn/wrong-top-level1.json | 15 ++- .../json-to-nn/wrong-top-level2.json | 14 +-- 74 files changed, 753 insertions(+), 790 deletions(-) diff --git a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/AbstractIdentifierAwareJaxRsProvider.java b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/AbstractIdentifierAwareJaxRsProvider.java index 095e73c952..761b3bdd2e 100644 --- a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/AbstractIdentifierAwareJaxRsProvider.java +++ b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/AbstractIdentifierAwareJaxRsProvider.java @@ -31,12 +31,18 @@ public class AbstractIdentifierAwareJaxRsProvider { @Context private Request request; + private final ControllerContext controllerContext; + + protected AbstractIdentifierAwareJaxRsProvider(ControllerContext controllerContext) { + this.controllerContext = controllerContext; + } + protected final String getIdentifier() { return this.uriInfo.getPathParameters(false).getFirst(RestconfConstants.IDENTIFIER); } protected InstanceIdentifierContext getInstanceIdentifierContext() { - return ControllerContext.getInstance().toInstanceIdentifier(getIdentifier()); + return controllerContext.toInstanceIdentifier(getIdentifier()); } protected UriInfo getUriInfo() { @@ -47,6 +53,10 @@ public class AbstractIdentifierAwareJaxRsProvider { return POST.equals(this.request.getMethod()); } + protected ControllerContext getControllerContext() { + return controllerContext; + } + Request getRequest() { return this.request; } diff --git a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/JsonNormalizedNodeBodyReader.java b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/JsonNormalizedNodeBodyReader.java index f684af7dec..f90997354b 100644 --- a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/JsonNormalizedNodeBodyReader.java +++ b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/JsonNormalizedNodeBodyReader.java @@ -61,6 +61,10 @@ public class JsonNormalizedNodeBodyReader private static final Logger LOG = LoggerFactory.getLogger(JsonNormalizedNodeBodyReader.class); + public JsonNormalizedNodeBodyReader(ControllerContext controllerContext) { + super(controllerContext); + } + @Override public boolean isReadable(final Class type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) { @@ -83,10 +87,10 @@ public class JsonNormalizedNodeBodyReader @SuppressWarnings("checkstyle:IllegalCatch") public static NormalizedNodeContext readFrom(final String uriPath, final InputStream entityStream, - final boolean isPost) throws RestconfDocumentedException { + final boolean isPost, final ControllerContext controllerContext) throws RestconfDocumentedException { try { - return readFrom(ControllerContext.getInstance().toInstanceIdentifier(uriPath), entityStream, isPost); + return readFrom(controllerContext.toInstanceIdentifier(uriPath), entityStream, isPost); } catch (final Exception e) { propagateExceptionAs(e); return null; // no-op diff --git a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/JsonToPatchBodyReader.java b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/JsonToPatchBodyReader.java index 9ed1d045bc..53cc16f8d3 100644 --- a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/JsonToPatchBodyReader.java +++ b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/JsonToPatchBodyReader.java @@ -22,6 +22,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.Optional; +import java.util.concurrent.atomic.AtomicReference; import javax.annotation.Nonnull; import javax.ws.rs.Consumes; import javax.ws.rs.WebApplicationException; @@ -66,7 +67,10 @@ public class JsonToPatchBodyReader extends AbstractIdentifierAwareJaxRsProvider implements MessageBodyReader { private static final Logger LOG = LoggerFactory.getLogger(JsonToPatchBodyReader.class); - private String patchId; + + public JsonToPatchBodyReader(ControllerContext controllerContext) { + super(controllerContext); + } @Override public boolean isReadable(final Class type, final Type genericType, @@ -91,7 +95,7 @@ public class JsonToPatchBodyReader extends AbstractIdentifierAwareJaxRsProvider public PatchContext readFrom(final String uriPath, final InputStream entityStream) throws RestconfDocumentedException { try { - return readFrom(ControllerContext.getInstance().toInstanceIdentifier(uriPath), entityStream); + return readFrom(getControllerContext().toInstanceIdentifier(uriPath), entityStream); } catch (final Exception e) { propagateExceptionAs(e); return null; // no-op @@ -107,10 +111,11 @@ public class JsonToPatchBodyReader extends AbstractIdentifierAwareJaxRsProvider final JsonReader jsonReader = new JsonReader(new InputStreamReader(nonEmptyInputStreamOptional.get(), StandardCharsets.UTF_8)); - final List resultList = read(jsonReader, path); + AtomicReference patchId = new AtomicReference<>(); + final List resultList = read(jsonReader, path, patchId); jsonReader.close(); - return new PatchContext(path, resultList, this.patchId); + return new PatchContext(path, resultList, patchId.get()); } private static RuntimeException propagateExceptionAs(final Exception exception) throws RestconfDocumentedException { @@ -127,7 +132,8 @@ public class JsonToPatchBodyReader extends AbstractIdentifierAwareJaxRsProvider ErrorTag.MALFORMED_MESSAGE, exception); } - private List read(final JsonReader in, final InstanceIdentifierContext path) throws IOException { + private List read(final JsonReader in, final InstanceIdentifierContext path, + final AtomicReference patchId) throws IOException { final List resultCollection = new ArrayList<>(); final StringModuleInstanceIdentifierCodec codec = new StringModuleInstanceIdentifierCodec( path.getSchemaContext()); @@ -154,7 +160,7 @@ public class JsonToPatchBodyReader extends AbstractIdentifierAwareJaxRsProvider case END_DOCUMENT: break; case NAME: - parseByName(in.nextName(), edit, in, path, codec, resultCollection); + parseByName(in.nextName(), edit, in, path, codec, resultCollection, patchId); break; case END_OBJECT: in.endObject(); @@ -185,7 +191,8 @@ public class JsonToPatchBodyReader extends AbstractIdentifierAwareJaxRsProvider private void parseByName(@Nonnull final String name, @Nonnull final PatchEdit edit, @Nonnull final JsonReader in, @Nonnull final InstanceIdentifierContext path, @Nonnull final StringModuleInstanceIdentifierCodec codec, - @Nonnull final List resultCollection) throws IOException { + @Nonnull final List resultCollection, + @Nonnull final AtomicReference patchId) throws IOException { switch (name) { case "edit" : if (in.peek() == JsonToken.BEGIN_ARRAY) { @@ -206,7 +213,7 @@ public class JsonToPatchBodyReader extends AbstractIdentifierAwareJaxRsProvider break; case "patch-id" : - this.patchId = in.nextString(); + patchId.set(in.nextString()); break; default: break; diff --git a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/RestconfApplication.java b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/RestconfApplication.java index 97227bded5..882ac2e916 100644 --- a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/RestconfApplication.java +++ b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/RestconfApplication.java @@ -24,11 +24,6 @@ public class RestconfApplication extends Application { @Override public Set> getClasses() { return ImmutableSet.>builder() - .add(RestconfDocumentedExceptionMapper.class) - .add(XmlNormalizedNodeBodyReader.class) - .add(JsonNormalizedNodeBodyReader.class) - .add(JsonToPatchBodyReader.class) - .add(XmlToPatchBodyReader.class) .add(PatchJsonBodyWriter.class) .add(PatchXmlBodyWriter.class) .add(NormalizedNodeJsonBodyWriter.class) @@ -51,6 +46,11 @@ public class RestconfApplication extends Application { singletons.add(brokerFacade); singletons.add(schemaRetrieval); singletons.add(new RestconfCompositeWrapper(StatisticsRestconfServiceWrapper.getInstance(), schemaRetrieval)); + singletons.add(new RestconfDocumentedExceptionMapper(controllerContext)); + singletons.add(new XmlNormalizedNodeBodyReader(controllerContext)); + singletons.add(new JsonNormalizedNodeBodyReader(controllerContext)); + singletons.add(new XmlToPatchBodyReader(controllerContext)); + singletons.add(new JsonToPatchBodyReader(controllerContext)); // singletons.add(StructuredDataToXmlProvider.INSTANCE); // singletons.add(StructuredDataToJsonProvider.INSTANCE); // singletons.add(JsonToCompositeNodeProvider.INSTANCE); diff --git a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/RestconfDocumentedExceptionMapper.java b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/RestconfDocumentedExceptionMapper.java index 430f366412..c271bf33d2 100644 --- a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/RestconfDocumentedExceptionMapper.java +++ b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/RestconfDocumentedExceptionMapper.java @@ -88,6 +88,12 @@ public class RestconfDocumentedExceptionMapper implements ExceptionMapper(null, - (DataSchemaNode) errorsSchemaNode, null, context.getGlobalSchema()), errContBuild.build()); + (DataSchemaNode) errorsSchemaNode, null, controllerContext.getGlobalSchema()), errContBuild.build()); Object responseBody; if (mediaType.getSubtype().endsWith("json")) { diff --git a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/XmlNormalizedNodeBodyReader.java b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/XmlNormalizedNodeBodyReader.java index 12d933f4b1..510a6cf344 100644 --- a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/XmlNormalizedNodeBodyReader.java +++ b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/XmlNormalizedNodeBodyReader.java @@ -31,6 +31,7 @@ import javax.xml.stream.XMLStreamException; import javax.xml.transform.dom.DOMSource; import org.opendaylight.netconf.sal.rest.api.Draft02; import org.opendaylight.netconf.sal.rest.api.RestconfService; +import org.opendaylight.netconf.sal.restconf.impl.ControllerContext; import org.opendaylight.restconf.common.context.InstanceIdentifierContext; import org.opendaylight.restconf.common.context.NormalizedNodeContext; import org.opendaylight.restconf.common.errors.RestconfDocumentedException; @@ -71,6 +72,10 @@ public class XmlNormalizedNodeBodyReader extends AbstractIdentifierAwareJaxRsPro private static final Logger LOG = LoggerFactory.getLogger(XmlNormalizedNodeBodyReader.class); + public XmlNormalizedNodeBodyReader(ControllerContext controllerContext) { + super(controllerContext); + } + @Override public boolean isReadable(final Class type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) { diff --git a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/XmlToPatchBodyReader.java b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/XmlToPatchBodyReader.java index 965bdf5fb0..071f8bda59 100644 --- a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/XmlToPatchBodyReader.java +++ b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/XmlToPatchBodyReader.java @@ -33,6 +33,7 @@ import javax.xml.stream.XMLStreamException; import javax.xml.transform.dom.DOMSource; import org.opendaylight.netconf.sal.rest.api.Draft02; import org.opendaylight.netconf.sal.rest.api.RestconfService; +import org.opendaylight.netconf.sal.restconf.impl.ControllerContext; import org.opendaylight.restconf.common.context.InstanceIdentifierContext; import org.opendaylight.restconf.common.errors.RestconfDocumentedException; import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag; @@ -79,6 +80,10 @@ public class XmlToPatchBodyReader extends AbstractIdentifierAwareJaxRsProvider i private static final Logger LOG = LoggerFactory.getLogger(XmlToPatchBodyReader.class); + public XmlToPatchBodyReader(ControllerContext controllerContext) { + super(controllerContext); + } + @Override public boolean isReadable(final Class type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) { diff --git a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/BrokerFacade.java b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/BrokerFacade.java index 2aa300f00e..3ab324393d 100644 --- a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/BrokerFacade.java +++ b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/BrokerFacade.java @@ -96,6 +96,7 @@ public class BrokerFacade { private DOMDataBroker domDataBroker; private DOMNotificationService domNotification; + private ControllerContext controllerContext; BrokerFacade() { @@ -109,6 +110,10 @@ public class BrokerFacade { this.domNotification = service; } + public void setControllerContext(ControllerContext controllerContext) { + this.controllerContext = controllerContext; + } + public static BrokerFacade getInstance() { return BrokerFacade.INSTANCE; } @@ -569,7 +574,7 @@ public class BrokerFacade { throw new RestconfDocumentedException("Bad value used with with-defaults parameter : " + withDefa); } - final SchemaContext ctx = ControllerContext.getInstance().getGlobalSchema(); + final SchemaContext ctx = controllerContext.getGlobalSchema(); final DataSchemaContextTree baseSchemaCtxTree = DataSchemaContextTree.from(ctx); final DataSchemaNode baseSchemaNode = baseSchemaCtxTree.getChild(path).getDataSchemaNode(); if (result instanceof ContainerNode) { @@ -791,13 +796,12 @@ public class BrokerFacade { } } - private static void insertWithPointLeafListPost(final DOMDataReadWriteTransaction rwTransaction, + private void insertWithPointLeafListPost(final DOMDataReadWriteTransaction rwTransaction, final LogicalDatastoreType datastore, final YangInstanceIdentifier path, final NormalizedNode payload, final SchemaContext schemaContext, final String point, final OrderedLeafSetNode readLeafList, final boolean before) { rwTransaction.delete(datastore, path.getParent().getParent()); - final InstanceIdentifierContext instanceIdentifier = - ControllerContext.getInstance().toInstanceIdentifier(point); + final InstanceIdentifierContext instanceIdentifier = controllerContext.toInstanceIdentifier(point); int lastItemPosition = 0; for (final LeafSetEntryNode nodeChild : readLeafList.getValue()) { if (nodeChild.getIdentifier().equals(instanceIdentifier.getInstanceIdentifier().getLastPathArgument())) { @@ -824,13 +828,12 @@ public class BrokerFacade { } } - private static void insertWithPointListPost(final DOMDataReadWriteTransaction rwTransaction, + private void insertWithPointListPost(final DOMDataReadWriteTransaction rwTransaction, final LogicalDatastoreType datastore, final YangInstanceIdentifier path, final NormalizedNode payload, final SchemaContext schemaContext, final String point, final MapNode readList, final boolean before) { rwTransaction.delete(datastore, path.getParent().getParent()); - final InstanceIdentifierContext instanceIdentifier = - ControllerContext.getInstance().toInstanceIdentifier(point); + final InstanceIdentifierContext instanceIdentifier = controllerContext.toInstanceIdentifier(point); int lastItemPosition = 0; for (final MapEntryNode mapEntryNode : readList.getValue()) { if (mapEntryNode.getIdentifier() @@ -1102,13 +1105,12 @@ public class BrokerFacade { } } - private static void insertWithPointLeafListPut(final DOMDataWriteTransaction tx, + private void insertWithPointLeafListPut(final DOMDataWriteTransaction tx, final LogicalDatastoreType datastore, final YangInstanceIdentifier path, final NormalizedNode payload, final SchemaContext schemaContext, final String point, final OrderedLeafSetNode readLeafList, final boolean before) { tx.delete(datastore, path.getParent()); - final InstanceIdentifierContext instanceIdentifier = - ControllerContext.getInstance().toInstanceIdentifier(point); + final InstanceIdentifierContext instanceIdentifier = controllerContext.toInstanceIdentifier(point); int index1 = 0; for (final LeafSetEntryNode nodeChild : readLeafList.getValue()) { if (nodeChild.getIdentifier().equals(instanceIdentifier.getInstanceIdentifier().getLastPathArgument())) { @@ -1133,12 +1135,11 @@ public class BrokerFacade { } } - private static void insertWithPointListPut(final DOMDataWriteTransaction tx, final LogicalDatastoreType datastore, + private void insertWithPointListPut(final DOMDataWriteTransaction tx, final LogicalDatastoreType datastore, final YangInstanceIdentifier path, final NormalizedNode payload, final SchemaContext schemaContext, final String point, final OrderedMapNode readList, final boolean before) { tx.delete(datastore, path.getParent()); - final InstanceIdentifierContext instanceIdentifier = - ControllerContext.getInstance().toInstanceIdentifier(point); + final InstanceIdentifierContext instanceIdentifier = controllerContext.toInstanceIdentifier(point); int index1 = 0; for (final MapEntryNode mapEntryNode : readList.getValue()) { if (mapEntryNode.getIdentifier().equals(instanceIdentifier.getInstanceIdentifier().getLastPathArgument())) { diff --git a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/ControllerContext.java b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/ControllerContext.java index 988384c24e..866eb0ccf4 100644 --- a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/ControllerContext.java +++ b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/ControllerContext.java @@ -14,6 +14,7 @@ import com.google.common.base.Strings; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import java.io.Closeable; import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URLDecoder; @@ -36,6 +37,8 @@ import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizat import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizer; import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint; import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService; +import org.opendaylight.controller.sal.core.api.model.SchemaService; +import org.opendaylight.mdsal.dom.api.DOMSchemaService; import org.opendaylight.mdsal.dom.api.DOMYangTextSourceProvider; import org.opendaylight.netconf.sal.rest.api.Draft02; import org.opendaylight.netconf.sal.rest.api.Draft02.RestConfModule; @@ -45,6 +48,7 @@ import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag; import org.opendaylight.restconf.common.errors.RestconfError.ErrorType; import org.opendaylight.restconf.common.util.RestUtil; import org.opendaylight.yangtools.concepts.Codec; +import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.common.Revision; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; @@ -74,7 +78,7 @@ import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class ControllerContext implements SchemaContextListener { +public final class ControllerContext implements SchemaContextListener, Closeable { // FIXME: this should be in md-sal somewhere public static final String MOUNT = "yang-ext:mount"; @@ -95,44 +99,66 @@ public class ControllerContext implements SchemaContextListener { private final AtomicReference> qnameToRpc = new AtomicReference<>(Collections.emptyMap()); - // FIXME; these three should be final + private DOMMountPointService mountService; + private DOMYangTextSourceProvider yangTextSourceProvider; + private ListenerRegistration listenerRegistration; private volatile SchemaContext globalSchema; - private volatile DOMMountPointService mountService; - private volatile DOMYangTextSourceProvider yangTextSourceProvider; - private DataNormalizer dataNormalizer; + private volatile DataNormalizer dataNormalizer; + private ControllerContext(SchemaService schemaService, DOMMountPointService mountService, + DOMYangTextSourceProvider yangTextSourceProvider) { + this.mountService = mountService; + this.yangTextSourceProvider = yangTextSourceProvider; - public void setGlobalSchema(final SchemaContext globalSchema) { - this.globalSchema = globalSchema; - this.dataNormalizer = new DataNormalizer(globalSchema); + setGlobalSchema(schemaService.getGlobalContext()); + listenerRegistration = schemaService.registerSchemaContextListener(this); } - public void setMountService(final DOMMountPointService mountService) { - this.mountService = mountService; + // Temporary until the static instance is removed. + @Deprecated + private ControllerContext() { } - public DOMYangTextSourceProvider getYangTextSourceProvider() { - return yangTextSourceProvider; - } + public static ControllerContext newInstance(SchemaService schemaService, DOMMountPointService mountService, + DOMSchemaService domSchemaService) { + final DOMYangTextSourceProvider yangTextSourceProvider = + (DOMYangTextSourceProvider) domSchemaService.getSupportedExtensions().get(DOMYangTextSourceProvider.class); - public void setYangTextSourceProvider(DOMYangTextSourceProvider yangTextSourceProvider) { - this.yangTextSourceProvider = yangTextSourceProvider; - } + INSTANCE.mountService = mountService; + INSTANCE.yangTextSourceProvider = yangTextSourceProvider; - ControllerContext() { + INSTANCE.onGlobalContextUpdated(schemaService.getGlobalContext()); + INSTANCE.listenerRegistration = schemaService.registerSchemaContextListener(INSTANCE); + return INSTANCE; + //return new ControllerContext(schemaService, mountService, domSchemaServiceExtension); } + @Deprecated public static ControllerContext getInstance() { return INSTANCE; } + private void setGlobalSchema(final SchemaContext globalSchema) { + this.globalSchema = globalSchema; + this.dataNormalizer = new DataNormalizer(globalSchema); + } + + public DOMYangTextSourceProvider getYangTextSourceProvider() { + return yangTextSourceProvider; + } + private void checkPreconditions() { if (this.globalSchema == null) { throw new RestconfDocumentedException(Status.SERVICE_UNAVAILABLE); } } + @Override + public void close() { + listenerRegistration.close(); + } + public void setSchemas(final SchemaContext schemas) { onGlobalContextUpdated(schemas); } @@ -522,9 +548,9 @@ public class ControllerContext implements SchemaContextListener { return ret; } - private static String toUriString(final Object object, final LeafSchemaNode leafNode, final DOMMountPoint mount) + private String toUriString(final Object object, final LeafSchemaNode leafNode, final DOMMountPoint mount) throws UnsupportedEncodingException { - final Codec codec = RestCodec.from(leafNode.getType(), mount); + final Codec codec = RestCodec.from(leafNode.getType(), mount, this); // FIXME: UrlEncoder looks up a well-known charset, we need something that will use it directly return object == null ? "" : URLEncoder.encode(codec.serialize(object).toString(), URI_ENCODING_CHARSET.name()); } @@ -626,11 +652,10 @@ public class ControllerContext implements SchemaContextListener { if (targetNode == null && parentNode instanceof Module) { final RpcDefinition rpc; if (mountPoint == null) { - rpc = ControllerContext.getInstance().getRpcDefinition(head, module.getRevision()); + rpc = getRpcDefinition(head, module.getRevision()); } else { final String rpcName = toNodeName(head); - ControllerContext.getInstance(); - rpc = ControllerContext.getRpcDefinition(module, rpcName); + rpc = getRpcDefinition(module, rpcName); } if (rpc != null) { return new InstanceIdentifierContext<>(builder.build(), rpc, mountPoint, @@ -782,7 +807,7 @@ public class ControllerContext implements SchemaContextListener { if (baseType instanceof LeafrefTypeDefinition) { typedef = SchemaContextUtil.getBaseTypeForLeafRef((LeafrefTypeDefinition) baseType, schemaContext, node); } - final Codec codec = RestCodec.from(typedef, mountPoint); + final Codec codec = RestCodec.from(typedef, mountPoint, this); Object decoded = codec.deserialize(urlDecoded); String additionalInfo = ""; if (decoded == null) { @@ -1000,5 +1025,4 @@ public class ControllerContext implements SchemaContextListener { public DataNormalizationOperation getRootOperation() { return this.dataNormalizer.getRootOperation(); } - } diff --git a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/JSONRestconfServiceImpl.java b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/JSONRestconfServiceImpl.java index c409e78126..47c4f487c9 100644 --- a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/JSONRestconfServiceImpl.java +++ b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/JSONRestconfServiceImpl.java @@ -50,6 +50,12 @@ public class JSONRestconfServiceImpl implements JSONRestconfService, AutoCloseab private static final Annotation[] EMPTY_ANNOTATIONS = new Annotation[0]; + private final ControllerContext controllerContext; + + public JSONRestconfServiceImpl(ControllerContext controllerContext) { + this.controllerContext = controllerContext; + } + @SuppressWarnings("checkstyle:IllegalCatch") @Override public void put(final String uriPath, final String payload) throws OperationFailedException { @@ -58,7 +64,8 @@ public class JSONRestconfServiceImpl implements JSONRestconfService, AutoCloseab LOG.debug("put: uriPath: {}, payload: {}", uriPath, payload); final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8)); - final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, false); + final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, false, + controllerContext); LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier()); LOG.debug("Parsed NormalizedNode: {}", context.getData()); @@ -79,7 +86,8 @@ public class JSONRestconfServiceImpl implements JSONRestconfService, AutoCloseab LOG.debug("post: uriPath: {}, payload: {}", uriPath, payload); final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8)); - final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true); + final NormalizedNodeContext context = JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true, + controllerContext); LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier()); LOG.debug("Parsed NormalizedNode: {}", context.getData()); @@ -150,7 +158,7 @@ public class JSONRestconfServiceImpl implements JSONRestconfService, AutoCloseab if (actualInput != null) { final InputStream entityStream = new ByteArrayInputStream(actualInput.getBytes(StandardCharsets.UTF_8)); final NormalizedNodeContext inputContext = - JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true); + JsonNormalizedNodeBodyReader.readFrom(uriPath, entityStream, true, controllerContext); LOG.debug("Parsed YangInstanceIdentifier: {}", inputContext.getInstanceIdentifierContext() .getInstanceIdentifier()); @@ -183,7 +191,7 @@ public class JSONRestconfServiceImpl implements JSONRestconfService, AutoCloseab final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8)); - JsonToPatchBodyReader jsonToPatchBodyReader = new JsonToPatchBodyReader(); + JsonToPatchBodyReader jsonToPatchBodyReader = new JsonToPatchBodyReader(controllerContext); final PatchContext context = jsonToPatchBodyReader.readFrom(uriPath, entityStream); LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier()); diff --git a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/RestCodec.java b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/RestCodec.java index 4b84cb0dea..513060e7be 100644 --- a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/RestCodec.java +++ b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/RestCodec.java @@ -51,8 +51,8 @@ public final class RestCodec { } public static Codec from(final TypeDefinition typeDefinition, - final DOMMountPoint mountPoint) { - return new ObjectCodec(typeDefinition, mountPoint); + final DOMMountPoint mountPoint, final ControllerContext controllerContext) { + return new ObjectCodec(typeDefinition, mountPoint, controllerContext); } @SuppressWarnings("rawtypes") @@ -61,20 +61,24 @@ public final class RestCodec { private static final Logger LOG = LoggerFactory.getLogger(ObjectCodec.class); public static final Codec LEAFREF_DEFAULT_CODEC = new LeafrefCodecImpl(); + + private final ControllerContext controllerContext; private final Codec instanceIdentifier; private final Codec identityrefCodec; private final TypeDefinition type; - private ObjectCodec(final TypeDefinition typeDefinition, final DOMMountPoint mountPoint) { + private ObjectCodec(final TypeDefinition typeDefinition, final DOMMountPoint mountPoint, + final ControllerContext controllerContext) { + this.controllerContext = controllerContext; this.type = RestUtil.resolveBaseTypeFrom(typeDefinition); if (this.type instanceof IdentityrefTypeDefinition) { - this.identityrefCodec = new IdentityrefCodecImpl(mountPoint); + this.identityrefCodec = new IdentityrefCodecImpl(mountPoint, controllerContext); } else { this.identityrefCodec = null; } if (this.type instanceof InstanceIdentifierTypeDefinition) { - this.instanceIdentifier = new InstanceIdentifierCodecImpl(mountPoint); + this.instanceIdentifier = new InstanceIdentifierCodecImpl(mountPoint, controllerContext); } else { this.instanceIdentifier = null; } @@ -100,7 +104,7 @@ public final class RestCodec { return this.instanceIdentifier.deserialize(input); } else { final StringModuleInstanceIdentifierCodec codec = new StringModuleInstanceIdentifierCodec( - ControllerContext.getInstance().getGlobalSchema()); + controllerContext.getGlobalSchema()); return codec.deserialize((String) input); } } else { @@ -163,9 +167,11 @@ public final class RestCodec { private static final Logger LOG = LoggerFactory.getLogger(IdentityrefCodecImpl.class); private final DOMMountPoint mountPoint; + private final ControllerContext controllerContext; - public IdentityrefCodecImpl(final DOMMountPoint mountPoint) { + public IdentityrefCodecImpl(final DOMMountPoint mountPoint, ControllerContext controllerContext) { this.mountPoint = mountPoint; + this.controllerContext = controllerContext; } @Override @@ -176,7 +182,8 @@ public final class RestCodec { @Override public QName deserialize(final IdentityValuesDTO data) { final IdentityValue valueWithNamespace = data.getValuesWithNamespaces().get(0); - final Module module = getModuleByNamespace(valueWithNamespace.getNamespace(), this.mountPoint); + final Module module = getModuleByNamespace(valueWithNamespace.getNamespace(), this.mountPoint, + controllerContext); if (module == null) { LOG.info("Module was not found for namespace {}", valueWithNamespace.getNamespace()); LOG.info("Idenetityref will be translated as NULL for data - {}", String.valueOf(valueWithNamespace)); @@ -204,10 +211,14 @@ public final class RestCodec { public static class InstanceIdentifierCodecImpl implements InstanceIdentifierCodec { private static final Logger LOG = LoggerFactory.getLogger(InstanceIdentifierCodecImpl.class); + private final DOMMountPoint mountPoint; + private final ControllerContext controllerContext; - public InstanceIdentifierCodecImpl(final DOMMountPoint mountPoint) { + public InstanceIdentifierCodecImpl(final DOMMountPoint mountPoint, + final ControllerContext controllerContext) { this.mountPoint = mountPoint; + this.controllerContext = controllerContext; } @Override @@ -236,7 +247,8 @@ public final class RestCodec { public YangInstanceIdentifier deserialize(final IdentityValuesDTO data) { final List result = new ArrayList<>(); final IdentityValue valueWithNamespace = data.getValuesWithNamespaces().get(0); - final Module module = getModuleByNamespace(valueWithNamespace.getNamespace(), this.mountPoint); + final Module module = getModuleByNamespace(valueWithNamespace.getNamespace(), this.mountPoint, + controllerContext); if (module == null) { LOG.info("Module by namespace '{}' of first node in instance-identifier was not found.", valueWithNamespace.getNamespace()); @@ -249,7 +261,8 @@ public final class RestCodec { final List identities = data.getValuesWithNamespaces(); for (int i = 0; i < identities.size(); i++) { final IdentityValue identityValue = identities.get(i); - URI validNamespace = resolveValidNamespace(identityValue.getNamespace(), this.mountPoint); + URI validNamespace = resolveValidNamespace(identityValue.getNamespace(), this.mountPoint, + controllerContext); final DataSchemaNode node = ControllerContext.findInstanceDataChildByNameAndNamespace( parentContainer, identityValue.getValue(), validNamespace); if (node == null) { @@ -276,7 +289,8 @@ public final class RestCodec { final DataNodeContainer listNode = (DataNodeContainer) node; final Map predicatesMap = new HashMap<>(); for (final Predicate predicate : identityValue.getPredicates()) { - validNamespace = resolveValidNamespace(predicate.getName().getNamespace(), this.mountPoint); + validNamespace = resolveValidNamespace(predicate.getName().getNamespace(), this.mountPoint, + controllerContext); final DataSchemaNode listKey = ControllerContext .findInstanceDataChildByNameAndNamespace(listNode, predicate.getName().getValue(), validNamespace); @@ -325,14 +339,15 @@ public final class RestCodec { } } - private static Module getModuleByNamespace(final String namespace, final DOMMountPoint mountPoint) { - final URI validNamespace = resolveValidNamespace(namespace, mountPoint); + private static Module getModuleByNamespace(final String namespace, final DOMMountPoint mountPoint, + final ControllerContext controllerContext) { + final URI validNamespace = resolveValidNamespace(namespace, mountPoint, controllerContext); Module module = null; if (mountPoint != null) { - module = ControllerContext.getInstance().findModuleByNamespace(mountPoint, validNamespace); + module = controllerContext.findModuleByNamespace(mountPoint, validNamespace); } else { - module = ControllerContext.getInstance().findModuleByNamespace(validNamespace); + module = controllerContext.findModuleByNamespace(validNamespace); } if (module == null) { LOG.info("Module for namespace " + validNamespace + " wasn't found."); @@ -341,12 +356,13 @@ public final class RestCodec { return module; } - private static URI resolveValidNamespace(final String namespace, final DOMMountPoint mountPoint) { + private static URI resolveValidNamespace(final String namespace, final DOMMountPoint mountPoint, + final ControllerContext controllerContext) { URI validNamespace; if (mountPoint != null) { - validNamespace = ControllerContext.getInstance().findNamespaceByModuleName(mountPoint, namespace); + validNamespace = controllerContext.findNamespaceByModuleName(mountPoint, namespace); } else { - validNamespace = ControllerContext.getInstance().findNamespaceByModuleName(namespace); + validNamespace = controllerContext.findNamespaceByModuleName(namespace); } if (validNamespace == null) { validNamespace = URI.create(namespace); diff --git a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/RestconfImpl.java b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/RestconfImpl.java index 95d5995e21..aac9645c5a 100644 --- a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/RestconfImpl.java +++ b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/RestconfImpl.java @@ -653,7 +653,7 @@ public final class RestconfImpl implements RestconfService { .withChild(ImmutableNodes.leafNode(streamNameQname, streamName)).build(); if (!Notificator.existListenerFor(streamName)) { - Notificator.createListener(pathIdentifier, streamName, outputType); + Notificator.createListener(pathIdentifier, streamName, outputType, controllerContext); } final DOMRpcResult defaultDOMRpcResult = new DefaultDOMRpcResult(output); @@ -1158,9 +1158,9 @@ public final class RestconfImpl implements RestconfService { * @return {@link InstanceIdentifierContext} of location leaf for * notification */ - private static InstanceIdentifierContext prepareIIDSubsStreamOutput() { + private InstanceIdentifierContext prepareIIDSubsStreamOutput() { final QName qnameBase = QName.create("subscribe:to:notification", "2016-10-28", "notifi"); - final SchemaContext schemaCtx = ControllerContext.getInstance().getGlobalSchema(); + final SchemaContext schemaCtx = controllerContext.getGlobalSchema(); final DataSchemaNode location = ((ContainerSchemaNode) schemaCtx .findModule(qnameBase.getModule()).orElse(null) .getDataChildByName(qnameBase)).getDataChildByName(QName.create(qnameBase, "location")); @@ -1493,7 +1493,7 @@ public final class RestconfImpl implements RestconfService { * contains list of qnames of notifications * @return - checked future object */ - private static CheckedFuture invokeSalRemoteRpcNotifiStrRPC( + private CheckedFuture invokeSalRemoteRpcNotifiStrRPC( final NormalizedNodeContext payload) { final ContainerNode data = (ContainerNode) payload.getData(); LeafSetNode leafSet = null; @@ -1514,8 +1514,7 @@ public final class RestconfImpl implements RestconfService { final Iterator iterator = entryNodes.iterator(); while (iterator.hasNext()) { final QName valueQName = QName.create((String) iterator.next().getValue()); - final Module module = - ControllerContext.getInstance().findModuleByNamespace(valueQName.getModule().getNamespace()); + final Module module = controllerContext.findModuleByNamespace(valueQName.getModule().getNamespace()); Preconditions.checkNotNull(module, "Module for namespace " + valueQName.getModule().getNamespace() + " does not exist"); NotificationDefinition notifiDef = null; @@ -1546,7 +1545,7 @@ public final class RestconfImpl implements RestconfService { .withChild(ImmutableNodes.leafNode(streamNameQname, streamName)).build(); if (!Notificator.existNotificationListenerFor(streamName)) { - Notificator.createNotificationListener(paths, streamName, outputType); + Notificator.createNotificationListener(paths, streamName, outputType, controllerContext); } final DOMRpcResult defaultDOMRpcResult = new DefaultDOMRpcResult(output); diff --git a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/RestconfProviderImpl.java b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/RestconfProviderImpl.java index 4afc5f8a68..06e3848475 100644 --- a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/RestconfProviderImpl.java +++ b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/RestconfProviderImpl.java @@ -11,12 +11,8 @@ import com.google.common.base.Preconditions; import java.math.BigInteger; import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean; import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker; -import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService; import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService; import org.opendaylight.controller.md.sal.dom.api.DOMRpcService; -import org.opendaylight.controller.sal.core.api.model.SchemaService; -import org.opendaylight.mdsal.dom.api.DOMSchemaService; -import org.opendaylight.mdsal.dom.api.DOMYangTextSourceProvider; import org.opendaylight.netconf.sal.rest.api.RestConnector; import org.opendaylight.netconf.sal.restconf.impl.jmx.Config; import org.opendaylight.netconf.sal.restconf.impl.jmx.Delete; @@ -29,50 +25,35 @@ import org.opendaylight.netconf.sal.restconf.impl.jmx.Rpcs; import org.opendaylight.netconf.sal.streams.websockets.WebSocketServer; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber; -import org.opendaylight.yangtools.concepts.ListenerRegistration; -import org.opendaylight.yangtools.yang.model.api.SchemaContextListener; public class RestconfProviderImpl extends AbstractMXBean implements AutoCloseable, RestConnector, RestConnectorRuntimeMXBean { private final DOMDataBroker domDataBroker; - private final SchemaService schemaService; private final DOMRpcService rpcService; private final DOMNotificationService notificationService; - private final DOMMountPointService mountPointService; private final IpAddress websocketAddress; private final PortNumber websocketPort; private final StatisticsRestconfServiceWrapper stats = StatisticsRestconfServiceWrapper.getInstance(); - private final DOMSchemaService domSchemaService; - private ListenerRegistration listenerRegistration; + private final ControllerContext controllerContext; private Thread webSocketServerThread; - public RestconfProviderImpl(DOMDataBroker domDataBroker, SchemaService schemaService, DOMRpcService rpcService, - DOMNotificationService notificationService, DOMMountPointService mountPointService, - DOMSchemaService domSchemaService, IpAddress websocketAddress, PortNumber websocketPort) { + public RestconfProviderImpl(DOMDataBroker domDataBroker, DOMRpcService rpcService, + DOMNotificationService notificationService, ControllerContext controllerContext, + IpAddress websocketAddress, PortNumber websocketPort) { super("Draft02ProviderStatistics", "restconf-connector", null); this.domDataBroker = Preconditions.checkNotNull(domDataBroker); - this.schemaService = Preconditions.checkNotNull(schemaService); this.rpcService = Preconditions.checkNotNull(rpcService); this.notificationService = Preconditions.checkNotNull(notificationService); - this.mountPointService = Preconditions.checkNotNull(mountPointService); this.websocketAddress = Preconditions.checkNotNull(websocketAddress); this.websocketPort = Preconditions.checkNotNull(websocketPort); - this.domSchemaService = Preconditions.checkNotNull(domSchemaService); + this.controllerContext = Preconditions.checkNotNull(controllerContext); } public void start() { - this.listenerRegistration = schemaService.registerSchemaContextListener(ControllerContext.getInstance()); - BrokerFacade.getInstance().setDomDataBroker(domDataBroker); BrokerFacade.getInstance().setRpcService(rpcService); BrokerFacade.getInstance().setDomNotificationService(notificationService); - - ControllerContext.getInstance().setSchemas(schemaService.getGlobalContext()); - ControllerContext.getInstance().setMountService(mountPointService); - final DOMYangTextSourceProvider domSchemaServiceExtension = - (DOMYangTextSourceProvider) domSchemaService.getSupportedExtensions() - .get(DOMYangTextSourceProvider.class); - ControllerContext.getInstance().setYangTextSourceProvider(domSchemaServiceExtension); + BrokerFacade.getInstance().setControllerContext(controllerContext); this.webSocketServerThread = new Thread(WebSocketServer.createInstance( new String(websocketAddress.getValue()), websocketPort.getValue())); @@ -86,10 +67,6 @@ public class RestconfProviderImpl extends AbstractMXBean public void close() { BrokerFacade.getInstance().setDomDataBroker(null); - if (this.listenerRegistration != null) { - this.listenerRegistration.close(); - } - WebSocketServer.destroyInstance(); if (this.webSocketServerThread != null) { this.webSocketServerThread.interrupt(); diff --git a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/streams/listeners/ListenerAdapter.java b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/streams/listeners/ListenerAdapter.java index f67bd749da..d58ca9a6a7 100644 --- a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/streams/listeners/ListenerAdapter.java +++ b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/streams/listeners/ListenerAdapter.java @@ -48,6 +48,7 @@ public class ListenerAdapter extends AbstractCommonSubscriber implements Cluster private static final Logger LOG = LoggerFactory.getLogger(ListenerAdapter.class); + private final ControllerContext controllerContext; private final YangInstanceIdentifier path; private final String streamName; private final NotificationOutputType outputType; @@ -66,12 +67,13 @@ public class ListenerAdapter extends AbstractCommonSubscriber implements Cluster * Type of output on notification (JSON, XML) */ ListenerAdapter(final YangInstanceIdentifier path, final String streamName, - final NotificationOutputType outputType) { + final NotificationOutputType outputType, final ControllerContext controllerContext) { register(this); this.outputType = Preconditions.checkNotNull(outputType); this.path = Preconditions.checkNotNull(path); Preconditions.checkArgument(streamName != null && !streamName.isEmpty()); this.streamName = streamName; + this.controllerContext = controllerContext; } @Override @@ -132,7 +134,7 @@ public class ListenerAdapter extends AbstractCommonSubscriber implements Cluster * @return Data in printable form. */ private String prepareXml() { - final SchemaContext schemaContext = ControllerContext.getInstance().getGlobalSchema(); + final SchemaContext schemaContext = controllerContext.getGlobalSchema(); final DataSchemaContextTree dataContextTree = DataSchemaContextTree.from(schemaContext); final Document doc = createDocument(); final Element notificationElement = basePartDoc(doc); @@ -202,7 +204,7 @@ public class ListenerAdapter extends AbstractCommonSubscriber implements Cluster YangInstanceIdentifier yiid = YangInstanceIdentifier.builder(parentYiid) .append(normalizedNode.getIdentifier()).build(); - boolean isNodeMixin = ControllerContext.getInstance().isNodeMixin(yiid); + boolean isNodeMixin = controllerContext.isNodeMixin(yiid); boolean isSkippedNonLeaf = getLeafNodesOnly() && !(normalizedNode instanceof LeafNode); if (!isNodeMixin && !isSkippedNonLeaf) { Node node = null; @@ -238,17 +240,17 @@ public class ListenerAdapter extends AbstractCommonSubscriber implements Cluster * * @param doc * {@link Document} - * @param path + * @param dataPath * Path to data in data store. * @param operation * {@link Operation} * @return {@link Node} node represented by changed event element. */ - private static Node createDataChangeEventElement(final Document doc, final YangInstanceIdentifier path, + private Node createDataChangeEventElement(final Document doc, final YangInstanceIdentifier dataPath, final Operation operation) { final Element dataChangeEventElement = doc.createElement("data-change-event"); final Element pathElement = doc.createElement("path"); - addPathAsValueToElement(path, pathElement); + addPathAsValueToElement(dataPath, pathElement); dataChangeEventElement.appendChild(pathElement); final Element operationElement = doc.createElement("operation"); @@ -294,14 +296,14 @@ public class ListenerAdapter extends AbstractCommonSubscriber implements Cluster /** * Adds path as value to element. * - * @param path + * @param dataPath * Path to data in data store. * @param element * {@link Element} */ @SuppressWarnings("rawtypes") - private static void addPathAsValueToElement(final YangInstanceIdentifier path, final Element element) { - final YangInstanceIdentifier normalizedPath = ControllerContext.getInstance().toXpathRepresentation(path); + private void addPathAsValueToElement(final YangInstanceIdentifier dataPath, final Element element) { + final YangInstanceIdentifier normalizedPath = controllerContext.toXpathRepresentation(dataPath); final StringBuilder textContent = new StringBuilder(); for (final PathArgument pathArgument : normalizedPath.getPathArguments()) { @@ -342,9 +344,9 @@ public class ListenerAdapter extends AbstractCommonSubscriber implements Cluster * @param qualifiedName * QName */ - private static void writeIdentifierWithNamespacePrefix(final Element element, final StringBuilder textContent, + private void writeIdentifierWithNamespacePrefix(final Element element, final StringBuilder textContent, final QName qualifiedName) { - final Module module = ControllerContext.getInstance().getGlobalSchema().findModule(qualifiedName.getModule()) + final Module module = controllerContext.getGlobalSchema().findModule(qualifiedName.getModule()) .get(); textContent.append(module.getName()); diff --git a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/streams/listeners/NotificationListenerAdapter.java b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/streams/listeners/NotificationListenerAdapter.java index 9775e344f6..e9471635e4 100644 --- a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/streams/listeners/NotificationListenerAdapter.java +++ b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/streams/listeners/NotificationListenerAdapter.java @@ -48,6 +48,7 @@ public class NotificationListenerAdapter extends AbstractCommonSubscriber implem private static final Logger LOG = LoggerFactory.getLogger(NotificationListenerAdapter.class); + private final ControllerContext controllerContext; private final String streamName; private final SchemaPath path; private final String outputType; @@ -65,12 +66,14 @@ public class NotificationListenerAdapter extends AbstractCommonSubscriber implem * @param outputType * type of output on notification (JSON, XML) */ - NotificationListenerAdapter(final SchemaPath path, final String streamName, final String outputType) { + NotificationListenerAdapter(final SchemaPath path, final String streamName, final String outputType, + final ControllerContext controllerContext) { register(this); this.outputType = Preconditions.checkNotNull(outputType); this.path = Preconditions.checkNotNull(path); Preconditions.checkArgument(streamName != null && !streamName.isEmpty()); this.streamName = streamName; + this.controllerContext = controllerContext; } /** @@ -86,7 +89,7 @@ public class NotificationListenerAdapter extends AbstractCommonSubscriber implem @Override @SuppressWarnings("checkstyle:hiddenField") public void onNotification(final DOMNotification notification) { - this.schemaContext = ControllerContext.getInstance().getGlobalSchema(); + this.schemaContext = controllerContext.getGlobalSchema(); this.notification = notification; final String xml = prepareXml(); diff --git a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/streams/listeners/Notificator.java b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/streams/listeners/Notificator.java index c08a2463c3..241b66d06f 100644 --- a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/streams/listeners/Notificator.java +++ b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/streams/listeners/Notificator.java @@ -14,6 +14,7 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; +import org.opendaylight.netconf.sal.restconf.impl.ControllerContext; import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.model.api.NotificationDefinition; @@ -79,8 +80,8 @@ public final class Notificator { * {@link YangInstanceIdentifier} path and stream name. */ public static ListenerAdapter createListener(final YangInstanceIdentifier path, final String streamName, - final NotificationOutputType outputType) { - final ListenerAdapter listener = new ListenerAdapter(path, streamName, outputType); + final NotificationOutputType outputType, final ControllerContext controllerContext) { + final ListenerAdapter listener = new ListenerAdapter(path, streamName, outputType, controllerContext); try { LOCK.lock(); dataChangeListener.put(streamName, listener); @@ -180,10 +181,11 @@ public final class Notificator { * @return List of {@link NotificationListenerAdapter} by paths */ public static List createNotificationListener(final List paths, - final String streamName, final String outputType) { + final String streamName, final String outputType, final ControllerContext controllerContext) { final List listListeners = new ArrayList<>(); for (final SchemaPath path : paths) { - final NotificationListenerAdapter listener = new NotificationListenerAdapter(path, streamName, outputType); + final NotificationListenerAdapter listener = + new NotificationListenerAdapter(path, streamName, outputType, controllerContext); listListeners.add(listener); } try { diff --git a/restconf/restconf-nb-bierman02/src/main/resources/org/opendaylight/blueprint/restconf-config.xml b/restconf/restconf-nb-bierman02/src/main/resources/org/opendaylight/blueprint/restconf-config.xml index 51d72f74ea..0e523d0d4f 100644 --- a/restconf/restconf-nb-bierman02/src/main/resources/org/opendaylight/blueprint/restconf-config.xml +++ b/restconf/restconf-nb-bierman02/src/main/resources/org/opendaylight/blueprint/restconf-config.xml @@ -53,14 +53,19 @@ + + + + + + - - - + @@ -68,7 +73,9 @@ + destroy-method="close"> + + diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/md/sal/rest/common/TestRestconfUtils.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/md/sal/rest/common/TestRestconfUtils.java index 39b881ac65..9c4795e287 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/md/sal/rest/common/TestRestconfUtils.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/md/sal/rest/common/TestRestconfUtils.java @@ -8,6 +8,11 @@ package org.opendaylight.controller.md.sal.rest.common; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; + +import com.google.common.base.Optional; import com.google.common.base.Preconditions; import java.io.File; import java.io.FileNotFoundException; @@ -20,11 +25,16 @@ import java.util.List; import javax.xml.parsers.ParserConfigurationException; import javax.xml.stream.XMLStreamException; import javax.xml.transform.dom.DOMSource; +import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint; +import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService; +import org.opendaylight.controller.sal.core.api.model.SchemaService; import org.opendaylight.controller.sal.rest.impl.test.providers.TestJsonBodyWriter; +import org.opendaylight.mdsal.dom.api.DOMSchemaService; import org.opendaylight.netconf.sal.restconf.impl.ControllerContext; import org.opendaylight.restconf.common.context.InstanceIdentifierContext; import org.opendaylight.restconf.common.context.NormalizedNodeContext; import org.opendaylight.yangtools.util.xml.UntrustedXML; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter; import org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream; @@ -51,6 +61,27 @@ public final class TestRestconfUtils { throw new UnsupportedOperationException("Test utility class"); } + public static ControllerContext newControllerContext(SchemaContext schemaContext) { + return newControllerContext(schemaContext, null); + } + + public static ControllerContext newControllerContext(SchemaContext schemaContext, DOMMountPoint mountInstance) { + if (mountInstance != null) { + doReturn(schemaContext).when(mountInstance).getSchemaContext(); + } + + final DOMMountPointService mockMountService = mock(DOMMountPointService.class); + doReturn(Optional.fromNullable(mountInstance)).when(mockMountService) + .getMountPoint(any(YangInstanceIdentifier.class)); + + SchemaService mockSchemaService = mock(SchemaService.class); + doReturn(schemaContext).when(mockSchemaService).getGlobalContext(); + + DOMSchemaService mockDomSchemaService = mock(DOMSchemaService.class); + + return ControllerContext.newInstance(mockSchemaService, mockMountService, mockDomSchemaService); + } + @SuppressWarnings("checkstyle:IllegalCatch") public static SchemaContext loadSchemaContext(final String yangPath, final SchemaContext schemaContext) { try { @@ -73,8 +104,8 @@ public final class TestRestconfUtils { @SuppressWarnings("checkstyle:IllegalCatch") public static NormalizedNodeContext loadNormalizedContextFromXmlFile(final String pathToInputFile, - final String uri) { - final InstanceIdentifierContext iiContext = ControllerContext.getInstance().toInstanceIdentifier(uri); + final String uri, final ControllerContext controllerContext) { + final InstanceIdentifierContext iiContext = controllerContext.toInstanceIdentifier(uri); final InputStream inputStream = TestJsonBodyWriter.class.getResourceAsStream(pathToInputFile); try { final Document doc = UntrustedXML.newDocumentBuilder().parse(inputStream); diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/InstanceIdentifierTypeLeafTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/InstanceIdentifierTypeLeafTest.java index 4e0801c2c8..98eb5a0890 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/InstanceIdentifierTypeLeafTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/InstanceIdentifierTypeLeafTest.java @@ -25,9 +25,9 @@ public class InstanceIdentifierTypeLeafTest { public void stringToInstanceIdentifierTest() throws Exception { final SchemaContext schemaContext = YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/instanceidentifier")); - ControllerContext.getInstance().setGlobalSchema(schemaContext); + ControllerContext controllerContext = TestRestconfUtils.newControllerContext(schemaContext); final InstanceIdentifierContext instanceIdentifier = - ControllerContext.getInstance().toInstanceIdentifier( + controllerContext.toInstanceIdentifier( "/iid-value-module:cont-iid/iid-list/%2Fiid-value-module%3Acont-iid%2Fiid-value-module%3A" + "values-iid%5Biid-value-module:value-iid='value'%5D"); final YangInstanceIdentifier yiD = instanceIdentifier.getInstanceIdentifier(); diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/AbstractBodyReaderTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/AbstractBodyReaderTest.java index 0912ce245a..adc3ad3dbb 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/AbstractBodyReaderTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/AbstractBodyReaderTest.java @@ -20,6 +20,7 @@ import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Request; import javax.ws.rs.core.UriInfo; +import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint; import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; import org.opendaylight.netconf.sal.rest.api.RestconfConstants; import org.opendaylight.netconf.sal.rest.impl.AbstractIdentifierAwareJaxRsProvider; @@ -29,20 +30,29 @@ import org.opendaylight.restconf.common.patch.PatchContext; import org.opendaylight.yangtools.yang.model.api.SchemaContext; public abstract class AbstractBodyReaderTest { - - protected static final ControllerContext CONTROLLER_CONTEXT = ControllerContext.getInstance(); - protected final MediaType mediaType; private static Field uriField; private static Field requestField; - public AbstractBodyReaderTest() throws NoSuchFieldException { - uriField = AbstractIdentifierAwareJaxRsProvider.class - .getDeclaredField("uriInfo"); - uriField.setAccessible(true); - requestField = AbstractIdentifierAwareJaxRsProvider.class - .getDeclaredField("request"); - requestField.setAccessible(true); + static { + try { + uriField = AbstractIdentifierAwareJaxRsProvider.class + .getDeclaredField("uriInfo"); + uriField.setAccessible(true); + requestField = AbstractIdentifierAwareJaxRsProvider.class + .getDeclaredField("request"); + requestField.setAccessible(true); + } catch (NoSuchFieldException e) { + throw new RuntimeException(e); + } + } + + protected final ControllerContext controllerContext; + protected final MediaType mediaType; + + protected AbstractBodyReaderTest(SchemaContext schemaContext, DOMMountPoint mountInstance) { this.mediaType = getMediaType(); + + controllerContext = TestRestconfUtils.newControllerContext(schemaContext, mountInstance); } protected abstract MediaType getMediaType(); diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyReader.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyReader.java index 7d5b015c16..cffbea138b 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyReader.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyReader.java @@ -46,8 +46,9 @@ public class TestJsonBodyReader extends AbstractBodyReaderTest { private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create( URI.create("instance:identifier:module"), Revision.of("2014-01-17")); - public TestJsonBodyReader() throws NoSuchFieldException, SecurityException { - this.jsonBodyReader = new JsonNormalizedNodeBodyReader(); + public TestJsonBodyReader() { + super(schemaContext, null); + this.jsonBodyReader = new JsonNormalizedNodeBodyReader(controllerContext); } @Override @@ -61,7 +62,6 @@ public class TestJsonBodyReader extends AbstractBodyReaderTest { final Collection testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang"); testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc")); schemaContext = YangParserTestUtils.parseYangFiles(testFiles); - CONTROLLER_CONTEXT.setSchemas(schemaContext); } @Test diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyReaderMountPoint.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyReaderMountPoint.java index 328e6182c1..675425704d 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyReaderMountPoint.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyReaderMountPoint.java @@ -10,11 +10,8 @@ package org.opendaylight.controller.sal.rest.impl.test.providers; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import com.google.common.base.Optional; import java.io.File; import java.io.InputStream; import java.net.URI; @@ -23,10 +20,8 @@ import javax.ws.rs.core.MediaType; import org.junit.BeforeClass; import org.junit.Test; import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint; -import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService; import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader; -import org.opendaylight.netconf.sal.restconf.impl.ControllerContext; import org.opendaylight.restconf.common.context.NormalizedNodeContext; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.common.QNameModule; @@ -49,9 +44,9 @@ public class TestJsonBodyReaderMountPoint extends AbstractBodyReaderTest { private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create( URI.create("instance:identifier:module"), Revision.of("2014-01-17")); - public TestJsonBodyReaderMountPoint() throws NoSuchFieldException, - SecurityException { - this.jsonBodyReader = new JsonNormalizedNodeBodyReader(); + public TestJsonBodyReaderMountPoint() throws NoSuchFieldException, SecurityException { + super(schemaContext, mock(DOMMountPoint.class)); + this.jsonBodyReader = new JsonNormalizedNodeBodyReader(controllerContext); } @Override @@ -64,15 +59,6 @@ public class TestJsonBodyReaderMountPoint extends AbstractBodyReaderTest { final Collection testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang"); testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc")); schemaContext = YangParserTestUtils.parseYangFiles(testFiles); - - final DOMMountPoint mountInstance = mock(DOMMountPoint.class); - when(mountInstance.getSchemaContext()).thenReturn(schemaContext); - final DOMMountPointService mockMountService = mock(DOMMountPointService.class); - when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))) - .thenReturn(Optional.of(mountInstance)); - - ControllerContext.getInstance().setMountService(mockMountService); - CONTROLLER_CONTEXT.setSchemas(schemaContext); } @Test diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyWriter.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyWriter.java index 8f7b008ec6..24c58a03ad 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyWriter.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonBodyWriter.java @@ -31,9 +31,10 @@ public class TestJsonBodyWriter extends AbstractBodyReaderTest { private final NormalizedNodeJsonBodyWriter jsonBodyWriter; private static SchemaContext schemaContext; - public TestJsonBodyWriter() throws NoSuchFieldException, SecurityException { + public TestJsonBodyWriter() { + super(schemaContext, null); this.jsonBodyWriter = new NormalizedNodeJsonBodyWriter(); - this.jsonBodyReader = new JsonNormalizedNodeBodyReader(); + this.jsonBodyReader = new JsonNormalizedNodeBodyReader(controllerContext); } @Override @@ -46,7 +47,6 @@ public class TestJsonBodyWriter extends AbstractBodyReaderTest { final Collection testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang"); testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc")); schemaContext = YangParserTestUtils.parseYangFiles(testFiles); - CONTROLLER_CONTEXT.setSchemas(schemaContext); } @Test diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonPatchBodyReader.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonPatchBodyReader.java index aec82f69dc..b29136702d 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonPatchBodyReader.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonPatchBodyReader.java @@ -26,8 +26,9 @@ public class TestJsonPatchBodyReader extends AbstractBodyReaderTest { private final JsonToPatchBodyReader jsonToPatchBodyReader; private static SchemaContext schemaContext; - public TestJsonPatchBodyReader() throws NoSuchFieldException, SecurityException { - jsonToPatchBodyReader = new JsonToPatchBodyReader(); + public TestJsonPatchBodyReader() { + super(schemaContext, null); + jsonToPatchBodyReader = new JsonToPatchBodyReader(controllerContext); } @Override @@ -38,7 +39,6 @@ public class TestJsonPatchBodyReader extends AbstractBodyReaderTest { @BeforeClass public static void initialization() { schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext); - CONTROLLER_CONTEXT.setSchemas(schemaContext); } @Test diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonPatchBodyReaderMountPoint.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonPatchBodyReaderMountPoint.java index 8567d67b5a..eca76aa33a 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonPatchBodyReaderMountPoint.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestJsonPatchBodyReaderMountPoint.java @@ -11,21 +11,16 @@ package org.opendaylight.controller.sal.rest.impl.test.providers; import static javax.ws.rs.core.MediaType.APPLICATION_JSON; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; -import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import com.google.common.base.Optional; import java.io.InputStream; import javax.ws.rs.core.MediaType; import org.junit.BeforeClass; import org.junit.Test; import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint; -import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService; import org.opendaylight.netconf.sal.rest.impl.JsonToPatchBodyReader; import org.opendaylight.restconf.common.errors.RestconfDocumentedException; import org.opendaylight.restconf.common.patch.PatchContext; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.model.api.SchemaContext; public class TestJsonPatchBodyReaderMountPoint extends AbstractBodyReaderTest { @@ -34,8 +29,9 @@ public class TestJsonPatchBodyReaderMountPoint extends AbstractBodyReaderTest { private static SchemaContext schemaContext; private static final String MOUNT_POINT = "instance-identifier-module:cont/yang-ext:mount"; - public TestJsonPatchBodyReaderMountPoint() throws NoSuchFieldException, SecurityException { - jsonToPatchBodyReader = new JsonToPatchBodyReader(); + public TestJsonPatchBodyReaderMountPoint() { + super(schemaContext, mock(DOMMountPoint.class)); + jsonToPatchBodyReader = new JsonToPatchBodyReader(controllerContext); } @Override @@ -46,15 +42,6 @@ public class TestJsonPatchBodyReaderMountPoint extends AbstractBodyReaderTest { @BeforeClass public static void initialization() { schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext); - - final DOMMountPoint mockMountPoint = mock(DOMMountPoint.class); - when(mockMountPoint.getSchemaContext()).thenReturn(schemaContext); - final DOMMountPointService mockMountPointService = mock(DOMMountPointService.class); - when(mockMountPointService.getMountPoint(any(YangInstanceIdentifier.class))) - .thenReturn(Optional.of(mockMountPoint)); - - CONTROLLER_CONTEXT.setMountService(mockMountPointService); - CONTROLLER_CONTEXT.setSchemas(schemaContext); } @Test diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlBodyReader.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlBodyReader.java index a34308160e..cd3762e1d5 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlBodyReader.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlBodyReader.java @@ -49,8 +49,9 @@ public class TestXmlBodyReader extends AbstractBodyReaderTest { private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create( URI.create("instance:identifier:module"), Revision.of("2014-01-17")); - public TestXmlBodyReader() throws Exception { - this.xmlBodyReader = new XmlNormalizedNodeBodyReader(); + public TestXmlBodyReader() { + super(schemaContext, null); + this.xmlBodyReader = new XmlNormalizedNodeBodyReader(controllerContext); } @Override @@ -64,7 +65,6 @@ public class TestXmlBodyReader extends AbstractBodyReaderTest { testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc")); testFiles.addAll(TestRestconfUtils.loadFiles("/foo-xml-test/yang")); schemaContext = YangParserTestUtils.parseYangFiles(testFiles); - CONTROLLER_CONTEXT.setSchemas(schemaContext); } @Test diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlBodyReaderMountPoint.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlBodyReaderMountPoint.java index 214ee79cc0..955a310b24 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlBodyReaderMountPoint.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlBodyReaderMountPoint.java @@ -11,11 +11,8 @@ package org.opendaylight.controller.sal.rest.impl.test.providers; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import com.google.common.base.Optional; import java.io.File; import java.io.InputStream; import java.net.URI; @@ -25,10 +22,8 @@ import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint; -import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService; import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader; -import org.opendaylight.netconf.sal.restconf.impl.ControllerContext; import org.opendaylight.restconf.common.context.NormalizedNodeContext; import org.opendaylight.restconf.common.errors.RestconfDocumentedException; import org.opendaylight.restconf.common.errors.RestconfError; @@ -52,8 +47,9 @@ public class TestXmlBodyReaderMountPoint extends AbstractBodyReaderTest { private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create( URI.create("instance:identifier:module"), Revision.of("2014-01-17")); - public TestXmlBodyReaderMountPoint() throws Exception { - this.xmlBodyReader = new XmlNormalizedNodeBodyReader(); + public TestXmlBodyReaderMountPoint() { + super(schemaContext, mock(DOMMountPoint.class)); + this.xmlBodyReader = new XmlNormalizedNodeBodyReader(controllerContext); } @Override @@ -66,15 +62,6 @@ public class TestXmlBodyReaderMountPoint extends AbstractBodyReaderTest { final Collection testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang"); testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc")); schemaContext = YangParserTestUtils.parseYangFiles(testFiles); - - final DOMMountPoint mountInstance = mock(DOMMountPoint.class); - when(mountInstance.getSchemaContext()).thenReturn(schemaContext); - final DOMMountPointService mockMountService = mock(DOMMountPointService.class); - when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))) - .thenReturn(Optional.of(mountInstance)); - - ControllerContext.getInstance().setMountService(mockMountService); - CONTROLLER_CONTEXT.setSchemas(schemaContext); } @Test diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlBodyWriter.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlBodyWriter.java index ca594a3690..ef1bffee38 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlBodyWriter.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlBodyWriter.java @@ -28,7 +28,8 @@ public class TestXmlBodyWriter extends AbstractBodyReaderTest { private final NormalizedNodeXmlBodyWriter xmlBodyWriter; private static SchemaContext schemaContext; - public TestXmlBodyWriter() throws NoSuchFieldException, SecurityException { + public TestXmlBodyWriter() { + super(schemaContext, null); this.xmlBodyWriter = new NormalizedNodeXmlBodyWriter(); } @@ -42,7 +43,6 @@ public class TestXmlBodyWriter extends AbstractBodyReaderTest { final Collection testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang"); testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc")); schemaContext = YangParserTestUtils.parseYangFiles(testFiles); - CONTROLLER_CONTEXT.setSchemas(schemaContext); } @Test @@ -50,7 +50,7 @@ public class TestXmlBodyWriter extends AbstractBodyReaderTest { final String uri = "invoke-rpc-module:rpc-test"; final String pathToInputFile = "/invoke-rpc/xml/rpc-output.xml"; final NormalizedNodeContext nnContext = TestRestconfUtils - .loadNormalizedContextFromXmlFile(pathToInputFile, uri); + .loadNormalizedContextFromXmlFile(pathToInputFile, uri, controllerContext); final OutputStream output = new ByteArrayOutputStream(); this.xmlBodyWriter.writeTo(nnContext, null, null, null, this.mediaType, null, output); diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlPatchBodyReader.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlPatchBodyReader.java index 1bdbab9519..9850f57452 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlPatchBodyReader.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlPatchBodyReader.java @@ -25,8 +25,9 @@ public class TestXmlPatchBodyReader extends AbstractBodyReaderTest { private final XmlToPatchBodyReader xmlToPatchBodyReader; private static SchemaContext schemaContext; - public TestXmlPatchBodyReader() throws NoSuchFieldException, SecurityException { - xmlToPatchBodyReader = new XmlToPatchBodyReader(); + public TestXmlPatchBodyReader() { + super(schemaContext, null); + xmlToPatchBodyReader = new XmlToPatchBodyReader(controllerContext); } @Override @@ -37,7 +38,6 @@ public class TestXmlPatchBodyReader extends AbstractBodyReaderTest { @BeforeClass public static void initialization() throws NoSuchFieldException, SecurityException { schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext); - CONTROLLER_CONTEXT.setSchemas(schemaContext); } @Test diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlPatchBodyReaderMountPoint.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlPatchBodyReaderMountPoint.java index b9fd057ea2..04cbc95fb8 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlPatchBodyReaderMountPoint.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/rest/impl/test/providers/TestXmlPatchBodyReaderMountPoint.java @@ -10,21 +10,16 @@ package org.opendaylight.controller.sal.rest.impl.test.providers; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; -import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import com.google.common.base.Optional; import java.io.InputStream; import javax.ws.rs.core.MediaType; import org.junit.BeforeClass; import org.junit.Test; import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint; -import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService; import org.opendaylight.netconf.sal.rest.impl.XmlToPatchBodyReader; import org.opendaylight.restconf.common.errors.RestconfDocumentedException; import org.opendaylight.restconf.common.patch.PatchContext; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.model.api.SchemaContext; public class TestXmlPatchBodyReaderMountPoint extends AbstractBodyReaderTest { @@ -33,8 +28,9 @@ public class TestXmlPatchBodyReaderMountPoint extends AbstractBodyReaderTest { private static SchemaContext schemaContext; private static final String MOUNT_POINT = "instance-identifier-module:cont/yang-ext:mount"; - public TestXmlPatchBodyReaderMountPoint() throws NoSuchFieldException, SecurityException { - xmlToPatchBodyReader = new XmlToPatchBodyReader(); + public TestXmlPatchBodyReaderMountPoint() { + super(schemaContext, mock(DOMMountPoint.class)); + xmlToPatchBodyReader = new XmlToPatchBodyReader(controllerContext); } @Override @@ -45,15 +41,6 @@ public class TestXmlPatchBodyReaderMountPoint extends AbstractBodyReaderTest { @BeforeClass public static void initialization() throws NoSuchFieldException, SecurityException { schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext); - - final DOMMountPoint mockMountPoint = mock(DOMMountPoint.class); - when(mockMountPoint.getSchemaContext()).thenReturn(schemaContext); - final DOMMountPointService mockMountPointService = mock(DOMMountPointService.class); - when(mockMountPointService.getMountPoint(any(YangInstanceIdentifier.class))) - .thenReturn(Optional.of(mockMountPoint)); - - CONTROLLER_CONTEXT.setMountService(mockMountPointService); - CONTROLLER_CONTEXT.setSchemas(schemaContext); } @Test diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/input/to/cnsn/test/RestPutListDataTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/input/to/cnsn/test/RestPutListDataTest.java index c18c13a232..7d1d524d40 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/input/to/cnsn/test/RestPutListDataTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/input/to/cnsn/test/RestPutListDataTest.java @@ -17,14 +17,15 @@ import static org.mockito.Mockito.when; import com.google.common.collect.Iterables; import com.google.common.util.concurrent.CheckedFuture; import java.io.FileNotFoundException; -import java.net.URI; import java.util.List; import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.UriInfo; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; import org.mockito.Mockito; +import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; import org.opendaylight.controller.sal.restconf.impl.test.TestUtils; import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade; import org.opendaylight.netconf.sal.restconf.impl.ControllerContext; @@ -53,24 +54,22 @@ import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; import org.opendaylight.yangtools.yang.model.api.SchemaContext; public class RestPutListDataTest { + private static SchemaContext schemaContextTestModule; private static BrokerFacade brokerFacade; private static RestconfImpl restconfImpl; - private static SchemaContext schemaContextTestModule; private static final String TEST_MODULE_NS_STRING = "test:module"; - private static final URI TEST_MODULE_NS; private static final String TEST_MODULE_REVISION = "2014-01-09"; - static { - TEST_MODULE_NS = URI.create("test:module"); + @BeforeClass + public static void staticSetup() throws FileNotFoundException { + schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module"); } @Before public void initialize() throws FileNotFoundException { - final ControllerContext controllerContext = ControllerContext.getInstance(); - schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module"); - controllerContext.setSchemas(schemaContextTestModule); + final ControllerContext controllerContext = TestRestconfUtils.newControllerContext(schemaContextTestModule); brokerFacade = mock(BrokerFacade.class); restconfImpl = RestconfImpl.getInstance(); restconfImpl.setBroker(brokerFacade); diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/nn/test/JsonIdentityrefToNnTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/nn/test/JsonIdentityrefToNnTest.java index 0345beab95..e3bb5c31f7 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/nn/test/JsonIdentityrefToNnTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/nn/test/JsonIdentityrefToNnTest.java @@ -25,16 +25,14 @@ public class JsonIdentityrefToNnTest extends AbstractBodyReaderTest { private final JsonNormalizedNodeBodyReader jsonBodyReader; private static SchemaContext schemaContext; - public JsonIdentityrefToNnTest() throws NoSuchFieldException, - SecurityException { - this.jsonBodyReader = new JsonNormalizedNodeBodyReader(); + public JsonIdentityrefToNnTest() { + super(schemaContext, null); + this.jsonBodyReader = new JsonNormalizedNodeBodyReader(controllerContext); } @BeforeClass public static void initialize() { - schemaContext = schemaContextLoader("/json-to-nn/identityref", - schemaContext); - CONTROLLER_CONTEXT.setSchemas(schemaContext); + schemaContext = schemaContextLoader("/json-to-nn/identityref", schemaContext); } @Test diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/nn/test/JsonLeafrefToNnTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/nn/test/JsonLeafrefToNnTest.java index f0bfa63d34..d25e361012 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/nn/test/JsonLeafrefToNnTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/nn/test/JsonLeafrefToNnTest.java @@ -25,15 +25,14 @@ public class JsonLeafrefToNnTest extends AbstractBodyReaderTest { private final JsonNormalizedNodeBodyReader jsonBodyReader; private static SchemaContext schemaContext; - public JsonLeafrefToNnTest() throws NoSuchFieldException, SecurityException { - this.jsonBodyReader = new JsonNormalizedNodeBodyReader(); + public JsonLeafrefToNnTest() { + super(schemaContext, null); + this.jsonBodyReader = new JsonNormalizedNodeBodyReader(controllerContext); } @BeforeClass public static void initialize() { - schemaContext = schemaContextLoader("/json-to-nn/leafref", - schemaContext); - CONTROLLER_CONTEXT.setSchemas(schemaContext); + schemaContext = schemaContextLoader("/json-to-nn/leafref", schemaContext); } @Test diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/nn/test/JsonToNnTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/nn/test/JsonToNnTest.java index 7f1de7f3f1..b6289b4009 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/nn/test/JsonToNnTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/nn/test/JsonToNnTest.java @@ -13,17 +13,23 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.util.Collection; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.MediaType; +import org.junit.BeforeClass; import org.junit.Test; +import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; import org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest; import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader; import org.opendaylight.restconf.common.context.NormalizedNodeContext; import org.opendaylight.restconf.common.errors.RestconfDocumentedException; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes; import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -31,35 +37,38 @@ public class JsonToNnTest extends AbstractBodyReaderTest { private static final Logger LOG = LoggerFactory.getLogger(AbstractBodyReaderTest.class); - private JsonNormalizedNodeBodyReader jsonBodyReader; - private SchemaContext schemaContext; + private final JsonNormalizedNodeBodyReader jsonBodyReader; + private static SchemaContext schemaContext; - public JsonToNnTest() throws NoSuchFieldException, SecurityException { + public JsonToNnTest() { + super(schemaContext, null); + this.jsonBodyReader = new JsonNormalizedNodeBodyReader(controllerContext); } - public static void initialize(final String path, SchemaContext schemaContext) { - schemaContext = schemaContextLoader(path, schemaContext); - CONTROLLER_CONTEXT.setSchemas(schemaContext); + @BeforeClass + public static void initialize() throws FileNotFoundException { + final Collection testFiles = TestRestconfUtils.loadFiles("/json-to-nn/simple-list-yang/1"); + testFiles.addAll(TestRestconfUtils.loadFiles("/json-to-nn/simple-list-yang/3")); + testFiles.addAll(TestRestconfUtils.loadFiles("/json-to-nn/simple-list-yang/4")); + testFiles.addAll(TestRestconfUtils.loadFiles("/json-to-nn/simple-container-yang")); + testFiles.addAll(TestRestconfUtils.loadFiles("/common/augment/yang")); + schemaContext = YangParserTestUtils.parseYangFiles(testFiles); } @Test public void simpleListTest() throws Exception { simpleTest("/json-to-nn/simple-list.json", - "/json-to-nn/simple-list-yang/1", "lst", "simple-list-yang1"); + "lst", "simple-list-yang1"); } @Test public void simpleContainerTest() throws Exception { simpleTest("/json-to-nn/simple-container.json", - "/json-to-nn/simple-container-yang", "cont", - "simple-container-yang"); + "cont", "simple-container-yang"); } @Test public void multipleItemsInLeafListTest() throws Exception { - - initialize("/json-to-nn/simple-list-yang/1", this.schemaContext); - final NormalizedNodeContext normalizedNodeContext = prepareNNC( "/json-to-nn/multiple-leaflist-items.json", "simple-list-yang1:lst"); @@ -74,8 +83,6 @@ public class JsonToNnTest extends AbstractBodyReaderTest { @Test public void multipleItemsInListTest() throws Exception { - initialize("/json-to-nn/simple-list-yang/3", this.schemaContext); - final NormalizedNodeContext normalizedNodeContext = prepareNNC( "/json-to-nn/multiple-items-in-list.json", "multiple-items-yang:lst"); @@ -89,8 +96,6 @@ public class JsonToNnTest extends AbstractBodyReaderTest { @Test public void nullArrayToSimpleNodeWithNullValueTest() throws Exception { - initialize("/json-to-nn/simple-list-yang/4", this.schemaContext); - final NormalizedNodeContext normalizedNodeContext = prepareNNC( "/json-to-nn/array-with-null.json", "array-with-null-yang:cont"); assertNotNull(normalizedNodeContext); @@ -106,9 +111,6 @@ public class JsonToNnTest extends AbstractBodyReaderTest { @Test public void incorrectTopLevelElementsTest() throws Exception { - - this.jsonBodyReader = new JsonNormalizedNodeBodyReader(); - initialize("/json-to-nn/simple-list-yang/1", this.schemaContext); mockBodyReader("simple-list-yang1:lst", this.jsonBodyReader, false); InputStream inputStream = this.getClass().getResourceAsStream( @@ -126,7 +128,7 @@ public class JsonToNnTest extends AbstractBodyReaderTest { } assertNotNull(exception); assertEquals( - "Error parsing input: Schema node with name cont was not found under " + "Error parsing input: Schema node with name wrong was not found under " + "(urn:ietf:params:xml:ns:netconf:base:1.0)data.", exception.getErrors().get(0).getErrorMessage()); @@ -166,9 +168,6 @@ public class JsonToNnTest extends AbstractBodyReaderTest { @Test public void emptyDataReadTest() throws Exception { - - initialize("/json-to-nn/simple-list-yang/4", this.schemaContext); - final NormalizedNodeContext normalizedNodeContext = prepareNNC( "/json-to-nn/empty-data.json", "array-with-null-yang:cont"); assertNotNull(normalizedNodeContext); @@ -183,7 +182,6 @@ public class JsonToNnTest extends AbstractBodyReaderTest { assertTrue(dataTree.contains("lflst2 45")); - this.jsonBodyReader = new JsonNormalizedNodeBodyReader(); RestconfDocumentedException exception = null; mockBodyReader("array-with-null-yang:cont", this.jsonBodyReader, false); final InputStream inputStream = this.getClass().getResourceAsStream( @@ -202,7 +200,6 @@ public class JsonToNnTest extends AbstractBodyReaderTest { @Test public void testJsonBlankInput() throws Exception { - initialize("/json-to-nn/simple-list-yang/4", this.schemaContext); final NormalizedNodeContext normalizedNodeContext = prepareNNC("", "array-with-null-yang:cont"); assertNull(normalizedNodeContext); @@ -210,9 +207,6 @@ public class JsonToNnTest extends AbstractBodyReaderTest { @Test public void notSupplyNamespaceIfAlreadySupplied()throws Exception { - - initialize("/json-to-nn/simple-list-yang/1", this.schemaContext); - final String uri = "simple-list-yang1" + ":" + "lst"; final NormalizedNodeContext normalizedNodeContext = prepareNNC( @@ -238,9 +232,6 @@ public class JsonToNnTest extends AbstractBodyReaderTest { @Test public void dataAugmentedTest() throws Exception { - - initialize("/common/augment/yang", this.schemaContext); - NormalizedNodeContext normalizedNodeContext = prepareNNC( "/common/augment/json/dataa.json", "main:cont"); @@ -265,11 +256,8 @@ public class JsonToNnTest extends AbstractBodyReaderTest { assertTrue(dataTree.contains("lf11 lf11 value from b")); } - private void simpleTest(final String jsonPath, final String yangPath, - final String topLevelElementName, final String moduleName) throws Exception { - - initialize(yangPath, this.schemaContext); - + private void simpleTest(final String jsonPath, final String topLevelElementName, + final String moduleName) throws Exception { final String uri = moduleName + ":" + topLevelElementName; final NormalizedNodeContext normalizedNodeContext = prepareNNC(jsonPath, uri); @@ -279,7 +267,6 @@ public class JsonToNnTest extends AbstractBodyReaderTest { } private NormalizedNodeContext prepareNNC(final String jsonPath, final String uri) throws Exception { - this.jsonBodyReader = new JsonNormalizedNodeBodyReader(); try { mockBodyReader(uri, this.jsonBodyReader, false); } catch (NoSuchFieldException | SecurityException @@ -329,8 +316,6 @@ public class JsonToNnTest extends AbstractBodyReaderTest { @Test public void unsupportedDataFormatTest() throws Exception { - this.jsonBodyReader = new JsonNormalizedNodeBodyReader(); - initialize("/json-to-nn/simple-list-yang/1", this.schemaContext); mockBodyReader("simple-list-yang1:lst", this.jsonBodyReader, false); final InputStream inputStream = this.getClass().getResourceAsStream( @@ -352,9 +337,6 @@ public class JsonToNnTest extends AbstractBodyReaderTest { @Test public void invalidUriCharacterInValue() throws Exception { - - this.jsonBodyReader = new JsonNormalizedNodeBodyReader(); - initialize("/json-to-nn/simple-list-yang/4", this.schemaContext); mockBodyReader("array-with-null-yang:cont", this.jsonBodyReader, false); final InputStream inputStream = this.getClass().getResourceAsStream( diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/json/test/NnJsonChoiceCaseTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/json/test/NnJsonChoiceCaseTest.java index c83261e028..43e950c9e9 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/json/test/NnJsonChoiceCaseTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/json/test/NnJsonChoiceCaseTest.java @@ -26,15 +26,14 @@ public class NnJsonChoiceCaseTest extends AbstractBodyReaderTest { private static SchemaContext schemaContext; private final NormalizedNodeJsonBodyWriter jsonBodyWriter; - public NnJsonChoiceCaseTest() throws NoSuchFieldException, - SecurityException { + public NnJsonChoiceCaseTest() { + super(schemaContext, null); jsonBodyWriter = new NormalizedNodeJsonBodyWriter(); } @BeforeClass public static void initialization() { schemaContext = schemaContextLoader("/nn-to-json/choice", schemaContext); - CONTROLLER_CONTEXT.setSchemas(schemaContext); } /** @@ -167,7 +166,7 @@ public class NnJsonChoiceCaseTest extends AbstractBodyReaderTest { private String getJson(final String xmlPath) throws Exception { final String uri = "choice-case-test:cont"; final NormalizedNodeContext testNN = TestRestconfUtils - .loadNormalizedContextFromXmlFile(xmlPath, uri); + .loadNormalizedContextFromXmlFile(xmlPath, uri, controllerContext); final OutputStream output = new ByteArrayOutputStream(); jsonBodyWriter.writeTo(testNN, null, null, null, mediaType, null, diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/json/test/NnToJsonLeafrefType.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/json/test/NnToJsonLeafrefType.java index c5d7713328..cb1bf574c8 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/json/test/NnToJsonLeafrefType.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/json/test/NnToJsonLeafrefType.java @@ -28,15 +28,14 @@ public class NnToJsonLeafrefType extends AbstractBodyReaderTest { private static SchemaContext schemaContext; private final NormalizedNodeJsonBodyWriter jsonBodyWriter; - public NnToJsonLeafrefType() throws NoSuchFieldException, SecurityException { + public NnToJsonLeafrefType() { + super(schemaContext, null); jsonBodyWriter = new NormalizedNodeJsonBodyWriter(); } @BeforeClass public static void initialization() { - schemaContext = schemaContextLoader("/nn-to-json/leafref", - schemaContext); - CONTROLLER_CONTEXT.setSchemas(schemaContext); + schemaContext = schemaContextLoader("/nn-to-json/leafref", schemaContext); } @Test @@ -91,7 +90,7 @@ public class NnToJsonLeafrefType extends AbstractBodyReaderTest { final String pathToInputFile = xmlDataPath; final NormalizedNodeContext testNN = TestRestconfUtils - .loadNormalizedContextFromXmlFile(pathToInputFile, uri); + .loadNormalizedContextFromXmlFile(pathToInputFile, uri, controllerContext); final OutputStream output = new ByteArrayOutputStream(); jsonBodyWriter.writeTo(testNN, null, null, null, mediaType, null, diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/json/test/NnToJsonWithAugmentTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/json/test/NnToJsonWithAugmentTest.java index 0aadde5a78..0623a07be5 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/json/test/NnToJsonWithAugmentTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/json/test/NnToJsonWithAugmentTest.java @@ -29,16 +29,14 @@ public class NnToJsonWithAugmentTest extends AbstractBodyReaderTest { private static SchemaContext schemaContext; private final NormalizedNodeJsonBodyWriter xmlBodyWriter; - public NnToJsonWithAugmentTest() throws NoSuchFieldException, - SecurityException { + public NnToJsonWithAugmentTest() { + super(schemaContext, null); xmlBodyWriter = new NormalizedNodeJsonBodyWriter(); } @BeforeClass public static void initialize() { - schemaContext = schemaContextLoader("/nn-to-json/augmentation", - schemaContext); - CONTROLLER_CONTEXT.setSchemas(schemaContext); + schemaContext = schemaContextLoader("/nn-to-json/augmentation", schemaContext); } @Test @@ -48,7 +46,7 @@ public class NnToJsonWithAugmentTest extends AbstractBodyReaderTest { final String pathToInputFile = "/nn-to-json/augmentation/xml/data.xml"; final NormalizedNodeContext testNN = TestRestconfUtils - .loadNormalizedContextFromXmlFile(pathToInputFile, uri); + .loadNormalizedContextFromXmlFile(pathToInputFile, uri, controllerContext); final OutputStream output = new ByteArrayOutputStream(); xmlBodyWriter diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnInstanceIdentifierToXmlTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnInstanceIdentifierToXmlTest.java index f1e5786b47..b6c6ec3207 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnInstanceIdentifierToXmlTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnInstanceIdentifierToXmlTest.java @@ -48,16 +48,14 @@ public class NnInstanceIdentifierToXmlTest extends AbstractBodyReaderTest { private static SchemaContext schemaContext; NormalizedNodeXmlBodyWriter xmlBodyWriter; - public NnInstanceIdentifierToXmlTest() throws NoSuchFieldException, - SecurityException { + public NnInstanceIdentifierToXmlTest() { + super(schemaContext, null); xmlBodyWriter = new NormalizedNodeXmlBodyWriter(); } @BeforeClass public static void initialization() throws URISyntaxException { - schemaContext = schemaContextLoader("/instanceidentifier/yang", - schemaContext); - CONTROLLER_CONTEXT.setSchemas(schemaContext); + schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext); } @Test diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnToXmlTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnToXmlTest.java index da5f91e25c..e5d4c8cc7c 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnToXmlTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnToXmlTest.java @@ -52,14 +52,14 @@ public class NnToXmlTest extends AbstractBodyReaderTest { private final NormalizedNodeXmlBodyWriter xmlBodyWriter; private static SchemaContext schemaContext; - public NnToXmlTest() throws NoSuchFieldException, SecurityException { + public NnToXmlTest() { + super(schemaContext, null); this.xmlBodyWriter = new NormalizedNodeXmlBodyWriter(); } @BeforeClass public static void initialization() { schemaContext = schemaContextLoader("/nn-to-xml/yang", schemaContext); - CONTROLLER_CONTEXT.setSchemas(schemaContext); } @Test diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnToXmlWithChoiceTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnToXmlWithChoiceTest.java index 01830d816c..6fbb4683e5 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnToXmlWithChoiceTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnToXmlWithChoiceTest.java @@ -40,15 +40,14 @@ public class NnToXmlWithChoiceTest extends AbstractBodyReaderTest { private final NormalizedNodeXmlBodyWriter xmlBodyWriter; private static SchemaContext schemaContext; - public NnToXmlWithChoiceTest() throws NoSuchFieldException, - SecurityException { + public NnToXmlWithChoiceTest() { + super(schemaContext, null); xmlBodyWriter = new NormalizedNodeXmlBodyWriter(); } @BeforeClass public static void initialization() { schemaContext = schemaContextLoader("/nn-to-xml/choice", schemaContext); - CONTROLLER_CONTEXT.setSchemas(schemaContext); } @Test diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnToXmlWithDataFromSeveralModulesTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnToXmlWithDataFromSeveralModulesTest.java index 08539d0a5b..1f4b0456d8 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnToXmlWithDataFromSeveralModulesTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/xml/test/NnToXmlWithDataFromSeveralModulesTest.java @@ -42,16 +42,14 @@ public class NnToXmlWithDataFromSeveralModulesTest extends private final NormalizedNodeXmlBodyWriter xmlBodyWriter; private static SchemaContext schemaContext; - public NnToXmlWithDataFromSeveralModulesTest() throws NoSuchFieldException, - SecurityException { + public NnToXmlWithDataFromSeveralModulesTest() { + super(schemaContext, null); xmlBodyWriter = new NormalizedNodeXmlBodyWriter(); } @BeforeClass public static void initialize() { - schemaContext = schemaContextLoader( - "/nn-to-xml/data-of-several-modules/yang", schemaContext); - CONTROLLER_CONTEXT.setSchemas(schemaContext); + schemaContext = schemaContextLoader("/nn-to-xml/data-of-several-modules/yang", schemaContext); } @Test diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/BrokerFacadeTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/BrokerFacadeTest.java index 70e1c340a6..287598868f 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/BrokerFacadeTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/BrokerFacadeTest.java @@ -28,7 +28,6 @@ import com.google.common.base.Optional; import com.google.common.collect.Lists; import com.google.common.util.concurrent.CheckedFuture; import com.google.common.util.concurrent.Futures; - import java.util.HashMap; import java.util.concurrent.Future; import org.junit.Before; @@ -53,6 +52,7 @@ import org.opendaylight.controller.md.sal.dom.api.DOMRpcException; import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult; import org.opendaylight.controller.md.sal.dom.api.DOMRpcService; import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain; +import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade; import org.opendaylight.netconf.sal.restconf.impl.ControllerContext; import org.opendaylight.netconf.sal.restconf.impl.PutResult; @@ -107,22 +107,26 @@ public class BrokerFacadeTest { private final QName qname = TestUtils.buildQName("interfaces","test:module", "2014-01-09"); private final SchemaPath type = SchemaPath.create(true, this.qname); private final YangInstanceIdentifier instanceID = YangInstanceIdentifier.builder().node(this.qname).build(); + private ControllerContext controllerContext; @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); + + controllerContext = TestRestconfUtils.newControllerContext( + TestUtils.loadSchemaContext("/full-versions/test-module", "/modules")); + this.brokerFacade.setDomDataBroker(this.domDataBroker); this.brokerFacade.setDomNotificationService(this.domNotification); this.brokerFacade.setRpcService(this.mockRpcService); + this.brokerFacade.setControllerContext(controllerContext); + when(this.domDataBroker.newReadOnlyTransaction()).thenReturn(this.readTransaction); when(this.domDataBroker.newWriteOnlyTransaction()).thenReturn(this.writeTransaction); when(this.domDataBroker.newReadWriteTransaction()).thenReturn(this.rwTransaction); HashMap extensions = new HashMap(); extensions.put(DOMDataTreeChangeService.class, Mockito.mock(DOMDataTreeChangeService.class)); when(this.domDataBroker.getSupportedExtensions()).thenReturn(extensions); - - ControllerContext.getInstance() - .setSchemas(TestUtils.loadSchemaContext("/full-versions/test-module", "/modules")); } private static CheckedFuture>, ReadFailedException> wrapDummyNode( @@ -331,7 +335,7 @@ public class BrokerFacadeTest { @Test public void testRegisterToListenDataChanges() { final ListenerAdapter listener = Notificator.createListener(this.instanceID, "stream", - NotificationOutputType.XML); + NotificationOutputType.XML, controllerContext); @SuppressWarnings("unchecked") final ListenerRegistration mockRegistration = mock(ListenerRegistration.class); @@ -362,7 +366,7 @@ public class BrokerFacadeTest { final String identifier = "create-notification-stream/toaster:toastDone"; final SchemaPath path = SchemaPath.create(true, QName.create("http://netconfcentral.org/ns/toaster", "2009-11-20", "toastDone")); - Notificator.createNotificationListener(Lists.newArrayList(path), identifier, "XML"); + Notificator.createNotificationListener(Lists.newArrayList(path), identifier, "XML", controllerContext); final NotificationListenerAdapter listener = Notificator.getNotificationListenerFor(identifier).get(0); // mock registration diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/Bug3595Test.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/Bug3595Test.java index db96f0cb2b..96d7b5e385 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/Bug3595Test.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/Bug3595Test.java @@ -14,6 +14,7 @@ import static org.junit.Assert.assertNotNull; import java.io.FileNotFoundException; import org.junit.BeforeClass; import org.junit.Test; +import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; import org.opendaylight.netconf.sal.restconf.impl.ControllerContext; import org.opendaylight.restconf.common.context.InstanceIdentifierContext; import org.opendaylight.yangtools.yang.common.QName; @@ -27,19 +28,17 @@ public class Bug3595Test { private static final QName CONT_QNAME = QName.create("leafref:module", "2014-04-17", "cont"); private static final QName LST_WITH_LFREF_KEY_QNAME = QName.create(CONT_QNAME, "lst-with-lfref-key"); private static final QName LFREF_KEY_QNAME = QName.create(CONT_QNAME, "lfref-key"); + private static SchemaContext schemaContext; - - private static ControllerContext controllerContext = ControllerContext.getInstance(); + private final ControllerContext controllerContext = TestRestconfUtils.newControllerContext(schemaContext); @BeforeClass public static void initialize() throws FileNotFoundException { - final SchemaContext schemaContext = TestUtils.loadSchemaContext("/leafref/yang"); + schemaContext = TestUtils.loadSchemaContext("/leafref/yang"); Module module = TestUtils.findModule(schemaContext.getModules(), "leafref-module"); assertNotNull(module); module = TestUtils.findModule(schemaContext.getModules(), "referenced-module"); assertNotNull(module); - - controllerContext.setGlobalSchema(schemaContext); } @Test diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/Bug8072Test.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/Bug8072Test.java index ad2ce0dc7b..a6dbef2c3a 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/Bug8072Test.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/Bug8072Test.java @@ -9,11 +9,9 @@ package org.opendaylight.controller.sal.restconf.impl.test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import com.google.common.base.Optional; import java.io.FileNotFoundException; import java.util.HashMap; import java.util.Map; @@ -21,7 +19,7 @@ import java.util.Set; import org.junit.BeforeClass; import org.junit.Test; import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint; -import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService; +import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade; import org.opendaylight.netconf.sal.restconf.impl.ControllerContext; import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl; @@ -40,21 +38,33 @@ public class Bug8072Test { private static final QName TYPE_QNAME = QName.create("test:module", "2014-01-09", "type"); private static final QName MODULE_TYPE_QNAME = QName.create("test:module", "2014-01-09", "module-type"); - private static final ControllerContext CONTROLLER_CONTEXT = ControllerContext.getInstance(); + private static SchemaContext schemaContext; + + private final ControllerContext controllerContext; + + public Bug8072Test() throws FileNotFoundException { + final SchemaContext mountPointContext = TestUtils.loadSchemaContext("/full-versions/test-module"); + final DOMMountPoint mountInstance = mock(DOMMountPoint.class); + controllerContext = TestRestconfUtils.newControllerContext(schemaContext, mountInstance); + doReturn(mountPointContext).when(mountInstance).getSchemaContext(); + + final BrokerFacade brokerFacade = mock(BrokerFacade.class); + final RestconfImpl restconfImpl = RestconfImpl.getInstance(); + restconfImpl.setBroker(brokerFacade); + restconfImpl.setControllerContext(controllerContext); + } @BeforeClass public static void init() throws FileNotFoundException, ReactorException { - final SchemaContext globalContext = TestUtils.loadSchemaContext("/full-versions/yangs"); - assertEquals(0, globalContext.findModules(EXTERNAL_MODULE_NAME).size()); - final Set allModules = globalContext.getModules(); + schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs"); + assertEquals(0, schemaContext.findModules(EXTERNAL_MODULE_NAME).size()); + final Set allModules = schemaContext.getModules(); assertNotNull(allModules); - CONTROLLER_CONTEXT.setSchemas(globalContext); } @Test public void testIdentityRefFromExternalModule() throws FileNotFoundException, ReactorException { - initMountService(); - final InstanceIdentifierContext ctx = CONTROLLER_CONTEXT.toInstanceIdentifier( + final InstanceIdentifierContext ctx = controllerContext.toInstanceIdentifier( "simple-nodes:users/yang-ext:mount/test-module:modules/module/test-module:module-type/name"); final Map keyValues = new HashMap<>(); @@ -65,17 +75,4 @@ public class Bug8072Test { assertEquals(expectedYII, ctx.getInstanceIdentifier()); } - - private void initMountService() throws FileNotFoundException, ReactorException { - final DOMMountPointService mountService = mock(DOMMountPointService.class); - CONTROLLER_CONTEXT.setMountService(mountService); - final BrokerFacade brokerFacade = mock(BrokerFacade.class); - final RestconfImpl restconfImpl = RestconfImpl.getInstance(); - restconfImpl.setBroker(brokerFacade); - restconfImpl.setControllerContext(CONTROLLER_CONTEXT); - final SchemaContext mountPointContext = TestUtils.loadSchemaContext("/full-versions/test-module"); - final DOMMountPoint mountInstance = mock(DOMMountPoint.class); - when(mountInstance.getSchemaContext()).thenReturn(mountPointContext); - when(mountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance)); - } } diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/CodecsExceptionsCatchingTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/CodecsExceptionsCatchingTest.java index 67f1041227..a671a6c85a 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/CodecsExceptionsCatchingTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/CodecsExceptionsCatchingTest.java @@ -17,9 +17,10 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.test.JerseyTest; -import org.junit.BeforeClass; +import org.junit.Before; import org.junit.Ignore; import org.junit.Test; +import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader; import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter; import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter; @@ -32,15 +33,14 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; public class CodecsExceptionsCatchingTest extends JerseyTest { - private static RestconfImpl restConf; - private static ControllerContext controllerContext = ControllerContext.getInstance(); + private RestconfImpl restConf; + private ControllerContext controllerContext; - @BeforeClass - public static void init() throws FileNotFoundException, ReactorException { + @Before + public void init() throws FileNotFoundException, ReactorException { restConf = RestconfImpl.getInstance(); - controllerContext = ControllerContext.getInstance(); final SchemaContext schemaContext = TestUtils.loadSchemaContext("/decoding-exception/yang"); - controllerContext.setGlobalSchema(schemaContext); + controllerContext = TestRestconfUtils.newControllerContext(schemaContext); restConf.setControllerContext(controllerContext); } @@ -53,7 +53,8 @@ public class CodecsExceptionsCatchingTest extends JerseyTest { // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue()); ResourceConfig resourceConfig = new ResourceConfig(); resourceConfig = resourceConfig.registerInstances(restConf, new NormalizedNodeJsonBodyWriter(), - new NormalizedNodeXmlBodyWriter(), new XmlNormalizedNodeBodyReader(), new JsonNormalizedNodeBodyReader()); + new NormalizedNodeXmlBodyWriter(), new XmlNormalizedNodeBodyReader(controllerContext), + new JsonNormalizedNodeBodyReader(controllerContext)); resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class); return resourceConfig; } @@ -66,4 +67,4 @@ public class CodecsExceptionsCatchingTest extends JerseyTest { final String exceptionMessage = response.readEntity(String.class); assertTrue(exceptionMessage.contains("invalid-value")); } -} \ No newline at end of file +} diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/CutDataToCorrectDepthTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/CutDataToCorrectDepthTest.java index 649ec8fc87..1224c3d389 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/CutDataToCorrectDepthTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/CutDataToCorrectDepthTest.java @@ -30,6 +30,7 @@ import javax.ws.rs.core.UriInfo; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.test.JerseyTest; import org.junit.BeforeClass; +import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader; import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter; import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter; @@ -73,6 +74,9 @@ public class CutDataToCorrectDepthTest extends JerseyTest { private NormalizedNode globalPayload; private static SchemaContext schemaContextModules; + private final ControllerContext controllerContext = + TestRestconfUtils.newControllerContext(schemaContextModules, null); + @Path("/") public class RestImpl { @@ -82,8 +86,7 @@ public class CutDataToCorrectDepthTest extends JerseyTest { public NormalizedNodeContext getData(@Encoded @PathParam("identifier") final String identifier, @Context final UriInfo uriInfo) { - final InstanceIdentifierContext iiWithData = ControllerContext.getInstance().toInstanceIdentifier( - identifier); + final InstanceIdentifierContext iiWithData = controllerContext.toInstanceIdentifier(identifier); NormalizedNode data = null; if (identifier.equals("nested-module:depth1-cont/depth2-cont1")) { @@ -167,7 +170,6 @@ public class CutDataToCorrectDepthTest extends JerseyTest { } public void getDataWithUriDepthParameter(final String mediaType) throws WebApplicationException, IOException { - ControllerContext.getInstance().setGlobalSchema(schemaContextModules); Response response; // Test config with depth 1 diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ExpressionParserTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ExpressionParserTest.java index 434304da21..220537909c 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ExpressionParserTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ExpressionParserTest.java @@ -134,7 +134,8 @@ public class ExpressionParserTest { final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class); final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-12-14", "localName")); Mockito.when(path.getLastPathArgument()).thenReturn(pathValue); - final ListenerAdapter listener = Notificator.createListener(path, "streamName", NotificationOutputType.JSON); + final ListenerAdapter listener = Notificator.createListener(path, "streamName", NotificationOutputType.JSON, + null); listener.setQueryParams(Instant.now(), Optional.empty(), Optional.ofNullable(filter), false); // FIXME: do not use reflection here diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/InvokeRpcMethodTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/InvokeRpcMethodTest.java index bebb7f4e5f..8f97a1e756 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/InvokeRpcMethodTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/InvokeRpcMethodTest.java @@ -16,7 +16,6 @@ import static org.junit.Assert.fail; import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; import com.google.common.base.Optional; @@ -32,7 +31,6 @@ import java.util.Set; import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.UriInfo; -import org.junit.Before; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; @@ -40,6 +38,7 @@ import org.opendaylight.controller.md.sal.dom.api.DOMRpcException; import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationNotAvailableException; import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult; import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult; +import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade; import org.opendaylight.netconf.sal.restconf.impl.ControllerContext; import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl; @@ -71,32 +70,31 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; public class InvokeRpcMethodTest { - private RestconfImpl restconfImpl = null; - private static ControllerContext controllerContext = null; private static UriInfo uriInfo; + private static SchemaContext schemaContext; + private final RestconfImpl restconfImpl = RestconfImpl.getInstance(); + private final ControllerContext controllerContext; + + public InvokeRpcMethodTest() { + controllerContext = TestRestconfUtils.newControllerContext(schemaContext); + restconfImpl.setControllerContext(controllerContext); + } @BeforeClass public static void init() throws FileNotFoundException, ReactorException { - final SchemaContext schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs", "/invoke-rpc"); + schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs", "/invoke-rpc"); final Set allModules = schemaContext.getModules(); assertNotNull(allModules); final Module module = TestUtils.resolveModule("invoke-rpc-module", allModules); assertNotNull(module); - controllerContext = spy(ControllerContext.getInstance()); - controllerContext.setSchemas(schemaContext); + uriInfo = mock(UriInfo.class); final MultivaluedMap map = new MultivaluedHashMap<>(); map.put("prettyPrint", Collections.singletonList("true")); when(uriInfo.getQueryParameters(any(Boolean.class))).thenReturn(map); } - @Before - public void initMethod() { - this.restconfImpl = RestconfImpl.getInstance(); - this.restconfImpl.setControllerContext(controllerContext); - } - /** * Test method invokeRpc in RestconfImpl class tests if composite node as input parameter of method invokeRpc * (second argument) is wrapped to parent composite node which has QName equals to QName of rpc (resolved from @@ -105,28 +103,26 @@ public class InvokeRpcMethodTest { @Test @Ignore public void invokeRpcMethodTest() { - final ControllerContext contContext = controllerContext; try { - contContext.findModuleNameByNamespace(new URI("invoke:rpc:module")); + controllerContext.findModuleNameByNamespace(new URI("invoke:rpc:module")); } catch (final URISyntaxException e) { assertTrue("Uri wasn't created sucessfuly", false); } final BrokerFacade mockedBrokerFacade = mock(BrokerFacade.class); - final RestconfImpl restconf = RestconfImpl.getInstance(); - restconf.setBroker(mockedBrokerFacade); - restconf.setControllerContext(contContext); + restconfImpl.setBroker(mockedBrokerFacade); final NormalizedNodeContext payload = prepareDomPayload(); - final NormalizedNodeContext rpcResponse = restconf.invokeRpc("invoke-rpc-module:rpc-test", payload, uriInfo); + final NormalizedNodeContext rpcResponse = + restconfImpl.invokeRpc("invoke-rpc-module:rpc-test", payload, uriInfo); assertTrue(rpcResponse != null); assertTrue(rpcResponse.getData() == null); } - private static NormalizedNodeContext prepareDomPayload() { + private NormalizedNodeContext prepareDomPayload() { final SchemaContext schema = controllerContext.getGlobalSchema(); final Module rpcModule = schema.findModules("invoke-rpc-module").iterator().next(); assertNotNull(rpcModule); @@ -289,7 +285,6 @@ public class InvokeRpcMethodTest { final SchemaPath path = SchemaPath.create(true, QName.create("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)make-toast")); - final SchemaContext schemaContext = controllerContext.getGlobalSchema(); final Module rpcModule = schemaContext.findModules("toaster").iterator().next(); assertNotNull(rpcModule); final QName rpcQName = QName.create(rpcModule.getQNameModule(), "make-toast"); diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/JSONRestconfServiceImplTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/JSONRestconfServiceImplTest.java index 5037c4bbf0..390a87f719 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/JSONRestconfServiceImplTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/JSONRestconfServiceImplTest.java @@ -19,7 +19,6 @@ import static org.mockito.Matchers.notNull; import static org.mockito.Matchers.same; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.reset; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -41,11 +40,11 @@ import org.mockito.Mockito; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint; -import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService; import org.opendaylight.controller.md.sal.dom.api.DOMRpcException; import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationNotAvailableException; import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult; import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult; +import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade; import org.opendaylight.netconf.sal.restconf.impl.ControllerContext; import org.opendaylight.netconf.sal.restconf.impl.JSONRestconfServiceImpl; @@ -106,21 +105,27 @@ public class JSONRestconfServiceImplTest { static final QName TEST_OUTPUT_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "testOutput"); static final QName TEXT_OUT_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "textOut"); - private static BrokerFacade brokerFacade; + private static SchemaContext schemaContext; - private final JSONRestconfServiceImpl service = new JSONRestconfServiceImpl(); + private final BrokerFacade brokerFacade = mock(BrokerFacade.class); + private final DOMMountPoint mockMountPoint = mock(DOMMountPoint.class); + private JSONRestconfServiceImpl service; @BeforeClass public static void init() throws IOException, ReactorException { - ControllerContext.getInstance().setSchemas(TestUtils.loadSchemaContext("/full-versions/yangs")); - brokerFacade = mock(BrokerFacade.class); - RestconfImpl.getInstance().setBroker(brokerFacade); - RestconfImpl.getInstance().setControllerContext(ControllerContext.getInstance()); + schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs"); } @Before - public void setup() { - reset(brokerFacade); + public void setup() throws FileNotFoundException { + final SchemaContext mountPointSchemaContext = TestUtils.loadSchemaContext("/full-versions/test-module"); + final ControllerContext controllerContext = + TestRestconfUtils.newControllerContext(schemaContext, mockMountPoint); + doReturn(mountPointSchemaContext).when(mockMountPoint).getSchemaContext(); + + service = new JSONRestconfServiceImpl(controllerContext); + RestconfImpl.getInstance().setBroker(brokerFacade); + RestconfImpl.getInstance().setControllerContext(controllerContext); } private static String loadData(final String path) throws IOException { @@ -164,7 +169,6 @@ public class JSONRestconfServiceImplTest { @SuppressWarnings("rawtypes") @Test public void testPutBehindMountPoint() throws Exception { - final DOMMountPoint mockMountPoint = setupTestMountPoint(); final PutResult result = mock(PutResult.class); when(brokerFacade.commitMountPointDataPut(notNull(DOMMountPoint.class), notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class), Mockito.anyString(), @@ -253,7 +257,6 @@ public class JSONRestconfServiceImplTest { @SuppressWarnings("rawtypes") @Test public void testPostBehindMountPoint() throws Exception { - final DOMMountPoint mockMountPoint = setupTestMountPoint(); doReturn(Futures.immediateCheckedFuture(null)).when(brokerFacade).commitConfigurationDataPost( notNull(DOMMountPoint.class), notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class), Mockito.anyString(), Mockito.anyString()); @@ -323,7 +326,6 @@ public class JSONRestconfServiceImplTest { @SuppressWarnings("rawtypes") @Test public void testPatchBehindMountPoint() throws Exception { - final DOMMountPoint mockMountPoint = setupTestMountPoint(); final PatchStatusContext result = mock(PatchStatusContext.class); when(brokerFacade.patchConfigurationDataWithinTransaction(notNull(PatchContext.class))) .thenReturn(result); @@ -527,19 +529,6 @@ public class JSONRestconfServiceImplTest { new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"}); } - DOMMountPoint setupTestMountPoint() throws FileNotFoundException, ReactorException { - final SchemaContext schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module"); - final DOMMountPoint mockMountPoint = mock(DOMMountPoint.class); - doReturn(schemaContextTestModule).when(mockMountPoint).getSchemaContext(); - - final DOMMountPointService mockMountService = mock(DOMMountPointService.class); - doReturn(Optional.of(mockMountPoint)) - .when(mockMountService).getMountPoint(notNull(YangInstanceIdentifier.class)); - - ControllerContext.getInstance().setMountService(mockMountService); - return mockMountPoint; - } - void verifyLeafNode(final DataContainerNode parent, final QName leafType, final Object leafValue) { final java.util.Optional> leafChild = parent.getChild(new NodeIdentifier(leafType)); assertEquals(leafType.toString() + " present", true, leafChild.isPresent()); diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/MediaTypesTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/MediaTypesTest.java index 7b3f3f716c..51b7ce0521 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/MediaTypesTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/MediaTypesTest.java @@ -31,22 +31,19 @@ import org.junit.Test; import org.mockito.Mockito; import org.opendaylight.netconf.sal.rest.api.Draft02; import org.opendaylight.netconf.sal.rest.api.RestconfService; -import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader; import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter; import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter; -import org.opendaylight.netconf.sal.rest.impl.RestconfDocumentedExceptionMapper; -import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader; import org.opendaylight.restconf.common.context.NormalizedNodeContext; public class MediaTypesTest extends JerseyTest { - private static RestconfService restconfService; private static String jsonData; private static String xmlData; + private RestconfService restconfService; + @BeforeClass public static void init() throws IOException { - restconfService = mock(RestconfService.class); final String jsonPath = RestconfImplTest.class.getResource("/parts/ietf-interfaces_interfaces.json").getPath(); jsonData = TestUtils.loadTextFile(jsonPath); final InputStream xmlStream = @@ -60,11 +57,11 @@ public class MediaTypesTest extends JerseyTest { // enable(TestProperties.LOG_TRAFFIC); // enable(TestProperties.DUMP_ENTITY); // enable(TestProperties.RECORD_LOG_LEVEL); - // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue()); + // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());' + restconfService = mock(RestconfService.class); ResourceConfig resourceConfig = new ResourceConfig(); resourceConfig = resourceConfig.registerInstances(restconfService, new NormalizedNodeJsonBodyWriter(), - new NormalizedNodeXmlBodyWriter(), new XmlNormalizedNodeBodyReader(), new JsonNormalizedNodeBodyReader()); - resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class); + new NormalizedNodeXmlBodyWriter()); return resourceConfig; } @@ -249,12 +246,12 @@ public class MediaTypesTest extends JerseyTest { private int post(final String uri, final String acceptMediaType, final String contentTypeMediaType, final String data) { if (acceptMediaType == null) { - if ((contentTypeMediaType == null) || (data == null)) { + if (contentTypeMediaType == null || data == null) { return target(uri).request().post(null).getStatus(); } return target(uri).request().post(Entity.entity(data, contentTypeMediaType)).getStatus(); } - if ((contentTypeMediaType == null) || (data == null)) { + if (contentTypeMediaType == null || data == null) { return target(uri).request(acceptMediaType).post(null).getStatus(); } return target(uri).request(acceptMediaType).post(Entity.entity(data, contentTypeMediaType)).getStatus(); diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestCodecExceptionsTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestCodecExceptionsTest.java index b14ec16560..c91a61badb 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestCodecExceptionsTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestCodecExceptionsTest.java @@ -25,7 +25,7 @@ public class RestCodecExceptionsTest { @Test public void serializeExceptionTest() { - final Codec codec = RestCodec.from(BaseTypes.bitsTypeBuilder(PATH).build(), null); + final Codec codec = RestCodec.from(BaseTypes.bitsTypeBuilder(PATH).build(), null, null); final String serializedValue = (String) codec.serialize("incorrect value"); // set // expected assertEquals("incorrect value", serializedValue); @@ -35,7 +35,7 @@ public class RestCodecExceptionsTest { public void deserializeExceptionTest() { final IdentityrefTypeDefinition mockedIidentityrefType = mock(IdentityrefTypeDefinition.class); - final Codec codec = RestCodec.from(mockedIidentityrefType, null); + final Codec codec = RestCodec.from(mockedIidentityrefType, null, null); assertNull(codec.deserialize("incorrect value")); } diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestDeleteOperationTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestDeleteOperationTest.java index aabc469499..72da87bacf 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestDeleteOperationTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestDeleteOperationTest.java @@ -27,6 +27,7 @@ import org.glassfish.jersey.test.JerseyTest; import org.junit.BeforeClass; import org.junit.Test; import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; +import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader; import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter; import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter; @@ -42,23 +43,17 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; public class RestDeleteOperationTest extends JerseyTest { + private static SchemaContext schemaContext; - private static ControllerContext controllerContext; - private static BrokerFacade brokerFacade; - private static RestconfImpl restconfImpl; + private ControllerContext controllerContext; + private BrokerFacade brokerFacade; + private RestconfImpl restconfImpl; @BeforeClass public static void init() throws FileNotFoundException, ReactorException { - final SchemaContext schemaContext = TestUtils.loadSchemaContext("/test-config-data/yang1"); + schemaContext = TestUtils.loadSchemaContext("/test-config-data/yang1"); final Set allModules = schemaContext.getModules(); assertNotNull(allModules); - - controllerContext = ControllerContext.getInstance(); - controllerContext.setSchemas(schemaContext); - brokerFacade = mock(BrokerFacade.class); - restconfImpl = RestconfImpl.getInstance(); - restconfImpl.setBroker(brokerFacade); - restconfImpl.setControllerContext(controllerContext); } @Override @@ -68,10 +63,18 @@ public class RestDeleteOperationTest extends JerseyTest { // enable(TestProperties.DUMP_ENTITY); // enable(TestProperties.RECORD_LOG_LEVEL); // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue()); + controllerContext = TestRestconfUtils.newControllerContext(schemaContext); + controllerContext.setSchemas(schemaContext); + brokerFacade = mock(BrokerFacade.class); + restconfImpl = RestconfImpl.getInstance(); + restconfImpl.setBroker(brokerFacade); + restconfImpl.setControllerContext(controllerContext); + ResourceConfig resourceConfig = new ResourceConfig(); resourceConfig = resourceConfig.registerInstances(restconfImpl, new NormalizedNodeJsonBodyWriter(), - new NormalizedNodeXmlBodyWriter(), new XmlNormalizedNodeBodyReader(), new JsonNormalizedNodeBodyReader()); - resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class); + new NormalizedNodeXmlBodyWriter(), new XmlNormalizedNodeBodyReader(controllerContext), + new JsonNormalizedNodeBodyReader(controllerContext), + new RestconfDocumentedExceptionMapper(controllerContext)); return resourceConfig; } diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestGetAugmentedElementWhenEqualNamesTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestGetAugmentedElementWhenEqualNamesTest.java index bf1841c977..e9488c1811 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestGetAugmentedElementWhenEqualNamesTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestGetAugmentedElementWhenEqualNamesTest.java @@ -16,23 +16,24 @@ import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; import org.opendaylight.netconf.sal.restconf.impl.ControllerContext; import org.opendaylight.restconf.common.context.InstanceIdentifierContext; import org.opendaylight.restconf.common.errors.RestconfDocumentedException; import org.opendaylight.yangtools.yang.model.api.SchemaContext; -import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; public class RestGetAugmentedElementWhenEqualNamesTest { - private static ControllerContext controllerContext = ControllerContext.getInstance(); + private static SchemaContext schemaContext; + + private final ControllerContext controllerContext = TestRestconfUtils.newControllerContext(schemaContext); @Rule public ExpectedException exception = ExpectedException.none(); @BeforeClass - public static void init() throws FileNotFoundException, ReactorException { - final SchemaContext schemaContextTestModule = TestUtils.loadSchemaContext("/common/augment/yang"); - controllerContext.setSchemas(schemaContextTestModule); + public static void init() throws FileNotFoundException { + schemaContext = TestUtils.loadSchemaContext("/common/augment/yang"); } @Test diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestGetOperationTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestGetOperationTest.java index 051ca9e7da..ebe05da1b1 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestGetOperationTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestGetOperationTest.java @@ -19,7 +19,6 @@ import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import com.google.common.base.Optional; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Maps; import java.net.URI; @@ -43,11 +42,10 @@ import org.junit.Ignore; import org.junit.Test; import org.mockito.Mockito; import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint; -import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService; +import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader; import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter; import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter; -import org.opendaylight.netconf.sal.rest.impl.RestconfApplication; import org.opendaylight.netconf.sal.rest.impl.RestconfDocumentedExceptionMapper; import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader; import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade; @@ -86,15 +84,18 @@ public class RestGetOperationTest extends JerseyTest { } } - private static BrokerFacade brokerFacade; - private static RestconfImpl restconfImpl; private static SchemaContext schemaContextYangsIetf; private static SchemaContext schemaContextTestModule; + private static SchemaContext schemaContextModules; + private static SchemaContext schemaContextBehindMountPoint; + @SuppressWarnings("rawtypes") private static NormalizedNode answerFromGet; - private static SchemaContext schemaContextModules; - private static SchemaContext schemaContextBehindMountPoint; + private BrokerFacade brokerFacade; + private RestconfImpl restconfImpl; + private ControllerContext controllerContext; + private DOMMountPoint mountInstance; private static final String RESTCONF_NS = "urn:ietf:params:xml:ns:yang:ietf-restconf"; @@ -102,13 +103,10 @@ public class RestGetOperationTest extends JerseyTest { public static void init() throws Exception { schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs"); schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module"); - brokerFacade = mock(BrokerFacade.class); - restconfImpl = RestconfImpl.getInstance(); - restconfImpl.setBroker(brokerFacade); - answerFromGet = TestUtils.prepareNormalizedNodeWithIetfInterfacesInterfacesData(); - schemaContextModules = TestUtils.loadSchemaContext("/modules"); schemaContextBehindMountPoint = TestUtils.loadSchemaContext("/modules/modules-behind-mount-point"); + + answerFromGet = TestUtils.prepareNormalizedNodeWithIetfInterfacesInterfacesData(); } @Override @@ -118,18 +116,24 @@ public class RestGetOperationTest extends JerseyTest { // enable(TestProperties.DUMP_ENTITY); // enable(TestProperties.RECORD_LOG_LEVEL); // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue()); + + mountInstance = mock(DOMMountPoint.class); + controllerContext = TestRestconfUtils.newControllerContext(schemaContextYangsIetf, mountInstance); + brokerFacade = mock(BrokerFacade.class); + restconfImpl = RestconfImpl.getInstance(); + restconfImpl.setBroker(brokerFacade); + restconfImpl.setControllerContext(controllerContext); + ResourceConfig resourceConfig = new ResourceConfig(); resourceConfig = resourceConfig.registerInstances(restconfImpl, new NormalizedNodeJsonBodyWriter(), - new NormalizedNodeXmlBodyWriter(), new XmlNormalizedNodeBodyReader(), new JsonNormalizedNodeBodyReader()); - resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class); - resourceConfig.registerClasses(new RestconfApplication().getClasses()); + new NormalizedNodeXmlBodyWriter(), new XmlNormalizedNodeBodyReader(controllerContext), + new JsonNormalizedNodeBodyReader(controllerContext), + new RestconfDocumentedExceptionMapper(controllerContext)); return resourceConfig; } - private static void setControllerContext(final SchemaContext schemaContext) { - final ControllerContext controllerContext = ControllerContext.getInstance(); + private void setControllerContext(final SchemaContext schemaContext) { controllerContext.setSchemas(schemaContext); - restconfImpl.setControllerContext(controllerContext); } /** @@ -169,12 +173,7 @@ public class RestGetOperationTest extends JerseyTest { when(brokerFacade.readConfigurationData(any(DOMMountPoint.class), any(YangInstanceIdentifier.class), Mockito.anyString())).thenReturn( prepareCnDataForMountPointTest(false)); - final DOMMountPoint mountInstance = mock(DOMMountPoint.class); when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule); - final DOMMountPointService mockMountService = mock(DOMMountPointService.class); - when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance)); - - ControllerContext.getInstance().setMountService(mockMountService); String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont/cont1"; assertEquals(200, get(uri, MediaType.APPLICATION_XML)); @@ -196,12 +195,8 @@ public class RestGetOperationTest extends JerseyTest { final YangInstanceIdentifier awaitedInstanceIdentifier = prepareInstanceIdentifierForList(); when(brokerFacade.readConfigurationData(any(DOMMountPoint.class), eq(awaitedInstanceIdentifier), Mockito.anyString())).thenReturn(prepareCnDataForSlashesBehindMountPointTest()); - final DOMMountPoint mountInstance = mock(DOMMountPoint.class); - when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule); - final DOMMountPointService mockMountService = mock(DOMMountPointService.class); - when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance)); - ControllerContext.getInstance().setMountService(mockMountService); + when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule); final String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/" + "test-module:cont/lst1/GigabitEthernet0%2F0%2F0%2F0"; @@ -231,12 +226,8 @@ public class RestGetOperationTest extends JerseyTest { when(brokerFacade.readConfigurationData(any(DOMMountPoint.class), any(YangInstanceIdentifier.class), Mockito.anyString())).thenReturn( prepareCnDataForMountPointTest(true)); - final DOMMountPoint mountInstance = mock(DOMMountPoint.class); - when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule); - final DOMMountPointService mockMountService = mock(DOMMountPointService.class); - when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance)); - ControllerContext.getInstance().setMountService(mockMountService); + when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule); final String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont"; assertEquals(200, get(uri, MediaType.APPLICATION_XML)); @@ -269,9 +260,7 @@ public class RestGetOperationTest extends JerseyTest { // /modules @Test public void getModulesTest() throws Exception { - final ControllerContext controllerContext = ControllerContext.getInstance(); - controllerContext.setGlobalSchema(schemaContextModules); - restconfImpl.setControllerContext(controllerContext); + setControllerContext(schemaContextModules); final String uri = "/modules"; @@ -379,43 +368,13 @@ public class RestGetOperationTest extends JerseyTest { } } - private static Matcher validateOperationsResponseXml(final String searchIn, final String rpcName, - final String namespace) { - final StringBuilder regex = new StringBuilder(); - - regex.append("^"); - - regex.append(".*"); - - regex.append(".*<"); - regex.append(".*" + rpcName); - regex.append(".*" + namespace); - regex.append(".*/"); - regex.append(".*>"); - - regex.append(".*"); - - regex.append(".*"); - regex.append("$"); - final Pattern ptrn = Pattern.compile(regex.toString(), Pattern.DOTALL); - return ptrn.matcher(searchIn); - } - // /operations/pathToMountPoint/yang-ext:mount @Ignore @Test public void getOperationsBehindMountPointTest() throws Exception { setControllerContext(schemaContextModules); - final DOMMountPoint mountInstance = mock(DOMMountPoint.class); when(mountInstance.getSchemaContext()).thenReturn(schemaContextBehindMountPoint); - final DOMMountPointService mockMountService = mock(DOMMountPointService.class); - when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance)); - - ControllerContext.getInstance().setMountService(mockMountService); final String uri = "/operations/ietf-interfaces:interfaces/interface/0/yang-ext:mount/"; @@ -449,12 +408,7 @@ public class RestGetOperationTest extends JerseyTest { public void getModulesBehindMountPoint() throws Exception { setControllerContext(schemaContextModules); - final DOMMountPoint mountInstance = mock(DOMMountPoint.class); when(mountInstance.getSchemaContext()).thenReturn(schemaContextBehindMountPoint); - final DOMMountPointService mockMountService = mock(DOMMountPointService.class); - when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance)); - - ControllerContext.getInstance().setMountService(mockMountService); final String uri = "/modules/ietf-interfaces:interfaces/interface/0/yang-ext:mount/"; @@ -482,12 +436,7 @@ public class RestGetOperationTest extends JerseyTest { public void getModuleBehindMountPoint() throws Exception { setControllerContext(schemaContextModules); - final DOMMountPoint mountInstance = mock(DOMMountPoint.class); when(mountInstance.getSchemaContext()).thenReturn(schemaContextBehindMountPoint); - final DOMMountPointService mockMountService = mock(DOMMountPointService.class); - when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance)); - - ControllerContext.getInstance().setMountService(mockMountService); final String uri = "/modules/module/ietf-interfaces:interfaces/interface/0/yang-ext:mount/" + "module1-behind-mount-point/2014-02-03"; @@ -657,12 +606,12 @@ public class RestGetOperationTest extends JerseyTest { } @SuppressWarnings("unchecked") - private static void mockReadOperationalDataMethod() { + private void mockReadOperationalDataMethod() { when(brokerFacade.readOperationalData(any(YangInstanceIdentifier.class))).thenReturn(answerFromGet); } @SuppressWarnings("unchecked") - private static void mockReadConfigurationDataMethod() { + private void mockReadConfigurationDataMethod() { when(brokerFacade.readConfigurationData(any(YangInstanceIdentifier.class), Mockito.anyString())) .thenReturn(answerFromGet); } @@ -735,7 +684,7 @@ public class RestGetOperationTest extends JerseyTest { } @SuppressWarnings({"rawtypes", "unchecked"}) - private static void getDataWithInvalidDepthParameterTest(final UriInfo uriInfo) { + private void getDataWithInvalidDepthParameterTest(final UriInfo uriInfo) { try { final QName qNameDepth1Cont = QName.create("urn:nested:module", "2014-06-3", "depth1-cont"); final YangInstanceIdentifier ii = YangInstanceIdentifier.builder().node(qNameDepth1Cont).build(); diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestPostOperationTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestPostOperationTest.java index 910bd7e1be..0f5ef01fc9 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestPostOperationTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestPostOperationTest.java @@ -15,16 +15,13 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.XML; -import com.google.common.base.Optional; import com.google.common.collect.ImmutableList; import com.google.common.util.concurrent.CheckedFuture; import com.google.common.util.concurrent.Futures; import java.io.IOException; -import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.URISyntaxException; import java.text.ParseException; -import java.util.Set; import javax.ws.rs.client.Entity; import javax.ws.rs.core.Application; import javax.ws.rs.core.MediaType; @@ -37,7 +34,7 @@ import org.mockito.ArgumentCaptor; import org.mockito.Mockito; import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint; -import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService; +import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; import org.opendaylight.netconf.sal.rest.api.Draft02; import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader; import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter; @@ -49,39 +46,29 @@ import org.opendaylight.netconf.sal.restconf.impl.ControllerContext; import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; -import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; public class RestPostOperationTest extends JerseyTest { - private static String xmlDataAbsolutePath; - private static String xmlDataInterfaceAbsolutePath; - private static String xmlDataRpcInput; private static String xmlBlockData; - private static String xmlTestInterface; private static String xmlData3; private static String xmlData4; - private static BrokerFacade brokerFacade; - private static RestconfImpl restconfImpl; private static SchemaContext schemaContextYangsIetf; private static SchemaContext schemaContextTestModule; private static SchemaContext schemaContext; - private static DOMMountPointService mountService; + private BrokerFacade brokerFacade; + private RestconfImpl restconfImpl; + private ControllerContext controllerContext; + private DOMMountPoint mountInstance; @BeforeClass public static void init() throws URISyntaxException, IOException, ReactorException { schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs"); schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module"); - brokerFacade = mock(BrokerFacade.class); - restconfImpl = RestconfImpl.getInstance(); - restconfImpl.setBroker(brokerFacade); - schemaContext = TestUtils.loadSchemaContext("/test-config-data/yang1"); - final Set modules = schemaContext.getModules(); - loadData(); } @@ -92,17 +79,24 @@ public class RestPostOperationTest extends JerseyTest { // enable(TestProperties.DUMP_ENTITY); // enable(TestProperties.RECORD_LOG_LEVEL); // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue()); + + mountInstance = mock(DOMMountPoint.class); + controllerContext = TestRestconfUtils.newControllerContext(schemaContext, mountInstance); + brokerFacade = mock(BrokerFacade.class); + restconfImpl = RestconfImpl.getInstance(); + restconfImpl.setBroker(brokerFacade); + restconfImpl.setControllerContext(controllerContext); + ResourceConfig resourceConfig = new ResourceConfig(); - resourceConfig = resourceConfig.registerInstances(restconfImpl, new XmlNormalizedNodeBodyReader(), - new NormalizedNodeXmlBodyWriter(), new JsonNormalizedNodeBodyReader(), new NormalizedNodeJsonBodyWriter()); - resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class); + resourceConfig = resourceConfig.registerInstances(restconfImpl, + new XmlNormalizedNodeBodyReader(controllerContext), new NormalizedNodeXmlBodyWriter(), + new JsonNormalizedNodeBodyReader(controllerContext), new NormalizedNodeJsonBodyWriter(), + new RestconfDocumentedExceptionMapper(controllerContext)); return resourceConfig; } - private static void setSchemaControllerContext(final SchemaContext schema) { - final ControllerContext context = ControllerContext.getInstance(); - context.setSchemas(schema); - restconfImpl.setControllerContext(context); + private void setSchemaControllerContext(final SchemaContext schema) { + controllerContext.setSchemas(schema); } @SuppressWarnings("unchecked") @@ -110,16 +104,10 @@ public class RestPostOperationTest extends JerseyTest { @Ignore /// xmlData* need netconf-yang public void postDataViaUrlMountPoint() throws UnsupportedEncodingException { setSchemaControllerContext(schemaContextYangsIetf); - when( - brokerFacade.commitConfigurationDataPost(any(DOMMountPoint.class), any(YangInstanceIdentifier.class), - any(NormalizedNode.class), null, null)).thenReturn(mock(CheckedFuture.class)); + when(brokerFacade.commitConfigurationDataPost(any(DOMMountPoint.class), any(YangInstanceIdentifier.class), + any(NormalizedNode.class), null, null)).thenReturn(mock(CheckedFuture.class)); - final DOMMountPoint mountInstance = mock(DOMMountPoint.class); when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule); - final DOMMountPointService mockMountService = mock(DOMMountPointService.class); - when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance)); - - ControllerContext.getInstance().setMountService(mockMountService); String uri = "/config/ietf-interfaces:interfaces/interface/0/"; assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData4)); @@ -134,7 +122,6 @@ public class RestPostOperationTest extends JerseyTest { @Ignore //jenkins has problem with JerseyTest // - we expecting problems with singletons ControllerContext as schemaContext holder public void createConfigurationDataTest() throws UnsupportedEncodingException, ParseException { - initMocking(); when(brokerFacade.commitConfigurationDataPost((SchemaContext) null, any(YangInstanceIdentifier.class), any(NormalizedNode.class), null, null)) .thenReturn(mock(CheckedFuture.class)); @@ -166,8 +153,6 @@ public class RestPostOperationTest extends JerseyTest { @Test public void createConfigurationDataNullTest() throws UnsupportedEncodingException { - initMocking(); - when(brokerFacade.commitConfigurationDataPost(any(SchemaContext.class), any(YangInstanceIdentifier.class), any(NormalizedNode.class), Mockito.anyString(), Mockito.anyString())) .thenReturn(Futures.immediateCheckedFuture(null)); @@ -180,38 +165,14 @@ public class RestPostOperationTest extends JerseyTest { assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData)); } - private static void initMocking() { - final ControllerContext controllerContext = ControllerContext.getInstance(); - controllerContext.setSchemas(schemaContext); - mountService = mock(DOMMountPointService.class); - controllerContext.setMountService(mountService); - brokerFacade = mock(BrokerFacade.class); - restconfImpl = RestconfImpl.getInstance(); - restconfImpl.setBroker(brokerFacade); - restconfImpl.setControllerContext(controllerContext); - } - private int post(final String uri, final String mediaType, final String data) { return target(uri).request(mediaType).post(Entity.entity(data, mediaType)).getStatus(); } private static void loadData() throws IOException, URISyntaxException { - InputStream xmlStream = RestconfImplTest.class - .getResourceAsStream("/parts/ietf-interfaces_interfaces_absolute_path.xml"); - xmlDataAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream)); - xmlStream = RestconfImplTest.class - .getResourceAsStream("/parts/ietf-interfaces_interfaces_interface_absolute_path.xml"); - xmlDataInterfaceAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream)); - final String xmlPathRpcInput = - RestconfImplTest.class.getResource("/full-versions/test-data2/data-rpc-input.xml").getPath(); - xmlDataRpcInput = TestUtils.loadTextFile(xmlPathRpcInput); final String xmlPathBlockData = RestconfImplTest.class.getResource("/test-config-data/xml/block-data.xml").getPath(); xmlBlockData = TestUtils.loadTextFile(xmlPathBlockData); - final String xmlPathTestInterface = - RestconfImplTest.class.getResource("/test-config-data/xml/test-interface.xml").getPath(); - xmlTestInterface = TestUtils.loadTextFile(xmlPathTestInterface); -// cnSnDataOutput = prepareCnSnRpcOutput(); final String data3Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data3.xml").getPath(); xmlData3 = TestUtils.loadTextFile(data3Input); final String data4Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data7.xml").getPath(); diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestPutConfigTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestPutConfigTest.java index 9581117674..043f78b807 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestPutConfigTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestPutConfigTest.java @@ -10,11 +10,13 @@ package org.opendaylight.controller.sal.restconf.impl.test; import com.google.common.util.concurrent.CheckedFuture; import com.google.common.util.concurrent.Futures; +import java.io.FileNotFoundException; import java.util.HashSet; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.UriInfo; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -39,19 +41,22 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext; @RunWith(MockitoJUnitRunner.class) public class RestPutConfigTest { + private static SchemaContext schemaContext; private RestconfImpl restconfService; private ControllerContext controllerCx; - private SchemaContext schemaCx; @Mock private BrokerFacade brokerFacade; + @BeforeClass + public static void staticInit() throws FileNotFoundException { + schemaContext = TestRestconfUtils.loadSchemaContext("/test-config-data/yang1/", null); + } + @Before public void init() { this.restconfService = RestconfImpl.getInstance(); - this.controllerCx = ControllerContext.getInstance(); - this.schemaCx = TestRestconfUtils.loadSchemaContext("/test-config-data/yang1/", null); - this.controllerCx.setSchemas(this.schemaCx); + this.controllerCx = TestRestconfUtils.newControllerContext(schemaContext); this.restconfService.setControllerContext(this.controllerCx); } @@ -131,7 +136,7 @@ public class RestPutConfigTest { final PutResult result = Mockito.mock(PutResult.class); final CheckedFuture checkedFuture = Futures.immediateCheckedFuture(null); - Mockito.when(this.brokerFacade.commitConfigurationDataPut(this.schemaCx, yii, data, null, null)) + Mockito.when(this.brokerFacade.commitConfigurationDataPut(this.schemaContext, yii, data, null, null)) .thenReturn(result); Mockito.when(result.getFutureOfPutData()).thenReturn(checkedFuture); Mockito.when(result.getStatus()).thenReturn(Status.OK); diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestPutOperationTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestPutOperationTest.java index 3400474c30..981c9d29f5 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestPutOperationTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestPutOperationTest.java @@ -14,7 +14,6 @@ import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import com.google.common.base.Optional; import com.google.common.util.concurrent.CheckedFuture; import com.google.common.util.concurrent.Futures; import java.io.FileNotFoundException; @@ -35,7 +34,7 @@ import org.junit.Test; import org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException; import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint; -import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService; +import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader; import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter; import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter; @@ -59,21 +58,17 @@ public class RestPutOperationTest extends JerseyTest { private static String xmlData2; private static String xmlData3; - private static BrokerFacade brokerFacade; - private static RestconfImpl restconfImpl; private static SchemaContext schemaContextYangsIetf; private static SchemaContext schemaContextTestModule; + private BrokerFacade brokerFacade; + private RestconfImpl restconfImpl; + private DOMMountPoint mountInstance; + @BeforeClass public static void init() throws IOException, ReactorException { schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs"); schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module"); - final ControllerContext controllerContext = ControllerContext.getInstance(); - controllerContext.setSchemas(schemaContextYangsIetf); - brokerFacade = mock(BrokerFacade.class); - restconfImpl = RestconfImpl.getInstance(); - restconfImpl.setBroker(brokerFacade); - restconfImpl.setControllerContext(controllerContext); loadData(); } @@ -96,10 +91,20 @@ public class RestPutOperationTest extends JerseyTest { // enable(TestProperties.DUMP_ENTITY); // enable(TestProperties.RECORD_LOG_LEVEL); // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue()); + + mountInstance = mock(DOMMountPoint.class); + final ControllerContext controllerContext = + TestRestconfUtils.newControllerContext(schemaContextYangsIetf, mountInstance); + brokerFacade = mock(BrokerFacade.class); + restconfImpl = RestconfImpl.getInstance(); + restconfImpl.setBroker(brokerFacade); + restconfImpl.setControllerContext(controllerContext); + ResourceConfig resourceConfig = new ResourceConfig(); - resourceConfig = resourceConfig.registerInstances(restconfImpl,new XmlNormalizedNodeBodyReader(), - new NormalizedNodeXmlBodyWriter(), new JsonNormalizedNodeBodyReader(), new NormalizedNodeJsonBodyWriter()); - resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class); + resourceConfig = resourceConfig.registerInstances(restconfImpl, + new XmlNormalizedNodeBodyReader(controllerContext), new NormalizedNodeXmlBodyWriter(), + new JsonNormalizedNodeBodyReader(controllerContext), new NormalizedNodeJsonBodyWriter(), + new RestconfDocumentedExceptionMapper(controllerContext)); return resourceConfig; } @@ -138,12 +143,7 @@ public class RestPutOperationTest extends JerseyTest { when(result.getFutureOfPutData()).thenReturn(dummyFuture); when(result.getStatus()).thenReturn(Status.OK); - final DOMMountPoint mountInstance = mock(DOMMountPoint.class); when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule); - final DOMMountPointService mockMountService = mock(DOMMountPointService.class); - when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance)); - - ControllerContext.getInstance().setMountService(mockMountService); String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont"; assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2)); @@ -161,12 +161,7 @@ public class RestPutOperationTest extends JerseyTest { when(result.getFutureOfPutData()).thenReturn(dummyFuture); when(result.getStatus()).thenReturn(Status.OK); - final DOMMountPoint mountInstance = mock(DOMMountPoint.class); when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule); - final DOMMountPointService mockMountService = mock(DOMMountPointService.class); - when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance)); - - ControllerContext.getInstance().setMountService(mockMountService); final String uri = "/config/ietf-interfaces:interfaces/yang-ext:mount"; assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData3)); @@ -205,7 +200,7 @@ public class RestPutOperationTest extends JerseyTest { return target(uri).request(mediaType).put(Entity.entity(data, mediaType)).getStatus(); } - private static void mockCommitConfigurationDataPutMethod(final boolean noErrors) { + private void mockCommitConfigurationDataPutMethod(final boolean noErrors) { final PutResult putResMock = mock(PutResult.class); if (noErrors) { doReturn(putResMock).when(brokerFacade).commitConfigurationDataPut( diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestconfDocumentedExceptionMapperTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestconfDocumentedExceptionMapperTest.java index 07effb5772..d00d382221 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestconfDocumentedExceptionMapperTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestconfDocumentedExceptionMapperTest.java @@ -53,6 +53,7 @@ import org.junit.Before; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; +import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; import org.opendaylight.netconf.sal.rest.api.Draft02; import org.opendaylight.netconf.sal.rest.api.RestconfService; import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader; @@ -67,6 +68,7 @@ import org.opendaylight.restconf.common.errors.RestconfError; import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag; import org.opendaylight.restconf.common.errors.RestconfError.ErrorType; import org.opendaylight.yangtools.util.xml.UntrustedXML; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Document; @@ -122,9 +124,11 @@ public class RestconfDocumentedExceptionMapperTest extends JerseyTest { static XPathExpression ERROR_APP_TAG; static XPathExpression ERROR_INFO; + private static SchemaContext schemaContext; + @BeforeClass public static void init() throws Exception { - ControllerContext.getInstance().setGlobalSchema(TestUtils.loadSchemaContext("/modules")); + schemaContext = TestUtils.loadSchemaContext("/modules"); final NamespaceContext nsContext = new NamespaceContext() { @Override @@ -162,9 +166,11 @@ public class RestconfDocumentedExceptionMapperTest extends JerseyTest { @Override protected Application configure() { ResourceConfig resourceConfig = new ResourceConfig(); - resourceConfig = resourceConfig.registerInstances(mockRestConf, new XmlNormalizedNodeBodyReader(), - new JsonNormalizedNodeBodyReader(), new NormalizedNodeJsonBodyWriter(), new NormalizedNodeXmlBodyWriter()); - resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class); + ControllerContext controllerContext = TestRestconfUtils.newControllerContext(schemaContext); + resourceConfig = resourceConfig.registerInstances(mockRestConf, + new XmlNormalizedNodeBodyReader(controllerContext), new JsonNormalizedNodeBodyReader(controllerContext), + new NormalizedNodeJsonBodyWriter(), new NormalizedNodeXmlBodyWriter(), + new RestconfDocumentedExceptionMapper(controllerContext)); return resourceConfig; } diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestconfImplNotificationSubscribingTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestconfImplNotificationSubscribingTest.java index 945eb6b211..6e644b9ca1 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestconfImplNotificationSubscribingTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestconfImplNotificationSubscribingTest.java @@ -7,6 +7,7 @@ */ package org.opendaylight.controller.sal.restconf.impl.test; +import java.io.FileNotFoundException; import java.time.Instant; import java.util.AbstractMap.SimpleImmutableEntry; import java.util.ArrayList; @@ -19,6 +20,7 @@ import javax.ws.rs.core.UriBuilder; import javax.ws.rs.core.UriInfo; import org.junit.Assert; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import org.mockito.Mock; import org.mockito.Mockito; @@ -37,12 +39,15 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument; import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils; public class RestconfImplNotificationSubscribingTest { private final String identifier = "data-change-event-subscription/datastore=OPERATIONAL/scope=ONE"; + private static SchemaContext schemaContext; + @Mock private BrokerFacade broker; @@ -52,19 +57,27 @@ public class RestconfImplNotificationSubscribingTest { @Mock private UriInfo uriInfo; + private ControllerContext controllerContext; + + @BeforeClass + public static void init() throws FileNotFoundException { + schemaContext = YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/notifications")); + } + @Before public void setup() throws Exception { MockitoAnnotations.initMocks(this); this.broker.setDomDataBroker(this.domDataBroker); RestconfImpl.getInstance().setBroker(this.broker); - ControllerContext.getInstance() - .setGlobalSchema(YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/notifications"))); + + controllerContext = TestRestconfUtils.newControllerContext(schemaContext); + RestconfImpl.getInstance().setControllerContext(controllerContext); final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class); final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-12-14", "localName")); Mockito.when(path.getLastPathArgument()).thenReturn(pathValue); - Notificator.createListener(path, this.identifier, NotificationOutputType.XML); + Notificator.createListener(path, this.identifier, NotificationOutputType.XML, controllerContext); } @Test @@ -198,7 +211,8 @@ public class RestconfImplNotificationSubscribingTest { final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class); final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-12-14", "localName")); Mockito.when(path.getLastPathArgument()).thenReturn(pathValue); - final ListenerAdapter listener = Notificator.createListener(path, this.identifier, NotificationOutputType.XML); + final ListenerAdapter listener = Notificator.createListener(path, this.identifier, NotificationOutputType.XML, + controllerContext); final List>> list = new ArrayList<>(); final List time = new ArrayList<>(); @@ -208,7 +222,7 @@ public class RestconfImplNotificationSubscribingTest { subscribe(list); - ArrayList candidates = new ArrayList(0); + ArrayList candidates = new ArrayList<>(0); Instant startOrig = listener.getStart(); Assert.assertNotNull(startOrig); listener.onDataTreeChanged(candidates); diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestconfImplTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestconfImplTest.java index 433684041f..26dc658742 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestconfImplTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestconfImplTest.java @@ -42,6 +42,7 @@ import org.mockito.Mockito; import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint; import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult; import org.opendaylight.controller.md.sal.dom.api.DOMRpcService; +import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade; import org.opendaylight.netconf.sal.restconf.impl.ControllerContext; import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl; @@ -73,18 +74,16 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; */ public class RestconfImplTest { - private RestconfImpl restconfImpl = null; - private static ControllerContext controllerContext = null; + private static SchemaContext schemaContext; + + private RestconfImpl restconfImpl; + private final ControllerContext controllerContext = TestRestconfUtils.newControllerContext(schemaContext); @BeforeClass public static void init() throws FileNotFoundException, ReactorException { - final SchemaContext schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs"); - + schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs"); final Set allModules = schemaContext.getModules(); assertNotNull(allModules); - - controllerContext = ControllerContext.getInstance(); - controllerContext.setSchemas(schemaContext); } @Before @@ -214,7 +213,7 @@ public class RestconfImplTest { // register test notification stream final SchemaPath path = SchemaPath.create( true, QName.create("http://netconfcentral.org/ns/toaster", "2009-11-20", "toastDone")); - Notificator.createNotificationListener(Lists.newArrayList(path), identifier, "XML"); + Notificator.createNotificationListener(Lists.newArrayList(path), identifier, "XML", controllerContext); final UriInfo uriInfo = mock(UriInfo.class); final UriBuilder uriBuilder = mock(UriBuilder.class); diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/URIParametersParsing.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/URIParametersParsing.java index 10d7f75d17..f7fbd80bdc 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/URIParametersParsing.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/URIParametersParsing.java @@ -24,6 +24,7 @@ import org.junit.Test; import org.mockito.Mockito; import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade; import org.opendaylight.netconf.sal.restconf.impl.ControllerContext; import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl; @@ -65,8 +66,8 @@ public class URIParametersParsing { public void init() throws FileNotFoundException, ReactorException { this.restconf = RestconfImpl.getInstance(); this.mockedBrokerFacade = mock(BrokerFacade.class); - this.controllerContext = ControllerContext.getInstance(); - this.controllerContext.setSchemas(TestUtils.loadSchemaContext("/datastore-and-scope-specification")); + this.controllerContext = TestRestconfUtils.newControllerContext( + TestUtils.loadSchemaContext("/datastore-and-scope-specification")); this.restconf.setControllerContext(this.controllerContext); this.restconf.setBroker(this.mockedBrokerFacade); } @@ -90,7 +91,7 @@ public class URIParametersParsing { final String datastoreValue = datastore == null ? "CONFIGURATION" : datastore; final String scopeValue = scope == null ? "BASE" : scope + ""; Notificator.createListener(iiBuilder.build(), "dummyStreamName/datastore=" + datastoreValue + "/scope=" - + scopeValue, NotificationOutputType.XML); + + scopeValue, NotificationOutputType.XML, controllerContext); final UriInfo mockedUriInfo = mock(UriInfo.class); @SuppressWarnings("unchecked") diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/URITest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/URITest.java index e3999f63d7..164a241565 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/URITest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/URITest.java @@ -8,64 +8,60 @@ package org.opendaylight.controller.sal.restconf.impl.test; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import com.google.common.base.Optional; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import java.io.FileNotFoundException; -import java.util.Set; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint; -import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService; +import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade; import org.opendaylight.netconf.sal.restconf.impl.ControllerContext; import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl; import org.opendaylight.restconf.common.context.InstanceIdentifierContext; import org.opendaylight.restconf.common.errors.RestconfDocumentedException; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; -import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; public class URITest { - private static final ControllerContext CONTROLLER_CONTEXT = ControllerContext.getInstance(); + private static SchemaContext schemaContext; + private static SchemaContext mountSchemaContext; + + private final DOMMountPoint mountInstance = mock(DOMMountPoint.class); + private final ControllerContext controllerContext = + TestRestconfUtils.newControllerContext(schemaContext, mountInstance); @Rule public ExpectedException exception = ExpectedException.none(); @BeforeClass public static void init() throws FileNotFoundException, ReactorException { - final SchemaContext schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs"); - final Set allModules = schemaContext.getModules(); - assertNotNull(allModules); - - CONTROLLER_CONTEXT.setSchemas(schemaContext); + schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs"); + mountSchemaContext = TestUtils.loadSchemaContext("/test-config-data/yang2"); } @Test public void testToInstanceIdentifierList() throws FileNotFoundException { - InstanceIdentifierContext instanceIdentifier = CONTROLLER_CONTEXT + InstanceIdentifierContext instanceIdentifier = controllerContext .toInstanceIdentifier("simple-nodes:userWithoutClass/foo"); assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass"); - instanceIdentifier = CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:userWithoutClass/foo"); + instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:userWithoutClass/foo"); assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "userWithoutClass"); - instanceIdentifier = CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:user/foo/boo"); + instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user/foo/boo"); assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user"); - instanceIdentifier = CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:user//boo"); + instanceIdentifier = controllerContext.toInstanceIdentifier("simple-nodes:user//boo"); assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "user"); } @@ -73,19 +69,19 @@ public class URITest { @Test public void testToInstanceIdentifierListWithNullKey() { this.exception.expect(RestconfDocumentedException.class); - CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:user/null/boo"); + controllerContext.toInstanceIdentifier("simple-nodes:user/null/boo"); } @Test public void testToInstanceIdentifierListWithMissingKey() { this.exception.expect(RestconfDocumentedException.class); - CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:user/foo"); + controllerContext.toInstanceIdentifier("simple-nodes:user/foo"); } @Test public void testToInstanceIdentifierContainer() throws FileNotFoundException { final InstanceIdentifierContext instanceIdentifier = - CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:users"); + controllerContext.toInstanceIdentifier("simple-nodes:users"); assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "users"); assertTrue(instanceIdentifier.getSchemaNode() instanceof ContainerSchemaNode); assertEquals(2, ((ContainerSchemaNode) instanceIdentifier.getSchemaNode()).getChildNodes().size()); @@ -95,7 +91,7 @@ public class URITest { @Ignore //jenkins has problem with JerseyTest // - we expecting problems with singletons ControllerContext as schemaContext holder public void testToInstanceIdentifierChoice() throws FileNotFoundException { - final InstanceIdentifierContext instanceIdentifier = CONTROLLER_CONTEXT + final InstanceIdentifierContext instanceIdentifier = controllerContext .toInstanceIdentifier("simple-nodes:food/nonalcoholic"); assertEquals(instanceIdentifier.getSchemaNode().getQName().getLocalName(), "nonalcoholic"); } @@ -103,31 +99,31 @@ public class URITest { @Test public void testToInstanceIdentifierChoiceException() { this.exception.expect(RestconfDocumentedException.class); - CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:food/snack"); + controllerContext.toInstanceIdentifier("simple-nodes:food/snack"); } @Test public void testToInstanceIdentifierCaseException() { this.exception.expect(RestconfDocumentedException.class); - CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:food/sports-arena"); + controllerContext.toInstanceIdentifier("simple-nodes:food/sports-arena"); } @Test public void testToInstanceIdentifierChoiceCaseException() { this.exception.expect(RestconfDocumentedException.class); - CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:food/snack/sports-arena"); + controllerContext.toInstanceIdentifier("simple-nodes:food/snack/sports-arena"); } @Test public void testToInstanceIdentifierWithoutNode() { this.exception.expect(RestconfDocumentedException.class); - CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes"); + controllerContext.toInstanceIdentifier("simple-nodes"); } @Test public void testMountPointWithExternModul() throws FileNotFoundException, ReactorException { initMountService(true); - final InstanceIdentifierContext instanceIdentifier = CONTROLLER_CONTEXT + final InstanceIdentifierContext instanceIdentifier = controllerContext .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class/student/name"); assertEquals( "[(urn:ietf:params:xml:ns:yang:test-interface2?revision=2014-08-01)class, " @@ -140,43 +136,29 @@ public class URITest { @Test public void testMountPointWithoutExternModul() throws FileNotFoundException, ReactorException { initMountService(true); - final InstanceIdentifierContext instanceIdentifier = CONTROLLER_CONTEXT + final InstanceIdentifierContext instanceIdentifier = controllerContext .toInstanceIdentifier("simple-nodes:users/yang-ext:mount/"); assertTrue(Iterables.isEmpty(instanceIdentifier.getInstanceIdentifier().getPathArguments())); } - @Test - public void testMountPointWithoutMountService() throws FileNotFoundException { - this.exception.expect(RestconfDocumentedException.class); - - CONTROLLER_CONTEXT.setMountService(null); - CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class/student/name"); - } - @Test public void testMountPointWithoutMountPointSchema() throws FileNotFoundException, ReactorException { initMountService(false); this.exception.expect(RestconfDocumentedException.class); - CONTROLLER_CONTEXT.toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class"); + controllerContext.toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class"); } public void initMountService(final boolean withSchema) throws FileNotFoundException, ReactorException { - final DOMMountPointService mountService = mock(DOMMountPointService.class); - CONTROLLER_CONTEXT.setMountService(mountService); final BrokerFacade brokerFacade = mock(BrokerFacade.class); final RestconfImpl restconfImpl = RestconfImpl.getInstance(); restconfImpl.setBroker(brokerFacade); - restconfImpl.setControllerContext(CONTROLLER_CONTEXT); - final SchemaContext schemaContext2 = TestUtils.loadSchemaContext("/test-config-data/yang2"); - final Set modules2 = schemaContext2.getModules(); + restconfImpl.setControllerContext(controllerContext); - final DOMMountPoint mountInstance = mock(DOMMountPoint.class); if (withSchema) { - when(mountInstance.getSchemaContext()).thenReturn(schemaContext2); + when(mountInstance.getSchemaContext()).thenReturn(mountSchemaContext); } else { when(mountInstance.getSchemaContext()).thenReturn(null); } - when(mountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance)); } } diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/websockets/test/RestStreamTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/websockets/test/RestStreamTest.java index 11f76c8ea4..6400c32258 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/websockets/test/RestStreamTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/websockets/test/RestStreamTest.java @@ -24,6 +24,7 @@ import org.glassfish.jersey.test.JerseyTest; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; +import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; import org.opendaylight.controller.sal.restconf.impl.test.TestUtils; import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader; import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter; @@ -41,19 +42,14 @@ import org.w3c.dom.Node; public class RestStreamTest extends JerseyTest { - private static BrokerFacade brokerFacade; - private static RestconfImpl restconfImpl; private static SchemaContext schemaContextYangsIetf; + private BrokerFacade brokerFacade; + private RestconfImpl restconfImpl; + @BeforeClass public static void init() throws FileNotFoundException, ReactorException { schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs"); - final ControllerContext controllerContext = ControllerContext.getInstance(); - controllerContext.setSchemas(schemaContextYangsIetf); - brokerFacade = mock(BrokerFacade.class); - restconfImpl = RestconfImpl.getInstance(); - restconfImpl.setBroker(brokerFacade); - restconfImpl.setControllerContext(controllerContext); } @Override @@ -63,10 +59,18 @@ public class RestStreamTest extends JerseyTest { // enable(TestProperties.DUMP_ENTITY); // enable(TestProperties.RECORD_LOG_LEVEL); // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue()); + + final ControllerContext controllerContext = TestRestconfUtils.newControllerContext(schemaContextYangsIetf); + brokerFacade = mock(BrokerFacade.class); + restconfImpl = RestconfImpl.getInstance(); + restconfImpl.setBroker(brokerFacade); + restconfImpl.setControllerContext(controllerContext); + ResourceConfig resourceConfig = new ResourceConfig(); resourceConfig = resourceConfig.registerInstances(restconfImpl, new NormalizedNodeJsonBodyWriter(), - new NormalizedNodeXmlBodyWriter(), new XmlNormalizedNodeBodyReader(), new JsonNormalizedNodeBodyReader()); - resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class); + new NormalizedNodeXmlBodyWriter(), new XmlNormalizedNodeBodyReader(controllerContext), + new JsonNormalizedNodeBodyReader(controllerContext), + new RestconfDocumentedExceptionMapper(controllerContext)); return resourceConfig; } diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/netconf/sal/restconf/impl/InstanceIdentifierCodecImplTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/netconf/sal/restconf/impl/InstanceIdentifierCodecImplTest.java index fca1543b05..44940b4a5a 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/netconf/sal/restconf/impl/InstanceIdentifierCodecImplTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/netconf/sal/restconf/impl/InstanceIdentifierCodecImplTest.java @@ -11,7 +11,9 @@ package org.opendaylight.netconf.sal.restconf.impl; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import java.io.FileNotFoundException; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; import org.opendaylight.netconf.sal.restconf.impl.RestCodec.InstanceIdentifierCodecImpl; @@ -23,19 +25,24 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils; public class InstanceIdentifierCodecImplTest { + private static SchemaContext schemaContext; private Codec instanceIdentifierDTO; private YangInstanceIdentifier instanceIdentifierBadNamespace; private YangInstanceIdentifier instanceIdentifierOKList; private YangInstanceIdentifier instanceIdentifierOKLeafList; - private SchemaContext schemaContext; + + @BeforeClass + public static void init() throws FileNotFoundException { + schemaContext = YangParserTestUtils.parseYangFiles( + TestRestconfUtils.loadFiles("/restconf/parser/deserializer")); + } @Before public void setUp() throws Exception { - this.schemaContext = - YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/restconf/parser/deserializer")); - this.instanceIdentifierDTO = new InstanceIdentifierCodecImpl(null); - ControllerContext.getInstance().setGlobalSchema(this.schemaContext); + ControllerContext controllerContext = TestRestconfUtils.newControllerContext(schemaContext); + + this.instanceIdentifierDTO = new InstanceIdentifierCodecImpl(null, controllerContext); final QName baseQName = QName.create("deserializer:test", "2016-06-06", "deserializer-test"); final QName contA = QName.create(baseQName, "contA"); @@ -90,4 +97,4 @@ public class InstanceIdentifierCodecImplTest { this.instanceIdentifierDTO.deserialize(valuesDTO); assertNull(deserializedIdentifier); } -} \ No newline at end of file +} diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/netconf/sal/streams/listeners/ListenerAdapterTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/netconf/sal/streams/listeners/ListenerAdapterTest.java index df9cf7fca0..52ede84d41 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/netconf/sal/streams/listeners/ListenerAdapterTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/netconf/sal/streams/listeners/ListenerAdapterTest.java @@ -19,6 +19,7 @@ import java.nio.file.Paths; import java.util.Optional; import org.json.JSONObject; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; @@ -27,6 +28,7 @@ import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker; import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService; import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier; +import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; import org.opendaylight.netconf.sal.restconf.impl.ControllerContext; import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.PatchCont; import org.opendaylight.yang.gen.v1.instance.identifier.patch.module.rev151121.patch.cont.MyList1; @@ -54,16 +56,23 @@ public class ListenerAdapterTest extends AbstractConcurrentDataBrokerTest { private static YangInstanceIdentifier PATCH_CONT_YIID = YangInstanceIdentifier.create(new YangInstanceIdentifier.NodeIdentifier(PatchCont.QNAME)); + private static SchemaContext schemaContext; + private DataBroker dataBroker; private DOMDataBroker domDataBroker; + private ControllerContext controllerContext; + + @BeforeClass + public static void init() { + schemaContext = YangParserTestUtils.parseYangResource( + "/instanceidentifier/yang/instance-identifier-patch-module.yang"); + } @Before public void setUp() throws Exception { dataBroker = getDataBroker(); domDataBroker = getDomBroker(); - SchemaContext sc = YangParserTestUtils.parseYangResource( - "/instanceidentifier/yang/instance-identifier-patch-module.yang"); - ControllerContext.getInstance().setGlobalSchema(sc); + controllerContext = TestRestconfUtils.newControllerContext(schemaContext); } class ListenerAdapterTester extends ListenerAdapter { @@ -73,7 +82,7 @@ public class ListenerAdapterTest extends AbstractConcurrentDataBrokerTest { ListenerAdapterTester(final YangInstanceIdentifier path, final String streamName, final NotificationOutputTypeGrouping.NotificationOutputType outputType, final boolean leafNodesOnly) { - super(path, streamName, outputType); + super(path, streamName, outputType, controllerContext); setQueryParams(EPOCH, Optional.empty(), Optional.empty(), leafNodesOnly); } diff --git a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/netconf/sal/streams/listeners/NotificationListenerTest.java b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/netconf/sal/streams/listeners/NotificationListenerTest.java index 1edfecb327..2f03868cad 100644 --- a/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/netconf/sal/streams/listeners/NotificationListenerTest.java +++ b/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/netconf/sal/streams/listeners/NotificationListenerTest.java @@ -14,6 +14,7 @@ import static org.mockito.Mockito.when; import com.google.common.base.Preconditions; import com.google.common.collect.Lists; +import java.io.FileNotFoundException; import java.net.URI; import java.util.ArrayList; import java.util.Collection; @@ -22,9 +23,11 @@ import java.util.List; import java.util.Map; import java.util.Optional; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import org.mockito.MockitoAnnotations; import org.opendaylight.controller.md.sal.dom.api.DOMNotification; +import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils; import org.opendaylight.controller.sal.restconf.impl.test.TestUtils; import org.opendaylight.netconf.sal.restconf.impl.ControllerContext; import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType; @@ -48,13 +51,19 @@ import org.opendaylight.yangtools.yang.model.api.SchemaPath; public class NotificationListenerTest { private static final QNameModule MODULE = QNameModule.create(URI.create("notifi:mod"), Revision.of("2016-11-23")); - private SchemaContext schmeaCtx; + private static SchemaContext schemaContext; + + private ControllerContext controllerContext; + + @BeforeClass + public static void staticInit() throws FileNotFoundException { + schemaContext = TestUtils.loadSchemaContext("/notifications"); + } @Before public void init() throws Exception { MockitoAnnotations.initMocks(this); - ControllerContext.getInstance().setGlobalSchema(TestUtils.loadSchemaContext("/notifications")); - this.schmeaCtx = ControllerContext.getInstance().getGlobalSchema(); + controllerContext = TestRestconfUtils.newControllerContext(schemaContext); } @Test @@ -222,10 +231,11 @@ public class NotificationListenerTest { final List paths = new ArrayList<>(); paths.add(schemaPathNotifi); final List listNotifi = - Notificator.createNotificationListener(paths, "stream-name", NotificationOutputType.JSON.toString()); + Notificator.createNotificationListener(paths, "stream-name", NotificationOutputType.JSON.toString(), + controllerContext); final NotificationListenerAdapter notifi = listNotifi.get(0); notifi.setNotification(notificationData); - notifi.setSchemaContext(this.schmeaCtx); + notifi.setSchemaContext(schemaContext); final String result = notifi.prepareJson(); return Preconditions.checkNotNull(result); } diff --git a/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/empty-data.json b/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/empty-data.json index a6ad7f6272..aa7142d4e5 100644 --- a/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/empty-data.json +++ b/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/empty-data.json @@ -1,6 +1,6 @@ { - "cont": { - "lflst1":[], - "lflst2":[45] - } -} \ No newline at end of file + "array-with-null-yang:cont": { + "lflst1":[], + "lflst2":[45] + } +} diff --git a/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/invalid-uri-character-in-value.json b/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/invalid-uri-character-in-value.json index 8281546836..545f99b085 100644 --- a/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/invalid-uri-character-in-value.json +++ b/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/invalid-uri-character-in-value.json @@ -1,6 +1,6 @@ { - "cont":{ - "lf1":"moduleName:value lf2" - } -} \ No newline at end of file + "array-with-null-yang:cont":{ + "lf1":"moduleName:value lf2" + } +} diff --git a/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/multiple-items-in-list.json b/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/multiple-items-in-list.json index 7702b8860f..2131744ad2 100644 --- a/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/multiple-items-in-list.json +++ b/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/multiple-items-in-list.json @@ -1,26 +1,26 @@ { - "multiple-items:lst":[ - { - "lst1": [ - { - "lf11":"lf11_1" - }, - { - "lflst11":[ - 45 - ] - }, - { - "cont11":{ - } - }, - { - "lst11":[ - { - } - ] - } - ] - } - ] -} \ No newline at end of file + "multiple-items-yang:lst":[ + { + "lst1": [ + { + "lf11":"lf11_1" + }, + { + "lflst11":[ + 45 + ] + }, + { + "cont11":{ + } + }, + { + "lst11":[ + { + } + ] + } + ] + } + ] +} diff --git a/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/multiple-leaflist-items.json b/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/multiple-leaflist-items.json index d8f0ce6654..c1f5d1e52d 100644 --- a/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/multiple-leaflist-items.json +++ b/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/multiple-leaflist-items.json @@ -1,5 +1,5 @@ { - "multiple-leaflist:lst": { - "lflst1":[45,55,66] - } -} \ No newline at end of file + "simple-list-yang1:lst": { + "lflst1":[45,55,66] + } +} diff --git a/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/simple-container.json b/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/simple-container.json index 1be414991e..28d244dc3a 100644 --- a/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/simple-container.json +++ b/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/simple-container.json @@ -1,15 +1,15 @@ { - "cont":{ - "cont1":{ - }, - "lst1": [ - { - } - ], - "lflst1":[ - "lflst1_1", - "lflst1_2" - ], - "lf1":"lf1" - } -} \ No newline at end of file + "simple-container-yang:cont":{ + "cont1":{ + }, + "lst1": [ + { + } + ], + "lflst1":[ + "lflst1_1", + "lflst1_2" + ], + "lf1":"lf1" + } +} diff --git a/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/simple-list.json b/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/simple-list.json index d18ecc007e..76a0190e86 100644 --- a/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/simple-list.json +++ b/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/simple-list.json @@ -1,17 +1,17 @@ { - "simple-list:lst":[ - { - "cont1":{ - }, - "lst1": [ - { - } - ], - "lflst1":[ - "lflst1_1", - "lflst1_2" - ], - "lf1":"lf1" - } - ] -} \ No newline at end of file + "simple-list-yang1:lst":[ + { + "cont1":{ + }, + "lst1": [ + { + } + ], + "lflst1":[ + "lflst1_1", + "lflst1_2" + ], + "lf1":"lf1" + } + ] +} diff --git a/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/wrong-top-level1.json b/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/wrong-top-level1.json index 594a802c0e..88baef328f 100644 --- a/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/wrong-top-level1.json +++ b/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/wrong-top-level1.json @@ -1,9 +1,8 @@ { - - "cont":[ - { - }, - { - } - ] -} \ No newline at end of file + "wrong":[ + { + }, + { + } + ] +} diff --git a/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/wrong-top-level2.json b/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/wrong-top-level2.json index af75a47e8b..c567273882 100644 --- a/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/wrong-top-level2.json +++ b/restconf/restconf-nb-bierman02/src/test/resources/json-to-nn/wrong-top-level2.json @@ -1,9 +1,9 @@ { - "lst": { - }, - "lst1":[ - { - } - ] -} \ No newline at end of file + "simple-list-yang1:lst": { + }, + "lst1":[ + { + } + ] +} -- 2.36.6