Fix compilation error
[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
9 package org.opendaylight.controller.cluster.schema.provider.impl;
10
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertTrue;
14
15 import com.google.common.base.Optional;
16 import com.google.common.io.ByteSource;
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.mockito.Mockito;
23 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
24 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
25 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
26 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
27 import scala.concurrent.Await;
28 import scala.concurrent.Future;
29 import scala.concurrent.duration.Duration;
30
31 public class RemoteYangTextSourceProviderImplTest {
32
33     private static final SourceIdentifier ID = SourceIdentifier.create("Test", Optional.of("2015-10-30"));
34
35     private RemoteYangTextSourceProviderImpl remoteRepository;
36     private SchemaRepository mockedLocalRepository;
37     private Set<SourceIdentifier> providedSources = Collections.singleton(ID);
38
39     @Before
40     public void setUp() {
41         mockedLocalRepository = Mockito.mock(SchemaRepository.class);
42
43         remoteRepository = new RemoteYangTextSourceProviderImpl(mockedLocalRepository, providedSources);
44     }
45
46     @Test
47     public void testGetExistingYangTextSchemaSource() throws Exception {
48         String source = "Test source.";
49         YangTextSchemaSource schemaSource = YangTextSchemaSource.delegateForByteSource(ID, ByteSource.wrap(source.getBytes()));
50         Mockito.when(mockedLocalRepository.getSchemaSource(ID, YangTextSchemaSource.class)).thenReturn(
51                 Futures.<YangTextSchemaSource, SchemaSourceException>immediateCheckedFuture(schemaSource));
52
53         Future<YangTextSchemaSourceSerializationProxy> retrievedSourceFuture = remoteRepository.getYangTextSchemaSource(ID);
54         assertTrue(retrievedSourceFuture.isCompleted());
55         YangTextSchemaSource resultSchemaSource = Await.result(retrievedSourceFuture, Duration.Zero()).getRepresentation();
56         assertEquals(resultSchemaSource.getIdentifier(), schemaSource.getIdentifier());
57         assertArrayEquals(resultSchemaSource.read(), schemaSource.read());
58     }
59
60     @Test(expected = SchemaSourceException.class)
61     public void testGetNonExistentYangTextSchemaSource() throws Exception {
62         Mockito.when(mockedLocalRepository.getSchemaSource(ID, YangTextSchemaSource.class)).thenReturn(
63                 Futures.<YangTextSchemaSource, SchemaSourceException>immediateFailedCheckedFuture(
64                         new SchemaSourceException("Source is not provided")));
65
66
67         Future<YangTextSchemaSourceSerializationProxy> retrievedSourceFuture = remoteRepository.getYangTextSchemaSource(ID);
68         assertTrue(retrievedSourceFuture.isCompleted());
69         Await.result(retrievedSourceFuture, Duration.Zero());
70     }
71
72     @Test
73     public void testGetProvidedSources() throws Exception {
74         Set<SourceIdentifier> remoteProvidedSources = Await.result(remoteRepository.getProvidedSources(), Duration.Zero());
75         assertEquals(providedSources, remoteProvidedSources);
76     }
77
78 }