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