blob: 240ed96aa96e72d5597d85c7a931aa6a3b222195 [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 Vachuska90b453f2015-01-30 18:57:14 -080038 static final String ROOT = "/tmp/app-junit/" + new Random().nextInt();
Thomas Vachuska02aeb032015-01-06 22:36:30 -080039
40 private ApplicationArchive aar = new ApplicationArchive();
41
42 @Before
43 public void setUp() {
Thomas Vachuska90b453f2015-01-30 18:57:14 -080044 aar.setRootPath(ROOT);
45 }
46
47 @After
48 public void tearDown() throws IOException {
49 if (new File(aar.getRootPath()).exists()) {
50 Tools.removeDirectory(aar.getRootPath());
51 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -080052 }
53
54 private void validate(ApplicationDescription app) {
55 assertEquals("incorrect name", APP_NAME, app.name());
56 assertEquals("incorrect version", VER, app.version());
57 assertEquals("incorrect origin", ORIGIN, app.origin());
58
59 assertEquals("incorrect description", DESC, app.description());
60 assertEquals("incorrect features URI", FURL, app.featuresRepo().get());
61 assertEquals("incorrect permissions", PERMS, app.permissions());
62 assertEquals("incorrect features", FEATURES, app.features());
63 }
64
65 @Test
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080066 public void saveZippedApp() throws IOException {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080067 InputStream stream = getClass().getResourceAsStream("app.zip");
68 ApplicationDescription app = aar.saveApplication(stream);
69 validate(app);
70 }
71
72 @Test
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080073 public void savePlainApp() throws IOException {
74 InputStream stream = getClass().getResourceAsStream("app.xml");
75 ApplicationDescription app = aar.saveApplication(stream);
76 validate(app);
77 }
78
79 @Test
Thomas Vachuska02aeb032015-01-06 22:36:30 -080080 public void loadApp() throws IOException {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080081 saveZippedApp();
Thomas Vachuska02aeb032015-01-06 22:36:30 -080082 ApplicationDescription app = aar.getApplicationDescription(APP_NAME);
83 validate(app);
84 }
85
86 @Test
87 public void getAppNames() throws IOException {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080088 saveZippedApp();
Thomas Vachuska02aeb032015-01-06 22:36:30 -080089 Set<String> names = aar.getApplicationNames();
90 assertEquals("incorrect names", ImmutableSet.of(APP_NAME), names);
91 }
92
93 @Test
94 public void purgeApp() throws IOException {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080095 saveZippedApp();
Thomas Vachuska02aeb032015-01-06 22:36:30 -080096 aar.purgeApplication(APP_NAME);
Thomas Vachuska90b453f2015-01-30 18:57:14 -080097 assertEquals("incorrect names", ImmutableSet.<String>of(),
98 aar.getApplicationNames());
Thomas Vachuska02aeb032015-01-06 22:36:30 -080099 }
100
101 @Test
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800102 public void getAppZipStream() throws IOException {
103 saveZippedApp();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800104 InputStream stream = aar.getApplicationInputStream(APP_NAME);
105 byte[] orig = ByteStreams.toByteArray(getClass().getResourceAsStream("app.zip"));
106 byte[] loaded = ByteStreams.toByteArray(stream);
107 assertArrayEquals("incorrect stream", orig, loaded);
108 }
109
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800110 @Test
111 public void getAppXmlStream() throws IOException {
112 savePlainApp();
113 InputStream stream = aar.getApplicationInputStream(APP_NAME);
114 byte[] orig = ByteStreams.toByteArray(getClass().getResourceAsStream("app.xml"));
115 byte[] loaded = ByteStreams.toByteArray(stream);
116 assertArrayEquals("incorrect stream", orig, loaded);
117 }
118
119 @Test
120 public void active() throws IOException {
121 savePlainApp();
122 assertFalse("should not be active", aar.isActive(APP_NAME));
123 aar.setActive(APP_NAME);
124 assertTrue("should not be active", aar.isActive(APP_NAME));
125 aar.clearActive(APP_NAME);
126 assertFalse("should not be active", aar.isActive(APP_NAME));
127 }
128
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800129 @Test(expected = ApplicationException.class)
130 public void getBadAppDesc() throws IOException {
131 aar.getApplicationDescription("org.foo.BAD");
132 }
133
134 @Test(expected = ApplicationException.class)
135 public void getBadAppStream() throws IOException {
136 aar.getApplicationInputStream("org.foo.BAD");
137 }
138
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800139 @Test(expected = ApplicationException.class)
140 public void setBadActive() throws IOException {
141 aar.setActive("org.foo.BAD");
142 }
143
144 @Test(expected = ApplicationException.class)
145 public void purgeBadApp() throws IOException {
146 aar.purgeApplication("org.foo.BAD");
147 }
148
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800149}