blob: 7346b0a2aff2ce2f48c80d8e8b7e262562c1a34e [file] [log] [blame]
Thomas Vachuskaf9c84362015-04-15 11:20:45 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onlab.stc;
17
18import org.junit.BeforeClass;
19import org.junit.Test;
20import org.onlab.util.Tools;
21
22import java.io.File;
23import java.io.FileInputStream;
24import java.io.FileNotFoundException;
25import java.io.IOException;
26
27import static com.google.common.io.ByteStreams.toByteArray;
28import static com.google.common.io.Files.write;
29import static org.junit.Assert.assertEquals;
30import static org.junit.Assert.assertSame;
31import static org.onlab.stc.Scenario.loadScenario;
32
33/**
34 * Test of the test scenario compiler.
35 */
36public class CompilerTest {
37
38 static final File TEST_DIR = new File("/tmp/junit-stc");
39
40 @BeforeClass
41 public static void setUpClass() throws IOException {
42 Tools.removeDirectory(TEST_DIR);
43 TEST_DIR.mkdirs();
44 stageTestResource("scenario.xml");
45 stageTestResource("simple-scenario.xml");
46 stageTestResource("one-scenario.xml");
47 stageTestResource("two-scenario.xml");
48
49 System.setProperty("prop.foo", "Foobar");
50 System.setProperty("prop.bar", "Barfoo");
51 System.setProperty("OC1", "1.2.3.1");
52 System.setProperty("OC2", "1.2.3.2");
53 System.setProperty("OC3", "1.2.3.3");
54 }
55
56 public static FileInputStream getStream(String name) throws FileNotFoundException {
57 return new FileInputStream(new File(TEST_DIR, name));
58 }
59
60 private static void stageTestResource(String name) throws IOException {
61 byte[] bytes = toByteArray(CompilerTest.class.getResourceAsStream(name));
62 write(bytes, new File(TEST_DIR, name));
63 }
64
65 @Test
66 public void basics() throws Exception {
67 Scenario scenario = loadScenario(getStream("scenario.xml"));
68 Compiler compiler = new Compiler(scenario);
69 compiler.compile();
70 ProcessFlow flow = compiler.processFlow();
71
72 assertSame("incorrect scenario", scenario, compiler.scenario());
73 assertEquals("incorrect step count", 25, flow.getVertexes().size());
74 assertEquals("incorrect dependency count", 21, flow.getEdges().size());
75 assertEquals("incorrect logDir", "/tmp/junit-stc/foo", compiler.logDir().getPath());
76
77 Step step = compiler.getStep("there");
78 assertEquals("incorrect edge count", 2, flow.getEdgesFrom(step).size());
79 assertEquals("incorrect edge count", 0, flow.getEdgesTo(step).size());
80
81 Step group = compiler.getStep("three");
82 assertEquals("incorrect edge count", 2, flow.getEdgesFrom(group).size());
83 assertEquals("incorrect edge count", 0, flow.getEdgesTo(group).size());
84 }
85
86}