5ce2d9ba10a42dae65074d1f5c369859d451a234
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / cluster / schema / provider / impl / RemoteSchemaProviderTest.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 akka.dispatch.ExecutionContexts;
16 import akka.dispatch.Futures;
17 import com.google.common.base.Optional;
18 import com.google.common.io.ByteSource;
19 import com.google.common.util.concurrent.CheckedFuture;
20 import com.google.common.util.concurrent.MoreExecutors;
21 import java.io.IOException;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.Mockito;
25 import org.opendaylight.controller.cluster.schema.provider.RemoteYangTextSourceProvider;
26 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
27 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
28 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
29 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
30
31 public class RemoteSchemaProviderTest {
32
33     private static final SourceIdentifier ID = RevisionSourceIdentifier.create("Test", Optional.of("2015-10-30"));
34
35     private RemoteSchemaProvider remoteSchemaProvider;
36     private RemoteYangTextSourceProvider mockedRemoteSchemaRepository;
37
38     @Before
39     public void setUp() {
40         mockedRemoteSchemaRepository = Mockito.mock(RemoteYangTextSourceProvider.class);
41         ExecutionContexts.fromExecutorService(MoreExecutors.newDirectExecutorService());
42         remoteSchemaProvider = new RemoteSchemaProvider(mockedRemoteSchemaRepository,
43                 ExecutionContexts.fromExecutorService(MoreExecutors.newDirectExecutorService()));
44     }
45
46     @Test
47     public void getExistingYangTextSchemaSource() throws IOException, SchemaSourceException {
48         String source = "Test";
49         YangTextSchemaSource schemaSource = YangTextSchemaSource.delegateForByteSource(
50                 ID, ByteSource.wrap(source.getBytes()));
51         YangTextSchemaSourceSerializationProxy sourceProxy = new YangTextSchemaSourceSerializationProxy(schemaSource);
52         Mockito.when(mockedRemoteSchemaRepository.getYangTextSchemaSource(ID))
53             .thenReturn(Futures.successful(sourceProxy));
54
55         YangTextSchemaSource providedSource = remoteSchemaProvider.getSource(ID).checkedGet();
56         assertEquals(providedSource.getIdentifier(), ID);
57         assertArrayEquals(providedSource.read(), schemaSource.read());
58     }
59
60     @Test(expected = SchemaSourceException.class)
61     public void getNonExistingSchemaSource() throws Exception {
62         Futures.failed(new Exception("halo"));
63
64         Mockito.when(mockedRemoteSchemaRepository.getYangTextSchemaSource(ID)).thenReturn(
65                 Futures.failed(
66                         new SchemaSourceException("Source not provided")));
67
68         CheckedFuture<?, ?> sourceFuture = remoteSchemaProvider.getSource(ID);
69         assertTrue(sourceFuture.isDone());
70         sourceFuture.checkedGet();
71     }
72 }

©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.