blob: 3d86ddaaf3ca3df811101689bb3e6889d6b4bf4b [file] [log] [blame]
Thomas Vachuska02aeb032015-01-06 22:36:30 -08001/*
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 */
16package org.onosproject.common.app;
17
18import com.google.common.collect.ImmutableSet;
19import com.google.common.io.ByteStreams;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080020import org.junit.After;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080021import org.junit.Before;
22import org.junit.Test;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080023import org.onlab.util.Tools;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080024import org.onosproject.app.ApplicationDescription;
25import org.onosproject.app.ApplicationException;
26
Thomas Vachuska90b453f2015-01-30 18:57:14 -080027import java.io.File;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080028import java.io.IOException;
29import java.io.InputStream;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080030import java.util.Random;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080031import java.util.Set;
32
33import static org.junit.Assert.assertArrayEquals;
34import static org.junit.Assert.assertEquals;
35import static org.onosproject.app.DefaultApplicationDescriptionTest.*;
36
37public class ApplicationArchiveTest {
38
Thomas Vachuska90b453f2015-01-30 18:57:14 -080039 static final String ROOT = "/tmp/app-junit/" + new Random().nextInt();
Thomas Vachuska02aeb032015-01-06 22:36:30 -080040
41 private ApplicationArchive aar = new ApplicationArchive();
42
43 @Before
44 public void setUp() {
Thomas Vachuska90b453f2015-01-30 18:57:14 -080045 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 Vachuska02aeb032015-01-06 22:36:30 -080053 }
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 Vachuska90b453f2015-01-30 18:57:14 -080091 assertEquals("incorrect names", ImmutableSet.<String>of(),
92 aar.getApplicationNames());
Thomas Vachuska02aeb032015-01-06 22:36:30 -080093 }
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}