blob: f66fb4de03b5b49045f39c64d4875cee19f834f0 [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.app.impl;
17
Thomas Vachuska761f0042015-11-11 19:10:17 -080018import com.google.common.collect.ImmutableList;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080019import com.google.common.collect.ImmutableSet;
20import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
23import org.onosproject.app.ApplicationEvent;
24import org.onosproject.app.ApplicationListener;
25import org.onosproject.app.ApplicationState;
26import org.onosproject.app.ApplicationStoreAdapter;
27import org.onosproject.common.app.ApplicationArchive;
Thomas Vachuskac65dd712015-11-04 17:19:10 -080028import org.onosproject.common.event.impl.TestEventDispatcher;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080029import org.onosproject.core.Application;
30import org.onosproject.core.ApplicationId;
31import org.onosproject.core.DefaultApplication;
32import org.onosproject.core.DefaultApplicationId;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080033
34import java.io.InputStream;
35import java.net.URI;
36import java.util.HashSet;
37import java.util.Optional;
38import java.util.Set;
39
Thomas Vachuskac65dd712015-11-04 17:19:10 -080040import static org.junit.Assert.*;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080041import static org.onosproject.app.ApplicationEvent.Type.*;
42import static org.onosproject.app.ApplicationState.ACTIVE;
43import static org.onosproject.app.ApplicationState.INSTALLED;
44import static org.onosproject.app.DefaultApplicationDescriptionTest.*;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070045import static org.onosproject.net.NetTestTools.injectEventDispatcher;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080046
47/**
48 * Test of the application manager implementation.
49 */
50public class ApplicationManagerTest {
51
52 public static final DefaultApplicationId APP_ID = new DefaultApplicationId(1, APP_NAME);
53
54 private ApplicationManager mgr = new ApplicationManager();
55 private ApplicationListener listener = new TestListener();
56
Thomas Vachuskac65dd712015-11-04 17:19:10 -080057 private boolean deactivated = false;
58
Thomas Vachuska02aeb032015-01-06 22:36:30 -080059 @Before
60 public void setUp() {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070061 injectEventDispatcher(mgr, new TestEventDispatcher());
Thomas Vachuska02aeb032015-01-06 22:36:30 -080062 mgr.featuresService = new TestFeaturesService();
63 mgr.store = new TestStore();
64 mgr.activate();
65 mgr.addListener(listener);
66 }
67
68 @After
69 public void tearDown() {
70 mgr.removeListener(listener);
71 mgr.deactivate();
72 }
73
74 private void validate(Application app) {
75 assertEquals("incorrect name", APP_NAME, app.id().name());
76 assertEquals("incorrect version", VER, app.version());
77 assertEquals("incorrect origin", ORIGIN, app.origin());
78
79 assertEquals("incorrect description", DESC, app.description());
80 assertEquals("incorrect features URI", FURL, app.featuresRepo().get());
81 assertEquals("incorrect features", FEATURES, app.features());
82 }
83
84 @Test
85 public void install() {
86 InputStream stream = ApplicationArchive.class.getResourceAsStream("app.zip");
87 Application app = mgr.install(stream);
88 validate(app);
89 assertEquals("incorrect features URI used", app.featuresRepo().get(),
90 ((TestFeaturesService) mgr.featuresService).uri);
91 assertEquals("incorrect app count", 1, mgr.getApplications().size());
92 assertEquals("incorrect app", app, mgr.getApplication(APP_ID));
93 assertEquals("incorrect app state", INSTALLED, mgr.getState(APP_ID));
Thomas Vachuskac65dd712015-11-04 17:19:10 -080094 mgr.registerDeactivateHook(app.id(), this::deactivateHook);
95 }
96
97 private void deactivateHook() {
98 deactivated = true;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080099 }
100
101 @Test
102 public void uninstall() {
103 install();
104 mgr.uninstall(APP_ID);
105 assertEquals("incorrect app count", 0, mgr.getApplications().size());
106 }
107
108 @Test
109 public void activate() {
110 install();
111 mgr.activate(APP_ID);
112 assertEquals("incorrect app state", ACTIVE, mgr.getState(APP_ID));
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800113 assertFalse("preDeactivate hook wrongly called", deactivated);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800114 }
115
116 @Test
117 public void deactivate() {
118 activate();
119 mgr.deactivate(APP_ID);
120 assertEquals("incorrect app state", INSTALLED, mgr.getState(APP_ID));
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800121 assertTrue("preDeactivate hook not called", deactivated);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800122 }
123
124
125 private class TestListener implements ApplicationListener {
126 private ApplicationEvent event;
127
128 @Override
129 public void event(ApplicationEvent event) {
130 this.event = event;
131 }
132 }
133
134 private class TestStore extends ApplicationStoreAdapter {
135
136 private Application app;
137 private ApplicationState state;
138
139 @Override
140 public Application create(InputStream appDescStream) {
Jian Lic35415d2016-01-14 17:22:31 -0800141 app = new DefaultApplication(APP_ID, VER, DESC, ORIGIN, CATEGORY,
142 URL, README, ICON, ROLE, PERMS,
Thomas Vachuska761f0042015-11-11 19:10:17 -0800143 Optional.of(FURL), FEATURES, ImmutableList.of());
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800144 state = INSTALLED;
145 delegate.notify(new ApplicationEvent(APP_INSTALLED, app));
146 return app;
147 }
148
149 @Override
150 public Set<Application> getApplications() {
151 return app != null ? ImmutableSet.of(app) : ImmutableSet.of();
152 }
153
154 @Override
155 public Application getApplication(ApplicationId appId) {
156 return app;
157 }
158
159 @Override
160 public void remove(ApplicationId appId) {
161 delegate.notify(new ApplicationEvent(APP_UNINSTALLED, app));
162 app = null;
163 state = null;
164 }
165
166 @Override
167 public ApplicationState getState(ApplicationId appId) {
168 return state;
169 }
170
171 @Override
172 public void activate(ApplicationId appId) {
173 state = ApplicationState.ACTIVE;
174 delegate.notify(new ApplicationEvent(APP_ACTIVATED, app));
175 }
176
177 @Override
178 public void deactivate(ApplicationId appId) {
179 state = INSTALLED;
180 delegate.notify(new ApplicationEvent(APP_DEACTIVATED, app));
181 }
Thomas Vachuska761f0042015-11-11 19:10:17 -0800182
183 @Override
184 public ApplicationId getId(String name) {
185 return new DefaultApplicationId(0, name);
186 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800187 }
188
189 private class TestFeaturesService extends FeaturesServiceAdapter {
190 private URI uri;
191 private Set<String> features = new HashSet<>();
192
193 @Override
194 public void addRepository(URI uri) throws Exception {
195 this.uri = uri;
196 }
197
198 @Override
199 public void removeRepository(URI uri) throws Exception {
200 this.uri = null;
201 }
202
203 @Override
204 public void installFeature(String name) throws Exception {
205 features.add(name);
206 }
207
208 @Override
209 public void uninstallFeature(String name) throws Exception {
210 features.remove(name);
211 }
212 }
213
214}