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 | 8044c09 | 2015-08-04 11:06:41 -0700 | [diff] [blame] | 20 | import com.google.common.io.Files; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 21 | import org.junit.After; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 22 | import org.junit.Before; |
Thomas Vachuska | e965b3d | 2016-03-03 11:42:48 -0800 | [diff] [blame^] | 23 | import org.junit.Ignore; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 24 | import org.junit.Test; |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 25 | import org.onlab.util.Tools; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 26 | import org.onosproject.app.ApplicationDescription; |
| 27 | import org.onosproject.app.ApplicationException; |
| 28 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 29 | import java.io.File; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 30 | import java.io.IOException; |
| 31 | import java.io.InputStream; |
| 32 | import java.util.Set; |
| 33 | |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 34 | import static org.junit.Assert.*; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 35 | import static org.onosproject.app.DefaultApplicationDescriptionTest.*; |
| 36 | |
Thomas Vachuska | ad35c34 | 2015-06-11 17:25:36 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Suite of tests for the application archive utility. |
| 39 | */ |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 40 | public class ApplicationArchiveTest { |
| 41 | |
Thomas Vachuska | 8044c09 | 2015-08-04 11:06:41 -0700 | [diff] [blame] | 42 | static final File STORE = Files.createTempDir(); |
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 | 8044c09 | 2015-08-04 11:06:41 -0700 | [diff] [blame] | 48 | aar.setRootPath(STORE.getAbsolutePath()); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | @After |
| 52 | public void tearDown() throws IOException { |
Thomas Vachuska | 8044c09 | 2015-08-04 11:06:41 -0700 | [diff] [blame] | 53 | if (STORE.exists()) { |
| 54 | Tools.removeDirectory(STORE); |
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 | |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 64 | assertEquals("incorrect category", CATEGORY, app.category()); |
| 65 | assertEquals("incorrect url", URL, app.url()); |
| 66 | assertEquals("incorrect readme", README, app.readme()); |
| 67 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 68 | assertEquals("incorrect description", DESC, app.description()); |
| 69 | assertEquals("incorrect features URI", FURL, app.featuresRepo().get()); |
| 70 | assertEquals("incorrect permissions", PERMS, app.permissions()); |
| 71 | assertEquals("incorrect features", FEATURES, app.features()); |
| 72 | } |
| 73 | |
| 74 | @Test |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 75 | public void saveZippedApp() throws IOException { |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 76 | InputStream stream = getClass().getResourceAsStream("app.zip"); |
| 77 | ApplicationDescription app = aar.saveApplication(stream); |
| 78 | validate(app); |
Kenji HIKICHI | 5448d46 | 2015-07-03 18:24:49 +0900 | [diff] [blame] | 79 | stream.close(); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Test |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 83 | public void savePlainApp() throws IOException { |
| 84 | InputStream stream = getClass().getResourceAsStream("app.xml"); |
| 85 | ApplicationDescription app = aar.saveApplication(stream); |
| 86 | validate(app); |
Kenji HIKICHI | 5448d46 | 2015-07-03 18:24:49 +0900 | [diff] [blame] | 87 | stream.close(); |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Test |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 91 | public void loadApp() throws IOException { |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 92 | saveZippedApp(); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 93 | ApplicationDescription app = aar.getApplicationDescription(APP_NAME); |
| 94 | validate(app); |
| 95 | } |
| 96 | |
| 97 | @Test |
| 98 | public void getAppNames() throws IOException { |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 99 | saveZippedApp(); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 100 | Set<String> names = aar.getApplicationNames(); |
| 101 | assertEquals("incorrect names", ImmutableSet.of(APP_NAME), names); |
| 102 | } |
| 103 | |
| 104 | @Test |
| 105 | public void purgeApp() throws IOException { |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 106 | saveZippedApp(); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 107 | aar.purgeApplication(APP_NAME); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame] | 108 | assertEquals("incorrect names", ImmutableSet.<String>of(), |
| 109 | aar.getApplicationNames()); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Test |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 113 | public void getAppZipStream() throws IOException { |
| 114 | saveZippedApp(); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 115 | InputStream stream = aar.getApplicationInputStream(APP_NAME); |
| 116 | byte[] orig = ByteStreams.toByteArray(getClass().getResourceAsStream("app.zip")); |
| 117 | byte[] loaded = ByteStreams.toByteArray(stream); |
| 118 | assertArrayEquals("incorrect stream", orig, loaded); |
Kenji HIKICHI | 5448d46 | 2015-07-03 18:24:49 +0900 | [diff] [blame] | 119 | stream.close(); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 122 | @Test |
| 123 | public void getAppXmlStream() throws IOException { |
| 124 | savePlainApp(); |
| 125 | InputStream stream = aar.getApplicationInputStream(APP_NAME); |
| 126 | byte[] orig = ByteStreams.toByteArray(getClass().getResourceAsStream("app.xml")); |
| 127 | byte[] loaded = ByteStreams.toByteArray(stream); |
| 128 | assertArrayEquals("incorrect stream", orig, loaded); |
Kenji HIKICHI | 5448d46 | 2015-07-03 18:24:49 +0900 | [diff] [blame] | 129 | stream.close(); |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Test |
| 133 | public void active() throws IOException { |
| 134 | savePlainApp(); |
| 135 | assertFalse("should not be active", aar.isActive(APP_NAME)); |
| 136 | aar.setActive(APP_NAME); |
| 137 | assertTrue("should not be active", aar.isActive(APP_NAME)); |
| 138 | aar.clearActive(APP_NAME); |
| 139 | assertFalse("should not be active", aar.isActive(APP_NAME)); |
| 140 | } |
| 141 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 142 | @Test(expected = ApplicationException.class) |
| 143 | public void getBadAppDesc() throws IOException { |
| 144 | aar.getApplicationDescription("org.foo.BAD"); |
| 145 | } |
| 146 | |
| 147 | @Test(expected = ApplicationException.class) |
| 148 | public void getBadAppStream() throws IOException { |
| 149 | aar.getApplicationInputStream("org.foo.BAD"); |
| 150 | } |
| 151 | |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 152 | @Test(expected = ApplicationException.class) |
Thomas Vachuska | e965b3d | 2016-03-03 11:42:48 -0800 | [diff] [blame^] | 153 | @Ignore("No longer needed") |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 154 | public void setBadActive() throws IOException { |
| 155 | aar.setActive("org.foo.BAD"); |
| 156 | } |
| 157 | |
Thomas Vachuska | f9c8436 | 2015-04-15 11:20:45 -0700 | [diff] [blame] | 158 | @Test // (expected = ApplicationException.class) |
Thomas Vachuska | 62ad95f | 2015-02-18 12:11:36 -0800 | [diff] [blame] | 159 | public void purgeBadApp() throws IOException { |
| 160 | aar.purgeApplication("org.foo.BAD"); |
| 161 | } |
| 162 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 163 | } |