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 | |
| 33 | import static org.junit.Assert.assertArrayEquals; |
| 34 | import static org.junit.Assert.assertEquals; |
| 35 | import static org.onosproject.app.DefaultApplicationDescriptionTest.*; |
| 36 | |
| 37 | public class ApplicationArchiveTest { |
| 38 | |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame^] | 39 | static final String ROOT = "/tmp/app-junit/" + new Random().nextInt(); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 40 | |
| 41 | private ApplicationArchive aar = new ApplicationArchive(); |
| 42 | |
| 43 | @Before |
| 44 | public void setUp() { |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame^] | 45 | aar.setRootPath(ROOT); |
| 46 | } |
| 47 | |
| 48 | @After |
| 49 | public void tearDown() throws IOException { |
| 50 | if (new File(aar.getRootPath()).exists()) { |
| 51 | Tools.removeDirectory(aar.getRootPath()); |
| 52 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | private void validate(ApplicationDescription app) { |
| 56 | assertEquals("incorrect name", APP_NAME, app.name()); |
| 57 | assertEquals("incorrect version", VER, app.version()); |
| 58 | assertEquals("incorrect origin", ORIGIN, app.origin()); |
| 59 | |
| 60 | assertEquals("incorrect description", DESC, app.description()); |
| 61 | assertEquals("incorrect features URI", FURL, app.featuresRepo().get()); |
| 62 | assertEquals("incorrect permissions", PERMS, app.permissions()); |
| 63 | assertEquals("incorrect features", FEATURES, app.features()); |
| 64 | } |
| 65 | |
| 66 | @Test |
| 67 | public void saveApp() throws IOException { |
| 68 | InputStream stream = getClass().getResourceAsStream("app.zip"); |
| 69 | ApplicationDescription app = aar.saveApplication(stream); |
| 70 | validate(app); |
| 71 | } |
| 72 | |
| 73 | @Test |
| 74 | public void loadApp() throws IOException { |
| 75 | saveApp(); |
| 76 | ApplicationDescription app = aar.getApplicationDescription(APP_NAME); |
| 77 | validate(app); |
| 78 | } |
| 79 | |
| 80 | @Test |
| 81 | public void getAppNames() throws IOException { |
| 82 | saveApp(); |
| 83 | Set<String> names = aar.getApplicationNames(); |
| 84 | assertEquals("incorrect names", ImmutableSet.of(APP_NAME), names); |
| 85 | } |
| 86 | |
| 87 | @Test |
| 88 | public void purgeApp() throws IOException { |
| 89 | saveApp(); |
| 90 | aar.purgeApplication(APP_NAME); |
Thomas Vachuska | 90b453f | 2015-01-30 18:57:14 -0800 | [diff] [blame^] | 91 | assertEquals("incorrect names", ImmutableSet.<String>of(), |
| 92 | aar.getApplicationNames()); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Test |
| 96 | public void getAppStream() throws IOException { |
| 97 | saveApp(); |
| 98 | InputStream stream = aar.getApplicationInputStream(APP_NAME); |
| 99 | byte[] orig = ByteStreams.toByteArray(getClass().getResourceAsStream("app.zip")); |
| 100 | byte[] loaded = ByteStreams.toByteArray(stream); |
| 101 | assertArrayEquals("incorrect stream", orig, loaded); |
| 102 | } |
| 103 | |
| 104 | @Test(expected = ApplicationException.class) |
| 105 | public void getBadAppDesc() throws IOException { |
| 106 | aar.getApplicationDescription("org.foo.BAD"); |
| 107 | } |
| 108 | |
| 109 | @Test(expected = ApplicationException.class) |
| 110 | public void getBadAppStream() throws IOException { |
| 111 | aar.getApplicationInputStream("org.foo.BAD"); |
| 112 | } |
| 113 | |
| 114 | } |