Bug 8002 - Fixing and cleanup of templates for binding2
[mdsal.git] / binding2 / mdsal-binding2-generator-impl / src / test / java / org / opendaylight / mdsal / binding / javav2 / generator / impl / YangTemplateTest.java
1 /*
2  * Copyright (c) 2017 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.mdsal.binding.javav2.generator.impl;
10
11 import com.google.common.annotations.Beta;
12 import java.io.BufferedReader;
13 import java.io.FileNotFoundException;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.io.InputStreamReader;
17 import java.net.URISyntaxException;
18 import java.util.Set;
19 import org.junit.Assert;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.mdsal.binding.javav2.generator.impl.txt.yangTemplateForModule;
23 import org.opendaylight.mdsal.binding.javav2.generator.util.YangSnippetCleaner;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
26 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
27
28 @Beta
29 public class YangTemplateTest {
30
31     private Set<Module> modules;
32
33     @Before
34     public void setup() throws URISyntaxException, ReactorException, FileNotFoundException {
35         this.modules = YangParserTestUtils.parseYangSources("/yang-template").getModules();
36     }
37
38     @Test
39     public void printYangSnippetForModule() throws Exception{
40         for (final Module module : this.modules) {
41             String originalFile;
42             try {
43                 originalFile = readFile(this.getClass().getResourceAsStream(
44                         new StringBuilder("/yang-template/").append(module.getName()).append(".yang").toString()));
45             } catch (final Exception e) {
46                 throw e;
47             }
48             final String moduleBody = yangTemplateForModule.render(module).body().trim();
49             final String cleanedModuleBody = YangSnippetCleaner.clean(moduleBody);
50             Assert.assertEquals(originalFile, cleanedModuleBody);
51         }
52     }
53
54     private String readFile(final InputStream inputStream) throws IOException {
55         final BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
56         try {
57             final StringBuilder sb = new StringBuilder();
58             String line = br.readLine();
59
60             while (line != null) {
61                 sb.append(line);
62                 sb.append("\n");
63                 line = br.readLine();
64             }
65             return sb.toString();
66         } finally {
67             br.close();
68         }
69     }
70 }