Do not use RevisionSourceIdentifier
[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 package org.opendaylight.controller.cluster.schema.provider.impl;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertSame;
13 import static org.junit.Assert.assertThrows;
14 import static org.junit.Assert.assertTrue;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17
18 import akka.dispatch.ExecutionContexts;
19 import akka.dispatch.Futures;
20 import com.google.common.io.CharSource;
21 import com.google.common.util.concurrent.ListenableFuture;
22 import com.google.common.util.concurrent.MoreExecutors;
23 import java.io.IOException;
24 import java.nio.charset.StandardCharsets;
25 import java.util.concurrent.ExecutionException;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.opendaylight.controller.cluster.schema.provider.RemoteYangTextSourceProvider;
29 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
30 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
31 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
32
33 public class RemoteSchemaProviderTest {
34     private static final SourceIdentifier ID = new SourceIdentifier("Test", "2015-10-30");
35
36     private RemoteSchemaProvider remoteSchemaProvider;
37     private RemoteYangTextSourceProvider mockedRemoteSchemaRepository;
38
39     @Before
40     public void setUp() {
41         mockedRemoteSchemaRepository = mock(RemoteYangTextSourceProvider.class);
42         remoteSchemaProvider = new RemoteSchemaProvider(mockedRemoteSchemaRepository,
43                 ExecutionContexts.fromExecutor(MoreExecutors.directExecutor()));
44     }
45
46     @Test
47     public void getExistingYangTextSchemaSource() throws IOException, InterruptedException, ExecutionException {
48         YangTextSchemaSource schemaSource = YangTextSchemaSource.delegateForByteSource(ID,
49             CharSource.wrap("Test").asByteSource(StandardCharsets.UTF_8));
50         doReturn(Futures.successful(new YangTextSchemaSourceSerializationProxy(schemaSource)))
51             .when(mockedRemoteSchemaRepository).getYangTextSchemaSource(ID);
52
53         YangTextSchemaSource providedSource = remoteSchemaProvider.getSource(ID).get();
54         assertEquals(ID, providedSource.getIdentifier());
55         assertArrayEquals(schemaSource.read(), providedSource.read());
56     }
57
58     @Test
59     public void getNonExistingSchemaSource() throws InterruptedException {
60         final var exception = new SchemaSourceException("Source not provided");
61         doReturn(Futures.failed(exception)).when(mockedRemoteSchemaRepository).getYangTextSchemaSource(ID);
62
63         ListenableFuture<YangTextSchemaSource> sourceFuture = remoteSchemaProvider.getSource(ID);
64         assertTrue(sourceFuture.isDone());
65
66         final var cause = assertThrows(ExecutionException.class, sourceFuture::get).getCause();
67         assertSame(exception, cause);
68     }
69 }