blob: 2682e86bee13febadce5fafa60efffa9bf84de19 [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
Thomas Vachuskacc5d0382015-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 Vachuska5d4a58d2015-04-28 11:40:05 -070041 static final String ROOT = "/tmp/app-junit/";
42 static final String STORE = ROOT + new Random().nextInt(1000) + "/foo";
Thomas Vachuska02aeb032015-01-06 22:36:30 -080043
44 private ApplicationArchive aar = new ApplicationArchive();
45
46 @Before
47 public void setUp() {
Thomas Vachuska5d4a58d2015-04-28 11:40:05 -070048 aar.setRootPath(STORE);
Thomas Vachuska90b453f2015-01-30 18:57:14 -080049 }
50
51 @After
52 public void tearDown() throws IOException {
Thomas Vachuska5d4a58d2015-04-28 11:40:05 -070053 if (new File(ROOT).exists()) {
54 Tools.removeDirectory(ROOT);
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
64 assertEquals("incorrect description", DESC, app.description());
65 assertEquals("incorrect features URI", FURL, app.featuresRepo().get());
66 assertEquals("incorrect permissions", PERMS, app.permissions());
67 assertEquals("incorrect features", FEATURES, app.features());
68 }
69
70 @Test
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080071 public void saveZippedApp() throws IOException {
Thomas Vachuska02aeb032015-01-06 22:36:30 -080072 InputStream stream = getClass().getResourceAsStream("app.zip");
73 ApplicationDescription app = aar.saveApplication(stream);
74 validate(app);
75 }
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);
82 }
83
84 @Test
Thomas Vachuska02aeb032015-01-06 22:36:30 -080085 public void loadApp() throws IOException {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080086 saveZippedApp();
Thomas Vachuska02aeb032015-01-06 22:36:30 -080087 ApplicationDescription app = aar.getApplicationDescription(APP_NAME);
88 validate(app);
89 }
90
91 @Test
92 public void getAppNames() throws IOException {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -080093 saveZippedApp();
Thomas Vachuska02aeb032015-01-06 22:36:30 -080094 Set<String> names = aar.getApplicationNames();
95 assertEquals("incorrect names", ImmutableSet.of(APP_NAME), names);
96 }
97
98 @Test
99 public void purgeApp() throws IOException {
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800100 saveZippedApp();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800101 aar.purgeApplication(APP_NAME);
Thomas Vachuska90b453f2015-01-30 18:57:14 -0800102 assertEquals("incorrect names", ImmutableSet.<String>of(),
103 aar.getApplicationNames());
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800104 }
105
106 @Test
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800107 public void getAppZipStream() throws IOException {
108 saveZippedApp();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800109 InputStream stream = aar.getApplicationInputStream(APP_NAME);
110 byte[] orig = ByteStreams.toByteArray(getClass().getResourceAsStream("app.zip"));
111 byte[] loaded = ByteStreams.toByteArray(stream);
112 assertArrayEquals("incorrect stream", orig, loaded);
113 }
114
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800115 @Test
116 public void getAppXmlStream() throws IOException {
117 savePlainApp();
118 InputStream stream = aar.getApplicationInputStream(APP_NAME);
119 byte[] orig = ByteStreams.toByteArray(getClass().getResourceAsStream("app.xml"));
120 byte[] loaded = ByteStreams.toByteArray(stream);
121 assertArrayEquals("incorrect stream", orig, loaded);
122 }
123
124 @Test
125 public void active() throws IOException {
126 savePlainApp();
127 assertFalse("should not be active", aar.isActive(APP_NAME));
128 aar.setActive(APP_NAME);
129 assertTrue("should not be active", aar.isActive(APP_NAME));
130 aar.clearActive(APP_NAME);
131 assertFalse("should not be active", aar.isActive(APP_NAME));
132 }
133
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800134 @Test(expected = ApplicationException.class)
135 public void getBadAppDesc() throws IOException {
136 aar.getApplicationDescription("org.foo.BAD");
137 }
138
139 @Test(expected = ApplicationException.class)
140 public void getBadAppStream() throws IOException {
141 aar.getApplicationInputStream("org.foo.BAD");
142 }
143
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800144 @Test(expected = ApplicationException.class)
145 public void setBadActive() throws IOException {
146 aar.setActive("org.foo.BAD");
147 }
148
Thomas Vachuskaf9c84362015-04-15 11:20:45 -0700149 @Test // (expected = ApplicationException.class)
Thomas Vachuska62ad95f2015-02-18 12:11:36 -0800150 public void purgeBadApp() throws IOException {
151 aar.purgeApplication("org.foo.BAD");
152 }
153
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800154}