blob: 8c835c195a77863dd46e7f77c0125391f95527bb [file] [log] [blame]
Thomas Vachuska02aeb032015-01-06 22:36:30 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska02aeb032015-01-06 22:36:30 -08003 *
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
18import com.google.common.collect.ImmutableSet;
19import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
22import org.onosproject.app.ApplicationEvent;
23import org.onosproject.app.ApplicationListener;
24import org.onosproject.app.ApplicationState;
25import org.onosproject.app.ApplicationStoreAdapter;
26import org.onosproject.common.app.ApplicationArchive;
Thomas Vachuskac65dd712015-11-04 17:19:10 -080027import org.onosproject.common.event.impl.TestEventDispatcher;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080028import org.onosproject.core.Application;
29import org.onosproject.core.ApplicationId;
30import org.onosproject.core.DefaultApplication;
31import org.onosproject.core.DefaultApplicationId;
Arnav Jainefb57e52019-07-09 14:24:10 -070032import org.onosproject.core.Version;
33import org.onosproject.core.VersionServiceAdapter;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080034
35import java.io.InputStream;
36import java.net.URI;
37import java.util.HashSet;
38import java.util.Optional;
39import java.util.Set;
40
Thomas Vachuskac65dd712015-11-04 17:19:10 -080041import static org.junit.Assert.*;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080042import static org.onosproject.app.ApplicationEvent.Type.*;
43import static org.onosproject.app.ApplicationState.ACTIVE;
44import static org.onosproject.app.ApplicationState.INSTALLED;
45import static org.onosproject.app.DefaultApplicationDescriptionTest.*;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070046import static org.onosproject.net.NetTestTools.injectEventDispatcher;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080047
48/**
49 * Test of the application manager implementation.
50 */
51public class ApplicationManagerTest {
52
53 public static final DefaultApplicationId APP_ID = new DefaultApplicationId(1, APP_NAME);
Arnav Jainefb57e52019-07-09 14:24:10 -070054 private static final Version CORE_VERSION = Version.version(2, 1, "0", "");
Thomas Vachuska02aeb032015-01-06 22:36:30 -080055
56 private ApplicationManager mgr = new ApplicationManager();
57 private ApplicationListener listener = new TestListener();
58
Arnav Jainefb57e52019-07-09 14:24:10 -070059
60
Thomas Vachuskac65dd712015-11-04 17:19:10 -080061 private boolean deactivated = false;
62
Thomas Vachuska02aeb032015-01-06 22:36:30 -080063 @Before
64 public void setUp() {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070065 injectEventDispatcher(mgr, new TestEventDispatcher());
Thomas Vachuska02aeb032015-01-06 22:36:30 -080066 mgr.featuresService = new TestFeaturesService();
67 mgr.store = new TestStore();
68 mgr.activate();
69 mgr.addListener(listener);
70 }
71
72 @After
73 public void tearDown() {
74 mgr.removeListener(listener);
75 mgr.deactivate();
76 }
77
78 private void validate(Application app) {
79 assertEquals("incorrect name", APP_NAME, app.id().name());
80 assertEquals("incorrect version", VER, app.version());
81 assertEquals("incorrect origin", ORIGIN, app.origin());
82
83 assertEquals("incorrect description", DESC, app.description());
84 assertEquals("incorrect features URI", FURL, app.featuresRepo().get());
85 assertEquals("incorrect features", FEATURES, app.features());
86 }
87
Arnav Jainefb57e52019-07-09 14:24:10 -070088 private static class TestVersionService extends VersionServiceAdapter {
89
90 @Override
91 public Version version() {
92 return CORE_VERSION;
93 }
94 }
95
96 @Test
Thomas Vachuska02aeb032015-01-06 22:36:30 -080097 public void install() {
98 InputStream stream = ApplicationArchive.class.getResourceAsStream("app.zip");
99 Application app = mgr.install(stream);
100 validate(app);
101 assertEquals("incorrect features URI used", app.featuresRepo().get(),
102 ((TestFeaturesService) mgr.featuresService).uri);
103 assertEquals("incorrect app count", 1, mgr.getApplications().size());
104 assertEquals("incorrect app", app, mgr.getApplication(APP_ID));
105 assertEquals("incorrect app state", INSTALLED, mgr.getState(APP_ID));
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800106 mgr.registerDeactivateHook(app.id(), this::deactivateHook);
107 }
108
109 private void deactivateHook() {
110 deactivated = true;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800111 }
112
113 @Test
114 public void uninstall() {
115 install();
116 mgr.uninstall(APP_ID);
117 assertEquals("incorrect app count", 0, mgr.getApplications().size());
118 }
119
120 @Test
121 public void activate() {
122 install();
123 mgr.activate(APP_ID);
124 assertEquals("incorrect app state", ACTIVE, mgr.getState(APP_ID));
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800125 assertFalse("preDeactivate hook wrongly called", deactivated);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800126 }
127
128 @Test
129 public void deactivate() {
130 activate();
131 mgr.deactivate(APP_ID);
132 assertEquals("incorrect app state", INSTALLED, mgr.getState(APP_ID));
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800133 assertTrue("preDeactivate hook not called", deactivated);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800134 }
135
136
137 private class TestListener implements ApplicationListener {
138 private ApplicationEvent event;
139
140 @Override
141 public void event(ApplicationEvent event) {
142 this.event = event;
143 }
144 }
145
146 private class TestStore extends ApplicationStoreAdapter {
147
148 private Application app;
149 private ApplicationState state;
150
151 @Override
152 public Application create(InputStream appDescStream) {
Ray Milkey47c95412017-09-15 10:40:48 -0700153 app = DefaultApplication.builder()
154 .withAppId(APP_ID)
155 .withVersion(VER)
156 .withTitle(TITLE)
157 .withDescription(DESC)
158 .withOrigin(ORIGIN)
159 .withCategory(CATEGORY)
160 .withUrl(URL)
161 .withReadme(README)
162 .withIcon(ICON)
163 .withRole(ROLE)
164 .withPermissions(PERMS)
165 .withFeaturesRepo(Optional.of(FURL))
166 .withFeatures(FEATURES)
167 .withRequiredApps(APPS)
168 .build();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800169 state = INSTALLED;
170 delegate.notify(new ApplicationEvent(APP_INSTALLED, app));
171 return app;
172 }
173
174 @Override
175 public Set<Application> getApplications() {
176 return app != null ? ImmutableSet.of(app) : ImmutableSet.of();
177 }
178
179 @Override
180 public Application getApplication(ApplicationId appId) {
181 return app;
182 }
183
184 @Override
185 public void remove(ApplicationId appId) {
186 delegate.notify(new ApplicationEvent(APP_UNINSTALLED, app));
187 app = null;
188 state = null;
189 }
190
191 @Override
192 public ApplicationState getState(ApplicationId appId) {
193 return state;
194 }
195
196 @Override
197 public void activate(ApplicationId appId) {
198 state = ApplicationState.ACTIVE;
199 delegate.notify(new ApplicationEvent(APP_ACTIVATED, app));
200 }
201
202 @Override
203 public void deactivate(ApplicationId appId) {
204 state = INSTALLED;
205 delegate.notify(new ApplicationEvent(APP_DEACTIVATED, app));
206 }
Thomas Vachuska761f0042015-11-11 19:10:17 -0800207
208 @Override
209 public ApplicationId getId(String name) {
210 return new DefaultApplicationId(0, name);
211 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800212 }
213
214 private class TestFeaturesService extends FeaturesServiceAdapter {
215 private URI uri;
216 private Set<String> features = new HashSet<>();
217
218 @Override
219 public void addRepository(URI uri) throws Exception {
220 this.uri = uri;
221 }
222
223 @Override
224 public void removeRepository(URI uri) throws Exception {
225 this.uri = null;
226 }
227
228 @Override
229 public void installFeature(String name) throws Exception {
230 features.add(name);
231 }
232
233 @Override
234 public void uninstallFeature(String name) throws Exception {
235 features.remove(name);
236 }
237 }
238
Arnav Jainefb57e52019-07-09 14:24:10 -0700239}