blob: 97012c4e0c9fc0ae381d4ac8750bc60f97d6df1a [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;
23import org.junit.Test;
Thomas Vachuska90b453f2015-01-30 18:57:14 -080024import org.onlab.util.Tools;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080025import org.onosproject.app.ApplicationDescription;
26import org.onosproject.app.ApplicationException;
27
Thomas Vachuska90b453f2015-01-30 18:57:14 -080028import java.io.File;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080029import java.io.IOException;
30import java.io.InputStream;
31import java.util.Set;
32
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080033import static org.junit.Assert.*;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080034import static org.onosproject.app.DefaultApplicationDescriptionTest.*;
35
Thomas Vachuskaad35c342015-06-11 17:25:36 -070036/**
37 * Suite of tests for the application archive utility.
38 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -080039public class ApplicationArchiveTest {
40
Thomas Vachuska8044c092015-08-04 11:06:41 -070041 static final File STORE = Files.createTempDir();
Thomas Vachuska02aeb032015-01-06 22:36:30 -080042
43 private ApplicationArchive aar = new ApplicationArchive();
44
45 @Before
46 public void setUp() {
Thomas Vachuska8044c092015-08-04 11:06:41 -070047 aar.setRootPath(STORE.getAbsolutePath());
Thomas Vachuska90b453f2015-01-30 18:57:14 -080048 }
49
50 @After
51 public void tearDown() throws IOException {
Thomas Vachuska8044c092015-08-04 11:06:41 -070052 if (STORE.exists()) {
53 Tools.removeDirectory(STORE);
Thomas Vachuska90b453f2015-01-30 18:57:14 -080054 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -080055 }
56
57 private void validate(ApplicationDescription app) {
58 assertEquals("incorrect name", APP_NAME, app.name());
59 assertEquals("incorrect version", VER, app.version());
60 assertEquals("incorrect origin", ORIGIN, app.origin());
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090061 assertEquals("incorrect role", ROLE, app.role());
Thomas Vachuska02aeb032015-01-06 22:36:30 -080062
63 assertEquals("incorrect description", DESC, app.description());
64 assertEquals("incorrect features URI", FURL, app.featuresRepo().get());
65 assertEquals("incorrect permissions", PERMS, app.permissions());
66 assertEquals("incorrect features", FEATURES, app.features());
67 }
68
69 @Test
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080070 public void saveZippedApp() throws IOException {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080071 InputStream stream = getClass().getResourceAsStream("app.zip");
72 ApplicationDescription app = aar.saveApplication(stream);
73 validate(app);
Kenji HIKICHI5448d462015-07-03 18:24:49 +090074 stream.close();
Thomas Vachuska02aeb032015-01-06 22:36:30 -080075 }
76
77 @Test
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080078 public void savePlainApp() throws IOException {
79 InputStream stream = getClass().getResourceAsStream("app.xml");
80 ApplicationDescription app = aar.saveApplication(stream);
81 validate(app);
Kenji HIKICHI5448d462015-07-03 18:24:49 +090082 stream.close();
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080083 }
84
85 @Test
Thomas Vachuska02aeb032015-01-06 22:36:30 -080086 public void loadApp() throws IOException {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080087 saveZippedApp();
Thomas Vachuska02aeb032015-01-06 22:36:30 -080088 ApplicationDescription app = aar.getApplicationDescription(APP_NAME);
89 validate(app);
90 }
91
92 @Test
93 public void getAppNames() throws IOException {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080094 saveZippedApp();
Thomas Vachuska02aeb032015-01-06 22:36:30 -080095 Set<String> names = aar.getApplicationNames();
96 assertEquals("incorrect names", ImmutableSet.of(APP_NAME), names);
97 }
98
99 @Test
100 public void purgeApp() throws IOException {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800101 saveZippedApp();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800102 aar.purgeApplication(APP_NAME);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800103 assertEquals("incorrect names", ImmutableSet.<String>of(),
104 aar.getApplicationNames());
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800105 }
106
107 @Test
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800108 public void getAppZipStream() throws IOException {
109 saveZippedApp();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800110 InputStream stream = aar.getApplicationInputStream(APP_NAME);
111 byte[] orig = ByteStreams.toByteArray(getClass().getResourceAsStream("app.zip"));
112 byte[] loaded = ByteStreams.toByteArray(stream);
113 assertArrayEquals("incorrect stream", orig, loaded);
Kenji HIKICHI5448d462015-07-03 18:24:49 +0900114 stream.close();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800115 }
116
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800117 @Test
118 public void getAppXmlStream() throws IOException {
119 savePlainApp();
120 InputStream stream = aar.getApplicationInputStream(APP_NAME);
121 byte[] orig = ByteStreams.toByteArray(getClass().getResourceAsStream("app.xml"));
122 byte[] loaded = ByteStreams.toByteArray(stream);
123 assertArrayEquals("incorrect stream", orig, loaded);
Kenji HIKICHI5448d462015-07-03 18:24:49 +0900124 stream.close();
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800125 }
126
127 @Test
128 public void active() throws IOException {
129 savePlainApp();
130 assertFalse("should not be active", aar.isActive(APP_NAME));
131 aar.setActive(APP_NAME);
132 assertTrue("should not be active", aar.isActive(APP_NAME));
133 aar.clearActive(APP_NAME);
134 assertFalse("should not be active", aar.isActive(APP_NAME));
135 }
136
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800137 @Test(expected = ApplicationException.class)
138 public void getBadAppDesc() throws IOException {
139 aar.getApplicationDescription("org.foo.BAD");
140 }
141
142 @Test(expected = ApplicationException.class)
143 public void getBadAppStream() throws IOException {
144 aar.getApplicationInputStream("org.foo.BAD");
145 }
146
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800147 @Test(expected = ApplicationException.class)
148 public void setBadActive() throws IOException {
149 aar.setActive("org.foo.BAD");
150 }
151
Thomas Vachuskaf9c84362015-04-15 11:20:45 -0700152 @Test // (expected = ApplicationException.class)
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800153 public void purgeBadApp() throws IOException {
154 aar.purgeApplication("org.foo.BAD");
155 }
156
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800157}