Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onlab.stc; |
| 17 | |
Brian O'Connor | c6dca1d | 2015-06-25 17:45:47 -0400 | [diff] [blame] | 18 | import com.google.common.io.Files; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 19 | import org.junit.BeforeClass; |
| 20 | import org.junit.Test; |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 21 | |
| 22 | import java.io.File; |
| 23 | import java.io.FileInputStream; |
| 24 | import java.io.FileNotFoundException; |
| 25 | import java.io.IOException; |
| 26 | |
| 27 | import static com.google.common.io.ByteStreams.toByteArray; |
| 28 | import static com.google.common.io.Files.write; |
| 29 | import static org.junit.Assert.assertEquals; |
| 30 | import static org.junit.Assert.assertSame; |
| 31 | import static org.onlab.stc.Scenario.loadScenario; |
| 32 | |
| 33 | /** |
| 34 | * Test of the test scenario compiler. |
| 35 | */ |
| 36 | public class CompilerTest { |
| 37 | |
Brian O'Connor | c6dca1d | 2015-06-25 17:45:47 -0400 | [diff] [blame] | 38 | static final File TEST_DIR = Files.createTempDir(); |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 39 | |
| 40 | @BeforeClass |
| 41 | public static void setUpClass() throws IOException { |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 42 | 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 Vachuska | 8189a74 | 2015-05-29 10:02:52 -0700 | [diff] [blame] | 49 | 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'Connor | c6dca1d | 2015-06-25 17:45:47 -0400 | [diff] [blame] | 52 | System.setProperty("test.dir", TEST_DIR.getAbsolutePath()); |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 53 | } |
| 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 Vachuska | 177ece6 | 2015-06-26 00:18:21 -0700 | [diff] [blame^] | 72 | assertEquals("incorrect step count", 24, flow.getVertexes().size()); |
| 73 | assertEquals("incorrect dependency count", 17, flow.getEdges().size()); |
Brian O'Connor | c6dca1d | 2015-06-25 17:45:47 -0400 | [diff] [blame] | 74 | assertEquals("incorrect logDir", |
| 75 | TEST_DIR.getAbsolutePath() + "/foo", compiler.logDir().getPath()); |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 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 | } |