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; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onosproject.app.ApplicationDescription; |
| 23 | import org.onosproject.app.ApplicationException; |
| 24 | |
| 25 | import java.io.IOException; |
| 26 | import java.io.InputStream; |
| 27 | import java.util.Set; |
| 28 | |
| 29 | import static org.junit.Assert.assertArrayEquals; |
| 30 | import static org.junit.Assert.assertEquals; |
| 31 | import static org.onosproject.app.DefaultApplicationDescriptionTest.*; |
| 32 | |
| 33 | public class ApplicationArchiveTest { |
| 34 | |
| 35 | static final String ROOT = "/tmp/app-junit"; |
| 36 | |
| 37 | private ApplicationArchive aar = new ApplicationArchive(); |
| 38 | |
| 39 | @Before |
| 40 | public void setUp() { |
| 41 | aar.setAppsRoot(ROOT); |
| 42 | } |
| 43 | |
| 44 | private void validate(ApplicationDescription app) { |
| 45 | assertEquals("incorrect name", APP_NAME, app.name()); |
| 46 | assertEquals("incorrect version", VER, app.version()); |
| 47 | assertEquals("incorrect origin", ORIGIN, app.origin()); |
| 48 | |
| 49 | assertEquals("incorrect description", DESC, app.description()); |
| 50 | assertEquals("incorrect features URI", FURL, app.featuresRepo().get()); |
| 51 | assertEquals("incorrect permissions", PERMS, app.permissions()); |
| 52 | assertEquals("incorrect features", FEATURES, app.features()); |
| 53 | } |
| 54 | |
| 55 | @Test |
| 56 | public void saveApp() throws IOException { |
| 57 | InputStream stream = getClass().getResourceAsStream("app.zip"); |
| 58 | ApplicationDescription app = aar.saveApplication(stream); |
| 59 | validate(app); |
| 60 | } |
| 61 | |
| 62 | @Test |
| 63 | public void loadApp() throws IOException { |
| 64 | saveApp(); |
| 65 | ApplicationDescription app = aar.getApplicationDescription(APP_NAME); |
| 66 | validate(app); |
| 67 | } |
| 68 | |
| 69 | @Test |
| 70 | public void getAppNames() throws IOException { |
| 71 | saveApp(); |
| 72 | Set<String> names = aar.getApplicationNames(); |
| 73 | assertEquals("incorrect names", ImmutableSet.of(APP_NAME), names); |
| 74 | } |
| 75 | |
| 76 | @Test |
| 77 | public void purgeApp() throws IOException { |
| 78 | saveApp(); |
| 79 | aar.purgeApplication(APP_NAME); |
| 80 | assertEquals("incorrect names", ImmutableSet.of(), aar.getApplicationNames()); |
| 81 | } |
| 82 | |
| 83 | @Test |
| 84 | public void getAppStream() throws IOException { |
| 85 | saveApp(); |
| 86 | InputStream stream = aar.getApplicationInputStream(APP_NAME); |
| 87 | byte[] orig = ByteStreams.toByteArray(getClass().getResourceAsStream("app.zip")); |
| 88 | byte[] loaded = ByteStreams.toByteArray(stream); |
| 89 | assertArrayEquals("incorrect stream", orig, loaded); |
| 90 | } |
| 91 | |
| 92 | @Test(expected = ApplicationException.class) |
| 93 | public void getBadAppDesc() throws IOException { |
| 94 | aar.getApplicationDescription("org.foo.BAD"); |
| 95 | } |
| 96 | |
| 97 | @Test(expected = ApplicationException.class) |
| 98 | public void getBadAppStream() throws IOException { |
| 99 | aar.getApplicationInputStream("org.foo.BAD"); |
| 100 | } |
| 101 | |
| 102 | } |