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 | |
| 18 | import org.junit.BeforeClass; |
| 19 | import org.junit.Test; |
| 20 | import org.onlab.util.Tools; |
| 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 | |
| 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 | } |