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