d8cd3f2bfca0343c852ab0dc01cfc0aa183b7d0f
[netconf.git] / netconf / yanglib / src / test / java / org / opendaylight / yanglib / impl / YangLibServiceImplTest.java
1 /*
2  * Copyright (c) 2016 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.yanglib.impl;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.ArgumentMatchers.eq;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16
17 import com.google.common.base.MoreObjects.ToStringHelper;
18 import com.google.common.util.concurrent.Futures;
19 import com.google.common.util.concurrent.ListenableFuture;
20 import java.io.ByteArrayInputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import org.junit.Test;
24 import org.opendaylight.yangtools.yang.common.Revision;
25 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
26 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
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 YangLibServiceImplTest {
32
33     private static final String TEST_OUTPUT_STRING = "hello world";
34
35     @Test
36     public void testSchema() throws SchemaSourceException {
37
38         final SchemaRepository schemaRepository = mock(SchemaRepository.class);
39         final YangLibServiceImpl yangLibService = new YangLibServiceImpl(schemaRepository);
40
41         final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("name", Revision.of("2016-01-01"));
42
43         final YangTextSchemaSource yangTextSchemaSource = new YangTextSchemaSource(sourceIdentifier) {
44             @Override
45             protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
46                 return null;
47             }
48
49             @Override
50             public InputStream openStream() throws IOException {
51                 return new ByteArrayInputStream(TEST_OUTPUT_STRING.getBytes());
52             }
53         };
54
55         final ListenableFuture<YangTextSchemaSource> sourceFuture = Futures.immediateFuture(yangTextSchemaSource);
56         doReturn(sourceFuture).when(schemaRepository).getSchemaSource(any(SourceIdentifier.class),
57                 eq(YangTextSchemaSource.class));
58
59         final String outputStream = yangLibService.getSchema("name", "2016-01-01");
60         assertEquals(TEST_OUTPUT_STRING, outputStream);
61     }
62
63 }