blob: 9472f5a9852b01c68c4a5076f7ccaee695d382df [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
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
36public class ApplicationArchiveTest {
37
Thomas Vachuska5d4a58d2015-04-28 11:40:05 -070038 static final String ROOT = "/tmp/app-junit/";
39 static final String STORE = ROOT + new Random().nextInt(1000) + "/foo";
Thomas Vachuska02aeb032015-01-06 22:36:30 -080040
41 private ApplicationArchive aar = new ApplicationArchive();
42
43 @Before
44 public void setUp() {
Thomas Vachuska5d4a58d2015-04-28 11:40:05 -070045 aar.setRootPath(STORE);
Thomas Vachuska90b453f2015-01-30 18:57:14 -080046 }
47
48 @After
49 public void tearDown() throws IOException {
Thomas Vachuska5d4a58d2015-04-28 11:40:05 -070050 if (new File(ROOT).exists()) {
51 Tools.removeDirectory(ROOT);
Thomas Vachuska90b453f2015-01-30 18:57:14 -080052 }
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());
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090059 assertEquals("incorrect role", ROLE, app.role());
Thomas Vachuska02aeb032015-01-06 22:36:30 -080060
61 assertEquals("incorrect description", DESC, app.description());
62 assertEquals("incorrect features URI", FURL, app.featuresRepo().get());
63 assertEquals("incorrect permissions", PERMS, app.permissions());
64 assertEquals("incorrect features", FEATURES, app.features());
65 }
66
67 @Test
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080068 public void saveZippedApp() throws IOException {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080069 InputStream stream = getClass().getResourceAsStream("app.zip");
70 ApplicationDescription app = aar.saveApplication(stream);
71 validate(app);
72 }
73
74 @Test
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080075 public void savePlainApp() throws IOException {
76 InputStream stream = getClass().getResourceAsStream("app.xml");
77 ApplicationDescription app = aar.saveApplication(stream);
78 validate(app);
79 }
80
81 @Test
Thomas Vachuska02aeb032015-01-06 22:36:30 -080082 public void loadApp() throws IOException {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080083 saveZippedApp();
Thomas Vachuska02aeb032015-01-06 22:36:30 -080084 ApplicationDescription app = aar.getApplicationDescription(APP_NAME);
85 validate(app);
86 }
87
88 @Test
89 public void getAppNames() throws IOException {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080090 saveZippedApp();
Thomas Vachuska02aeb032015-01-06 22:36:30 -080091 Set<String> names = aar.getApplicationNames();
92 assertEquals("incorrect names", ImmutableSet.of(APP_NAME), names);
93 }
94
95 @Test
96 public void purgeApp() throws IOException {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080097 saveZippedApp();
Thomas Vachuska02aeb032015-01-06 22:36:30 -080098 aar.purgeApplication(APP_NAME);
Thomas Vachuska90b453f2015-01-30 18:57:14 -080099 assertEquals("incorrect names", ImmutableSet.<String>of(),
100 aar.getApplicationNames());
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800101 }
102
103 @Test
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800104 public void getAppZipStream() throws IOException {
105 saveZippedApp();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800106 InputStream stream = aar.getApplicationInputStream(APP_NAME);
107 byte[] orig = ByteStreams.toByteArray(getClass().getResourceAsStream("app.zip"));
108 byte[] loaded = ByteStreams.toByteArray(stream);
109 assertArrayEquals("incorrect stream", orig, loaded);
110 }
111
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800112 @Test
113 public void getAppXmlStream() throws IOException {
114 savePlainApp();
115 InputStream stream = aar.getApplicationInputStream(APP_NAME);
116 byte[] orig = ByteStreams.toByteArray(getClass().getResourceAsStream("app.xml"));
117 byte[] loaded = ByteStreams.toByteArray(stream);
118 assertArrayEquals("incorrect stream", orig, loaded);
119 }
120
121 @Test
122 public void active() throws IOException {
123 savePlainApp();
124 assertFalse("should not be active", aar.isActive(APP_NAME));
125 aar.setActive(APP_NAME);
126 assertTrue("should not be active", aar.isActive(APP_NAME));
127 aar.clearActive(APP_NAME);
128 assertFalse("should not be active", aar.isActive(APP_NAME));
129 }
130
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800131 @Test(expected = ApplicationException.class)
132 public void getBadAppDesc() throws IOException {
133 aar.getApplicationDescription("org.foo.BAD");
134 }
135
136 @Test(expected = ApplicationException.class)
137 public void getBadAppStream() throws IOException {
138 aar.getApplicationInputStream("org.foo.BAD");
139 }
140
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800141 @Test(expected = ApplicationException.class)
142 public void setBadActive() throws IOException {
143 aar.setActive("org.foo.BAD");
144 }
145
Thomas Vachuskaf9c84362015-04-15 11:20:45 -0700146 @Test // (expected = ApplicationException.class)
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800147 public void purgeBadApp() throws IOException {
148 aar.purgeApplication("org.foo.BAD");
149 }
150
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800151}