*/
package org.opendaylight.netconf.client.mdsal;
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.SettableFuture;
-import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import org.junit.BeforeClass;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
import org.opendaylight.mdsal.dom.api.DOMNotification;
import org.opendaylight.netconf.api.CapabilityURN;
import org.opendaylight.netconf.api.NetconfMessage;
import org.opendaylight.netconf.api.xml.XmlUtil;
+import org.opendaylight.netconf.client.mdsal.NetconfDevice.EmptySchemaContextException;
import org.opendaylight.netconf.client.mdsal.api.NetconfDeviceSchemasResolver;
import org.opendaylight.netconf.client.mdsal.api.NetconfSessionPreferences;
import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceHandler;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
-import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
-import org.xml.sax.SAXException;
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class NetconfDeviceTest extends AbstractTestModelTest {
- private static final NetconfMessage NOTIFICATION;
-
- static {
- try {
- NOTIFICATION = new NetconfMessage(XmlUtil
- .readXmlToDocument(NetconfDeviceTest.class.getResourceAsStream("/notification-payload.xml")));
- } catch (SAXException | IOException e) {
- throw new ExceptionInInitializerError(e);
- }
- }
-
public static final String TEST_NAMESPACE = "test:namespace";
public static final String TEST_MODULE = "test-module";
public static final String TEST_REVISION = "2013-07-22";
private static final NetconfDeviceSchemasResolver STATE_SCHEMAS_RESOLVER =
(deviceRpc, remoteSessionCapabilities, id, schemaContext) -> NetconfStateSchemas.EMPTY;
+ private static NetconfMessage NOTIFICATION;
+
+ @Mock
+ private SchemaSourceRegistry schemaRegistry;
+
+ @BeforeClass
+ public static final void setupNotification() throws Exception {
+ NOTIFICATION = new NetconfMessage(XmlUtil.readXmlToDocument(
+ NetconfDeviceTest.class.getResourceAsStream("/notification-payload.xml")));
+ }
@Test
public void testNetconfDeviceFlawedModelFailedResolution() throws Exception {
return new NetconfStateSchemas(Sets.newHashSet(source1, source2));
};
+ doReturn(mock(SchemaSourceRegistration.class)).when(schemaRegistry).registerSchemaSource(any(), any());
final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice
- .SchemaResourcesDTO(getSchemaRegistry(), schemaRepository, schemaFactory, stateSchemasResolver);
+ .SchemaResourcesDTO(schemaRegistry, schemaRepository, schemaFactory, stateSchemasResolver);
final NetconfDevice device = new NetconfDeviceBuilder()
.setReconnectOnSchemasChange(true)
.when(schemaFactory).createEffectiveModelContext(anyCollection());
final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice
- .SchemaResourcesDTO(getSchemaRegistry(), schemaRepository, schemaFactory, STATE_SCHEMAS_RESOLVER);
+ .SchemaResourcesDTO(schemaRegistry, schemaRepository, schemaFactory, STATE_SCHEMAS_RESOLVER);
final NetconfDevice device = new NetconfDeviceBuilder()
.setReconnectOnSchemasChange(true)
.setSchemaResourcesDTO(schemaResourcesDTO)
device.onRemoteSessionUp(sessionCaps, listener);
verify(facade, timeout(5000)).onDeviceDisconnected();
+ final var captor = ArgumentCaptor.forClass(Throwable.class);
+ verify(facade, timeout(5000)).onDeviceFailed(captor.capture());
+ assertThat(captor.getValue(), instanceOf(EmptySchemaContextException.class));
+
verify(listener, timeout(5000)).close();
verify(schemaFactory, times(1)).createEffectiveModelContext(anyCollection());
}
return new NetconfStateSchemas(Sets.newHashSet(source1, source2));
};
+ doReturn(mock(SchemaSourceRegistration.class)).when(schemaRegistry).registerSchemaSource(any(), any());
final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice
- .SchemaResourcesDTO(getSchemaRegistry(), schemaRepository, schemaFactory, stateSchemasResolver);
+ .SchemaResourcesDTO(schemaRegistry, schemaRepository, schemaFactory, stateSchemasResolver);
final NetconfDevice device = new NetconfDeviceBuilder()
.setReconnectOnSchemasChange(true)
verify(schemaFactory, times(1)).createEffectiveModelContext(anyCollection());
}
- private static SchemaSourceRegistry getSchemaRegistry() {
- final SchemaSourceRegistry mock = mock(SchemaSourceRegistry.class);
- final SchemaSourceRegistration<?> mockReg = mock(SchemaSourceRegistration.class);
- doNothing().when(mockReg).close();
- doReturn(mockReg).when(mock).registerSchemaSource(
- any(org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider.class),
- any(PotentialSchemaSource.class));
- return mock;
- }
-
private static SchemaRepository getSchemaRepository() {
final SchemaRepository mock = mock(SchemaRepository.class);
final YangTextSchemaSource mockRep = mock(YangTextSchemaSource.class);
final EffectiveModelContextFactory schemaContextProviderFactory = mock(EffectiveModelContextFactory.class);
final SettableFuture<SchemaContext> schemaFuture = SettableFuture.create();
doReturn(schemaFuture).when(schemaContextProviderFactory).createEffectiveModelContext(anyCollection());
- final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO =
- new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), getSchemaRepository(),
- schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
+ final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(schemaRegistry,
+ getSchemaRepository(), schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
final NetconfDevice device = new NetconfDeviceBuilder()
.setReconnectOnSchemasChange(true)
.setSchemaResourcesDTO(schemaResourcesDTO)
final EffectiveModelContextFactory schemaContextProviderFactory = getSchemaFactory();
final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(
- getSchemaRegistry(), getSchemaRepository(), schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
+ schemaRegistry, getSchemaRepository(), schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
final NetconfDevice device = new NetconfDeviceBuilder()
.setReconnectOnSchemasChange(true)
.setSchemaResourcesDTO(schemaResourcesDTO)
final EffectiveModelContextFactory schemaContextProviderFactory = mock(EffectiveModelContextFactory.class);
final SettableFuture<SchemaContext> schemaFuture = SettableFuture.create();
doReturn(schemaFuture).when(schemaContextProviderFactory).createEffectiveModelContext(anyCollection());
- final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO
- = new NetconfDevice.SchemaResourcesDTO(getSchemaRegistry(), getSchemaRepository(),
- schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
+ final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(schemaRegistry,
+ getSchemaRepository(), schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
final NetconfDevice device = new NetconfDeviceBuilder()
.setReconnectOnSchemasChange(true)
.setSchemaResourcesDTO(schemaResourcesDTO)
final EffectiveModelContextFactory schemaContextProviderFactory = getSchemaFactory();
- final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(
- getSchemaRegistry(), getSchemaRepository(), schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
+ final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(schemaRegistry,
+ getSchemaRepository(), schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
final NetconfDevice device = new NetconfDeviceBuilder()
.setReconnectOnSchemasChange(true)
.setSchemaResourcesDTO(schemaResourcesDTO)
final NetconfDeviceCommunicator listener = getListener();
final EffectiveModelContextFactory schemaContextProviderFactory = getSchemaFactory();
- final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(
- getSchemaRegistry(), getSchemaRepository(), schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
+ final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(schemaRegistry,
+ getSchemaRepository(), schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
final NetconfDevice device = new NetconfDeviceBuilder()
.setSchemaResourcesDTO(schemaResourcesDTO)
.setGlobalProcessingExecutor(MoreExecutors.directExecutor())
final NetconfDeviceCommunicator listener = getListener();
final EffectiveModelContextFactory schemaContextProviderFactory = getSchemaFactory();
- final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(
- getSchemaRegistry(), getSchemaRepository(), schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
+ final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(schemaRegistry,
+ getSchemaRepository(), schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
final NetconfDevice device = new NetconfDeviceBuilder()
.setSchemaResourcesDTO(schemaResourcesDTO)
.setGlobalProcessingExecutor(MoreExecutors.directExecutor())
final NetconfDeviceCommunicator listener = getListener();
final EffectiveModelContextFactory schemaContextProviderFactory = getSchemaFactory();
- final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(
- getSchemaRegistry(), getSchemaRepository(), schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
+ final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = new NetconfDevice.SchemaResourcesDTO(schemaRegistry,
+ getSchemaRepository(), schemaContextProviderFactory, STATE_SCHEMAS_RESOLVER);
final NetconfDevice device = new NetconfDeviceBuilder()
.setSchemaResourcesDTO(schemaResourcesDTO)
.setGlobalProcessingExecutor(MoreExecutors.directExecutor())
}
private static EffectiveModelContextFactory getSchemaFactory() throws Exception {
- final EffectiveModelContextFactory schemaFactory = mockClass(EffectiveModelContextFactory.class);
+ final EffectiveModelContextFactory schemaFactory = mock(EffectiveModelContextFactory.class);
doReturn(Futures.immediateFuture(SCHEMA_CONTEXT))
.when(schemaFactory).createEffectiveModelContext(anyCollection());
return schemaFactory;
private static <T extends AutoCloseable> T mockCloseableClass(final Class<T> remoteDeviceHandlerClass)
throws Exception {
- final T mock = mockClass(remoteDeviceHandlerClass);
- doNothing().when(mock).close();
- return mock;
- }
-
- private static <T> T mockClass(final Class<T> remoteDeviceHandlerClass) {
final T mock = mock(remoteDeviceHandlerClass);
- doReturn(remoteDeviceHandlerClass.getSimpleName()).when(mock).toString();
+ doNothing().when(mock).close();
return mock;
}