Improve segmented journal actor metrics
[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.api.source.SourceIdentifier;
26 import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
27 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
28 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
29 import org.opendaylight.yangtools.yang.model.spi.source.DelegatedYangTextSource;
30 import scala.concurrent.Await;
31 import scala.concurrent.duration.FiniteDuration;
32
33 @RunWith(MockitoJUnitRunner.StrictStubs.class)
34 public class RemoteYangTextSourceProviderImplTest {
35     private static final SourceIdentifier ID = new SourceIdentifier("Test", "2015-10-30");
36
37     @Mock
38     private SchemaRepository mockedLocalRepository;
39
40     private RemoteYangTextSourceProviderImpl remoteRepository;
41     private final Set<SourceIdentifier> providedSources = Collections.singleton(ID);
42
43     @Before
44     public void setUp() {
45         remoteRepository = new RemoteYangTextSourceProviderImpl(mockedLocalRepository, providedSources);
46     }
47
48     @Test
49     public void testGetExistingYangTextSchemaSource() throws Exception {
50         var schemaSource = new DelegatedYangTextSource(ID, CharSource.wrap("Test source."));
51
52         doReturn(Futures.immediateFuture(schemaSource)).when(mockedLocalRepository)
53             .getSchemaSource(ID, YangTextSource.class);
54
55         var retrievedSourceFuture = remoteRepository.getYangTextSchemaSource(ID);
56         assertTrue(retrievedSourceFuture.isCompleted());
57         var resultSchemaSource = Await.result(retrievedSourceFuture, FiniteDuration.Zero()).getRepresentation();
58         assertEquals(resultSchemaSource.sourceId(), schemaSource.sourceId());
59         assertEquals(resultSchemaSource.read(), schemaSource.read());
60     }
61
62     @Test
63     public void testGetNonExistentYangTextSchemaSource() throws Exception {
64         final var exception = new SchemaSourceException(ID, "Source is not provided");
65
66         doReturn(Futures.immediateFailedFuture(exception)).when(mockedLocalRepository)
67             .getSchemaSource(ID, YangTextSource.class);
68
69         var retrievedSourceFuture = remoteRepository.getYangTextSchemaSource(ID);
70         assertTrue(retrievedSourceFuture.isCompleted());
71
72         final var ex = assertThrows(SchemaSourceException.class,
73             () -> Await.result(retrievedSourceFuture, FiniteDuration.Zero()));
74         assertSame(ex, exception);
75     }
76
77     @Test
78     public void testGetProvidedSources() throws Exception {
79         var remoteProvidedSources = Await.result(remoteRepository.getProvidedSources(), FiniteDuration.Zero());
80         assertEquals(providedSources, remoteProvidedSources);
81     }
82 }