blob: 1f273cc7d9bceccad9f45fa34084c6e0a4a62e51 [file] [log] [blame]
Thomas Vachuska02aeb032015-01-06 22:36:30 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska02aeb032015-01-06 22:36:30 -08003 *
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 Vachuska8044c092015-08-04 11:06:41 -070020import com.google.common.io.Files;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080021import org.junit.After;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080022import org.junit.Before;
Thomas Vachuskae965b3d2016-03-03 11:42:48 -080023import org.junit.Ignore;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080024import org.junit.Test;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080025import org.onlab.util.Tools;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080026import org.onosproject.app.ApplicationDescription;
27import org.onosproject.app.ApplicationException;
28
Thomas Vachuska90b453f2015-01-30 18:57:14 -080029import java.io.File;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080030import java.io.IOException;
31import java.io.InputStream;
32import java.util.Set;
33
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080034import static org.junit.Assert.*;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080035import static org.onosproject.app.DefaultApplicationDescriptionTest.*;
36
Thomas Vachuskaad35c342015-06-11 17:25:36 -070037/**
38 * Suite of tests for the application archive utility.
39 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -080040public class ApplicationArchiveTest {
41
Thomas Vachuska8044c092015-08-04 11:06:41 -070042 static final File STORE = Files.createTempDir();
Thomas Vachuska02aeb032015-01-06 22:36:30 -080043
44 private ApplicationArchive aar = new ApplicationArchive();
45
46 @Before
47 public void setUp() {
Thomas Vachuska8044c092015-08-04 11:06:41 -070048 aar.setRootPath(STORE.getAbsolutePath());
Thomas Vachuska90b453f2015-01-30 18:57:14 -080049 }
50
51 @After
52 public void tearDown() throws IOException {
Thomas Vachuska8044c092015-08-04 11:06:41 -070053 if (STORE.exists()) {
54 Tools.removeDirectory(STORE);
Thomas Vachuska90b453f2015-01-30 18:57:14 -080055 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -080056 }
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 Yoonbdeb88a2015-05-12 20:35:31 +090062 assertEquals("incorrect role", ROLE, app.role());
Thomas Vachuska02aeb032015-01-06 22:36:30 -080063
Jian Lic35415d2016-01-14 17:22:31 -080064 assertEquals("incorrect category", CATEGORY, app.category());
65 assertEquals("incorrect url", URL, app.url());
66 assertEquals("incorrect readme", README, app.readme());
67
Simon Huntafae2f72016-03-04 21:18:23 -080068 assertEquals("incorrect title", TITLE, app.title());
Thomas Vachuska02aeb032015-01-06 22:36:30 -080069 assertEquals("incorrect description", DESC, app.description());
70 assertEquals("incorrect features URI", FURL, app.featuresRepo().get());
71 assertEquals("incorrect permissions", PERMS, app.permissions());
72 assertEquals("incorrect features", FEATURES, app.features());
73 }
74
75 @Test
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080076 public void saveZippedApp() throws IOException {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080077 InputStream stream = getClass().getResourceAsStream("app.zip");
78 ApplicationDescription app = aar.saveApplication(stream);
79 validate(app);
Kenji HIKICHI5448d462015-07-03 18:24:49 +090080 stream.close();
Thomas Vachuska02aeb032015-01-06 22:36:30 -080081 }
82
83 @Test
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080084 public void savePlainApp() throws IOException {
85 InputStream stream = getClass().getResourceAsStream("app.xml");
86 ApplicationDescription app = aar.saveApplication(stream);
87 validate(app);
Kenji HIKICHI5448d462015-07-03 18:24:49 +090088 stream.close();
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080089 }
90
91 @Test
Thomas Vachuska6cc74882017-01-10 16:01:02 -080092 public void saveSelfContainedApp() throws IOException {
93 InputStream stream = getClass().getResourceAsStream("app.scj");
94 ApplicationDescription app = aar.saveApplication(stream);
95 validate(app);
96 stream.close();
97 }
98
99 @Test
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800100 public void loadApp() throws IOException {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800101 saveZippedApp();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800102 ApplicationDescription app = aar.getApplicationDescription(APP_NAME);
103 validate(app);
104 }
105
106 @Test
107 public void getAppNames() throws IOException {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800108 saveZippedApp();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800109 Set<String> names = aar.getApplicationNames();
110 assertEquals("incorrect names", ImmutableSet.of(APP_NAME), names);
111 }
112
113 @Test
114 public void purgeApp() throws IOException {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800115 saveZippedApp();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800116 aar.purgeApplication(APP_NAME);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800117 assertEquals("incorrect names", ImmutableSet.<String>of(),
118 aar.getApplicationNames());
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800119 }
120
121 @Test
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800122 public void getAppZipStream() throws IOException {
123 saveZippedApp();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800124 InputStream stream = aar.getApplicationInputStream(APP_NAME);
125 byte[] orig = ByteStreams.toByteArray(getClass().getResourceAsStream("app.zip"));
126 byte[] loaded = ByteStreams.toByteArray(stream);
127 assertArrayEquals("incorrect stream", orig, loaded);
Kenji HIKICHI5448d462015-07-03 18:24:49 +0900128 stream.close();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800129 }
130
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800131 @Test
132 public void getAppXmlStream() throws IOException {
133 savePlainApp();
134 InputStream stream = aar.getApplicationInputStream(APP_NAME);
135 byte[] orig = ByteStreams.toByteArray(getClass().getResourceAsStream("app.xml"));
136 byte[] loaded = ByteStreams.toByteArray(stream);
137 assertArrayEquals("incorrect stream", orig, loaded);
Kenji HIKICHI5448d462015-07-03 18:24:49 +0900138 stream.close();
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800139 }
140
141 @Test
142 public void active() throws IOException {
143 savePlainApp();
144 assertFalse("should not be active", aar.isActive(APP_NAME));
145 aar.setActive(APP_NAME);
146 assertTrue("should not be active", aar.isActive(APP_NAME));
147 aar.clearActive(APP_NAME);
148 assertFalse("should not be active", aar.isActive(APP_NAME));
149 }
150
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800151 @Test(expected = ApplicationException.class)
152 public void getBadAppDesc() throws IOException {
153 aar.getApplicationDescription("org.foo.BAD");
154 }
155
156 @Test(expected = ApplicationException.class)
157 public void getBadAppStream() throws IOException {
158 aar.getApplicationInputStream("org.foo.BAD");
159 }
160
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800161 @Test(expected = ApplicationException.class)
Thomas Vachuskae965b3d2016-03-03 11:42:48 -0800162 @Ignore("No longer needed")
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800163 public void setBadActive() throws IOException {
164 aar.setActive("org.foo.BAD");
165 }
166
Thomas Vachuskaf9c84362015-04-15 11:20:45 -0700167 @Test // (expected = ApplicationException.class)
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800168 public void purgeBadApp() throws IOException {
169 aar.purgeApplication("org.foo.BAD");
170 }
171
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800172}