Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [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.onosproject.common.app; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.io.ByteStreams; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 20 | import org.junit.After; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 23 | import org.onlab.util.Tools; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 24 | import org.onosproject.app.ApplicationDescription; |
| 25 | import org.onosproject.app.ApplicationException; |
| 26 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 27 | import java.io.File; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 28 | import java.io.IOException; |
| 29 | import java.io.InputStream; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 30 | import java.util.Random; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 31 | import java.util.Set; |
| 32 | |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 33 | import static org.junit.Assert.*; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 34 | import static org.onosproject.app.DefaultApplicationDescriptionTest.*; |
| 35 | |
Thomas Vachuska | cc5d038 | 2015-06-11 17:25:36 -0700 | [diff] [blame^] | 36 | /** |
| 37 | * Suite of tests for the application archive utility. |
| 38 | */ |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 39 | public class ApplicationArchiveTest { |
| 40 | |
Thomas Vachuska | 5d4a58d | 2015-04-28 11:40:05 -0700 | [diff] [blame] | 41 | static final String ROOT = "/tmp/app-junit/"; |
| 42 | static final String STORE = ROOT + new Random().nextInt(1000) + "/foo"; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 43 | |
| 44 | private ApplicationArchive aar = new ApplicationArchive(); |
| 45 | |
| 46 | @Before |
| 47 | public void setUp() { |
Thomas Vachuska | 5d4a58d | 2015-04-28 11:40:05 -0700 | [diff] [blame] | 48 | aar.setRootPath(STORE); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | @After |
| 52 | public void tearDown() throws IOException { |
Thomas Vachuska | 5d4a58d | 2015-04-28 11:40:05 -0700 | [diff] [blame] | 53 | if (new File(ROOT).exists()) { |
| 54 | Tools.removeDirectory(ROOT); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 55 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | private void validate(ApplicationDescription app) { |
| 59 | assertEquals("incorrect name", APP_NAME, app.name()); |
| 60 | assertEquals("incorrect version", VER, app.version()); |
| 61 | assertEquals("incorrect origin", ORIGIN, app.origin()); |
Changhoon Yoon | bdeb88a | 2015-05-12 20:35:31 +0900 | [diff] [blame] | 62 | assertEquals("incorrect role", ROLE, app.role()); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 63 | |
| 64 | assertEquals("incorrect description", DESC, app.description()); |
| 65 | assertEquals("incorrect features URI", FURL, app.featuresRepo().get()); |
| 66 | assertEquals("incorrect permissions", PERMS, app.permissions()); |
| 67 | assertEquals("incorrect features", FEATURES, app.features()); |
| 68 | } |
| 69 | |
| 70 | @Test |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 71 | public void saveZippedApp() throws IOException { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 72 | InputStream stream = getClass().getResourceAsStream("app.zip"); |
| 73 | ApplicationDescription app = aar.saveApplication(stream); |
| 74 | validate(app); |
| 75 | } |
| 76 | |
| 77 | @Test |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 78 | public void savePlainApp() throws IOException { |
| 79 | InputStream stream = getClass().getResourceAsStream("app.xml"); |
| 80 | ApplicationDescription app = aar.saveApplication(stream); |
| 81 | validate(app); |
| 82 | } |
| 83 | |
| 84 | @Test |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 85 | public void loadApp() throws IOException { |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 86 | saveZippedApp(); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 87 | ApplicationDescription app = aar.getApplicationDescription(APP_NAME); |
| 88 | validate(app); |
| 89 | } |
| 90 | |
| 91 | @Test |
| 92 | public void getAppNames() throws IOException { |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 93 | saveZippedApp(); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 94 | Set<String> names = aar.getApplicationNames(); |
| 95 | assertEquals("incorrect names", ImmutableSet.of(APP_NAME), names); |
| 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void purgeApp() throws IOException { |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 100 | saveZippedApp(); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 101 | aar.purgeApplication(APP_NAME); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 102 | assertEquals("incorrect names", ImmutableSet.<String>of(), |
| 103 | aar.getApplicationNames()); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Test |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 107 | public void getAppZipStream() throws IOException { |
| 108 | saveZippedApp(); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 109 | InputStream stream = aar.getApplicationInputStream(APP_NAME); |
| 110 | byte[] orig = ByteStreams.toByteArray(getClass().getResourceAsStream("app.zip")); |
| 111 | byte[] loaded = ByteStreams.toByteArray(stream); |
| 112 | assertArrayEquals("incorrect stream", orig, loaded); |
| 113 | } |
| 114 | |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 115 | @Test |
| 116 | public void getAppXmlStream() throws IOException { |
| 117 | savePlainApp(); |
| 118 | InputStream stream = aar.getApplicationInputStream(APP_NAME); |
| 119 | byte[] orig = ByteStreams.toByteArray(getClass().getResourceAsStream("app.xml")); |
| 120 | byte[] loaded = ByteStreams.toByteArray(stream); |
| 121 | assertArrayEquals("incorrect stream", orig, loaded); |
| 122 | } |
| 123 | |
| 124 | @Test |
| 125 | public void active() throws IOException { |
| 126 | savePlainApp(); |
| 127 | assertFalse("should not be active", aar.isActive(APP_NAME)); |
| 128 | aar.setActive(APP_NAME); |
| 129 | assertTrue("should not be active", aar.isActive(APP_NAME)); |
| 130 | aar.clearActive(APP_NAME); |
| 131 | assertFalse("should not be active", aar.isActive(APP_NAME)); |
| 132 | } |
| 133 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 134 | @Test(expected = ApplicationException.class) |
| 135 | public void getBadAppDesc() throws IOException { |
| 136 | aar.getApplicationDescription("org.foo.BAD"); |
| 137 | } |
| 138 | |
| 139 | @Test(expected = ApplicationException.class) |
| 140 | public void getBadAppStream() throws IOException { |
| 141 | aar.getApplicationInputStream("org.foo.BAD"); |
| 142 | } |
| 143 | |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 144 | @Test(expected = ApplicationException.class) |
| 145 | public void setBadActive() throws IOException { |
| 146 | aar.setActive("org.foo.BAD"); |
| 147 | } |
| 148 | |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 149 | @Test // (expected = ApplicationException.class) |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 150 | public void purgeBadApp() throws IOException { |
| 151 | aar.purgeApplication("org.foo.BAD"); |
| 152 | } |
| 153 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 154 | } |