blob: 87c636387484eb02dd81e7cae6b8e7e78a8dfd22 [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 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
Thomas Vachuska02aeb032015-01-06 22:36:30 -080068 assertEquals("incorrect description", DESC, app.description());
69 assertEquals("incorrect features URI", FURL, app.featuresRepo().get());
70 assertEquals("incorrect permissions", PERMS, app.permissions());
71 assertEquals("incorrect features", FEATURES, app.features());
72 }
73
74 @Test
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080075 public void saveZippedApp() throws IOException {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080076 InputStream stream = getClass().getResourceAsStream("app.zip");
77 ApplicationDescription app = aar.saveApplication(stream);
78 validate(app);
Kenji HIKICHI5448d462015-07-03 18:24:49 +090079 stream.close();
Thomas Vachuska02aeb032015-01-06 22:36:30 -080080 }
81
82 @Test
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080083 public void savePlainApp() throws IOException {
84 InputStream stream = getClass().getResourceAsStream("app.xml");
85 ApplicationDescription app = aar.saveApplication(stream);
86 validate(app);
Kenji HIKICHI5448d462015-07-03 18:24:49 +090087 stream.close();
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080088 }
89
90 @Test
Thomas Vachuska02aeb032015-01-06 22:36:30 -080091 public void loadApp() throws IOException {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080092 saveZippedApp();
Thomas Vachuska02aeb032015-01-06 22:36:30 -080093 ApplicationDescription app = aar.getApplicationDescription(APP_NAME);
94 validate(app);
95 }
96
97 @Test
98 public void getAppNames() throws IOException {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080099 saveZippedApp();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800100 Set<String> names = aar.getApplicationNames();
101 assertEquals("incorrect names", ImmutableSet.of(APP_NAME), names);
102 }
103
104 @Test
105 public void purgeApp() throws IOException {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800106 saveZippedApp();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800107 aar.purgeApplication(APP_NAME);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800108 assertEquals("incorrect names", ImmutableSet.<String>of(),
109 aar.getApplicationNames());
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800110 }
111
112 @Test
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800113 public void getAppZipStream() throws IOException {
114 saveZippedApp();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800115 InputStream stream = aar.getApplicationInputStream(APP_NAME);
116 byte[] orig = ByteStreams.toByteArray(getClass().getResourceAsStream("app.zip"));
117 byte[] loaded = ByteStreams.toByteArray(stream);
118 assertArrayEquals("incorrect stream", orig, loaded);
Kenji HIKICHI5448d462015-07-03 18:24:49 +0900119 stream.close();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800120 }
121
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800122 @Test
123 public void getAppXmlStream() throws IOException {
124 savePlainApp();
125 InputStream stream = aar.getApplicationInputStream(APP_NAME);
126 byte[] orig = ByteStreams.toByteArray(getClass().getResourceAsStream("app.xml"));
127 byte[] loaded = ByteStreams.toByteArray(stream);
128 assertArrayEquals("incorrect stream", orig, loaded);
Kenji HIKICHI5448d462015-07-03 18:24:49 +0900129 stream.close();
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800130 }
131
132 @Test
133 public void active() throws IOException {
134 savePlainApp();
135 assertFalse("should not be active", aar.isActive(APP_NAME));
136 aar.setActive(APP_NAME);
137 assertTrue("should not be active", aar.isActive(APP_NAME));
138 aar.clearActive(APP_NAME);
139 assertFalse("should not be active", aar.isActive(APP_NAME));
140 }
141
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800142 @Test(expected = ApplicationException.class)
143 public void getBadAppDesc() throws IOException {
144 aar.getApplicationDescription("org.foo.BAD");
145 }
146
147 @Test(expected = ApplicationException.class)
148 public void getBadAppStream() throws IOException {
149 aar.getApplicationInputStream("org.foo.BAD");
150 }
151
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800152 @Test(expected = ApplicationException.class)
Thomas Vachuskae965b3d2016-03-03 11:42:48 -0800153 @Ignore("No longer needed")
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800154 public void setBadActive() throws IOException {
155 aar.setActive("org.foo.BAD");
156 }
157
Thomas Vachuskaf9c84362015-04-15 11:20:45 -0700158 @Test // (expected = ApplicationException.class)
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800159 public void purgeBadApp() throws IOException {
160 aar.purgeApplication("org.foo.BAD");
161 }
162
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800163}