4c9338e1e7b8bc2ef77de8bfa5de71ac5488f991
[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.Matchers.any;
13 import static org.mockito.Matchers.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();
40         yangLibService.setSchemaRepository(schemaRepository);
41
42         final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("name", Revision.of("2016-01-01"));
43
44         final YangTextSchemaSource yangTextSchemaSource = new YangTextSchemaSource(sourceIdentifier) {
45             @Override
46             protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
47                 return null;
48             }
49
50             @Override
51             public InputStream openStream() throws IOException {
52                 return new ByteArrayInputStream(TEST_OUTPUT_STRING.getBytes());
53             }
54         };
55
56         final ListenableFuture<YangTextSchemaSource> sourceFuture = Futures.immediateFuture(yangTextSchemaSource);
57         doReturn(sourceFuture).when(schemaRepository).getSchemaSource(any(SourceIdentifier.class),
58                 eq(YangTextSchemaSource.class));
59
60         final String outputStream = yangLibService.getSchema("name", "2016-01-01");
61         assertEquals(TEST_OUTPUT_STRING, outputStream);
62     }
63
64 }