blob: d2c6f3c097f2758365ea2512a4ab21398bcef36a [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 Vachuska02aeb032015-01-06 22:36:30 -080092 public void loadApp() throws IOException {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080093 saveZippedApp();
Thomas Vachuska02aeb032015-01-06 22:36:30 -080094 ApplicationDescription app = aar.getApplicationDescription(APP_NAME);
95 validate(app);
96 }
97
98 @Test
99 public void getAppNames() throws IOException {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800100 saveZippedApp();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800101 Set<String> names = aar.getApplicationNames();
102 assertEquals("incorrect names", ImmutableSet.of(APP_NAME), names);
103 }
104
105 @Test
106 public void purgeApp() throws IOException {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800107 saveZippedApp();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800108 aar.purgeApplication(APP_NAME);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800109 assertEquals("incorrect names", ImmutableSet.<String>of(),
110 aar.getApplicationNames());
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800111 }
112
113 @Test
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800114 public void getAppZipStream() throws IOException {
115 saveZippedApp();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800116 InputStream stream = aar.getApplicationInputStream(APP_NAME);
117 byte[] orig = ByteStreams.toByteArray(getClass().getResourceAsStream("app.zip"));
118 byte[] loaded = ByteStreams.toByteArray(stream);
119 assertArrayEquals("incorrect stream", orig, loaded);
Kenji HIKICHI5448d462015-07-03 18:24:49 +0900120 stream.close();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800121 }
122
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800123 @Test
124 public void getAppXmlStream() throws IOException {
125 savePlainApp();
126 InputStream stream = aar.getApplicationInputStream(APP_NAME);
127 byte[] orig = ByteStreams.toByteArray(getClass().getResourceAsStream("app.xml"));
128 byte[] loaded = ByteStreams.toByteArray(stream);
129 assertArrayEquals("incorrect stream", orig, loaded);
Kenji HIKICHI5448d462015-07-03 18:24:49 +0900130 stream.close();
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800131 }
132
133 @Test
134 public void active() throws IOException {
135 savePlainApp();
136 assertFalse("should not be active", aar.isActive(APP_NAME));
137 aar.setActive(APP_NAME);
138 assertTrue("should not be active", aar.isActive(APP_NAME));
139 aar.clearActive(APP_NAME);
140 assertFalse("should not be active", aar.isActive(APP_NAME));
141 }
142
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800143 @Test(expected = ApplicationException.class)
144 public void getBadAppDesc() throws IOException {
145 aar.getApplicationDescription("org.foo.BAD");
146 }
147
148 @Test(expected = ApplicationException.class)
149 public void getBadAppStream() throws IOException {
150 aar.getApplicationInputStream("org.foo.BAD");
151 }
152
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800153 @Test(expected = ApplicationException.class)
Thomas Vachuskae965b3d2016-03-03 11:42:48 -0800154 @Ignore("No longer needed")
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800155 public void setBadActive() throws IOException {
156 aar.setActive("org.foo.BAD");
157 }
158
Thomas Vachuskaf9c84362015-04-15 11:20:45 -0700159 @Test // (expected = ApplicationException.class)
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800160 public void purgeBadApp() throws IOException {
161 aar.purgeApplication("org.foo.BAD");
162 }
163
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800164}