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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.