85fe58584bfd3b3fc624329d7aefb489b608eebc
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / cluster / schema / provider / impl / RemoteYangTextSourceProviderImplTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.schema.provider.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertSame;
12 import static org.junit.Assert.assertThrows;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.Mockito.doReturn;
15
16 import com.google.common.io.CharSource;
17 import com.google.common.util.concurrent.Futures;
18 import java.util.Collections;
19 import java.util.Set;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.Mock;
24 import org.mockito.junit.MockitoJUnitRunner;
25 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
26 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
27 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
28 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
29 import scala.concurrent.Await;
30 import scala.concurrent.duration.FiniteDuration;
31
32 @RunWith(MockitoJUnitRunner.StrictStubs.class)
33 public class RemoteYangTextSourceProviderImplTest {
34     private static final SourceIdentifier ID = new SourceIdentifier("Test", "2015-10-30");
35
36     @Mock
37     private SchemaRepository mockedLocalRepository;
38
39     private RemoteYangTextSourceProviderImpl remoteRepository;
40     private final Set<SourceIdentifier> providedSources = Collections.singleton(ID);
41
42     @Before
43     public void setUp() {
44         remoteRepository = new RemoteYangTextSourceProviderImpl(mockedLocalRepository, providedSources);
45     }
46
47     @Test
48     public void testGetExistingYangTextSchemaSource() throws Exception {
49         var schemaSource = YangTextSchemaSource.delegateForCharSource(ID, CharSource.wrap("Test source."));
50
51         doReturn(Futures.immediateFuture(schemaSource)).when(mockedLocalRepository)
52             .getSchemaSource(ID, YangTextSchemaSource.class);
53
54         var retrievedSourceFuture = remoteRepository.getYangTextSchemaSource(ID);
55         assertTrue(retrievedSourceFuture.isCompleted());
56         var resultSchemaSource = Await.result(retrievedSourceFuture, FiniteDuration.Zero()).getRepresentation();
57         assertEquals(resultSchemaSource.getIdentifier(), schemaSource.getIdentifier());
58         assertEquals(resultSchemaSource.read(), schemaSource.read());
59     }
60
61     @Test
62     public void testGetNonExistentYangTextSchemaSource() throws Exception {
63         final var exception = new SchemaSourceException("Source is not provided");
64
65         doReturn(Futures.immediateFailedFuture(exception)).when(mockedLocalRepository)
66             .getSchemaSource(ID, YangTextSchemaSource.class);
67
68         var retrievedSourceFuture = remoteRepository.getYangTextSchemaSource(ID);
69         assertTrue(retrievedSourceFuture.isCompleted());
70
71         final var ex = assertThrows(SchemaSourceException.class,
72             () -> Await.result(retrievedSourceFuture, FiniteDuration.Zero()));
73         assertSame(ex, exception);
74     }
75
76     @Test
77     public void testGetProvidedSources() throws Exception {
78         var remoteProvidedSources = Await.result(remoteRepository.getProvidedSources(), FiniteDuration.Zero());
79         assertEquals(providedSources, remoteProvidedSources);
80     }
81 }