blob: 35b200bfc93fbfd1bf2ea226a0b530948c9b4adb [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
Brian O'Connorc6dca1d2015-06-25 17:45:47 -040018import com.google.common.io.Files;
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070019import org.junit.BeforeClass;
20import org.junit.Test;
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070021
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
Brian O'Connorc6dca1d2015-06-25 17:45:47 -040038 static final File TEST_DIR = Files.createTempDir();
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070039
40 @BeforeClass
41 public static void setUpClass() throws IOException {
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070042 stageTestResource("scenario.xml");
43 stageTestResource("simple-scenario.xml");
44 stageTestResource("one-scenario.xml");
45 stageTestResource("two-scenario.xml");
46
47 System.setProperty("prop.foo", "Foobar");
48 System.setProperty("prop.bar", "Barfoo");
Thomas Vachuska8189a742015-05-29 10:02:52 -070049 System.setProperty("TOC1", "1.2.3.1");
50 System.setProperty("TOC2", "1.2.3.2");
51 System.setProperty("TOC3", "1.2.3.3");
Brian O'Connorc6dca1d2015-06-25 17:45:47 -040052 System.setProperty("test.dir", TEST_DIR.getAbsolutePath());
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070053 }
54
55 public static FileInputStream getStream(String name) throws FileNotFoundException {
56 return new FileInputStream(new File(TEST_DIR, name));
57 }
58
59 private static void stageTestResource(String name) throws IOException {
60 byte[] bytes = toByteArray(CompilerTest.class.getResourceAsStream(name));
61 write(bytes, new File(TEST_DIR, name));
62 }
63
64 @Test
65 public void basics() throws Exception {
66 Scenario scenario = loadScenario(getStream("scenario.xml"));
67 Compiler compiler = new Compiler(scenario);
68 compiler.compile();
69 ProcessFlow flow = compiler.processFlow();
70
71 assertSame("incorrect scenario", scenario, compiler.scenario());
Thomas Vachuska177ece62015-06-26 00:18:21 -070072 assertEquals("incorrect step count", 24, flow.getVertexes().size());
73 assertEquals("incorrect dependency count", 17, flow.getEdges().size());
Brian O'Connorc6dca1d2015-06-25 17:45:47 -040074 assertEquals("incorrect logDir",
75 TEST_DIR.getAbsolutePath() + "/foo", compiler.logDir().getPath());
Thomas Vachuskaf9c84362015-04-15 11:20:45 -070076
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}