blob: 203abb78017ae610f6f01ee88055466254f3c953 [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
88 @Test
Arnav Jainefb57e52019-07-09 14:24:10 -070089 public void testGetRegisteredApps() {
90 mgr.versionService = new TestVersionService();
91 Set<Application> apps = mgr.getRegisteredApplications();
92 System.out.println(apps);
93 assertFalse("SET contains less Apps than it should", apps.size() < 158);
94 }
95
96 private static class TestVersionService extends VersionServiceAdapter {
97
98 @Override
99 public Version version() {
100 return CORE_VERSION;
101 }
102 }
103
104 @Test
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800105 public void install() {
106 InputStream stream = ApplicationArchive.class.getResourceAsStream("app.zip");
107 Application app = mgr.install(stream);
108 validate(app);
109 assertEquals("incorrect features URI used", app.featuresRepo().get(),
110 ((TestFeaturesService) mgr.featuresService).uri);
111 assertEquals("incorrect app count", 1, mgr.getApplications().size());
112 assertEquals("incorrect app", app, mgr.getApplication(APP_ID));
113 assertEquals("incorrect app state", INSTALLED, mgr.getState(APP_ID));
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800114 mgr.registerDeactivateHook(app.id(), this::deactivateHook);
115 }
116
117 private void deactivateHook() {
118 deactivated = true;
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800119 }
120
121 @Test
122 public void uninstall() {
123 install();
124 mgr.uninstall(APP_ID);
125 assertEquals("incorrect app count", 0, mgr.getApplications().size());
126 }
127
128 @Test
129 public void activate() {
130 install();
131 mgr.activate(APP_ID);
132 assertEquals("incorrect app state", ACTIVE, mgr.getState(APP_ID));
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800133 assertFalse("preDeactivate hook wrongly called", deactivated);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800134 }
135
136 @Test
137 public void deactivate() {
138 activate();
139 mgr.deactivate(APP_ID);
140 assertEquals("incorrect app state", INSTALLED, mgr.getState(APP_ID));
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800141 assertTrue("preDeactivate hook not called", deactivated);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800142 }
143
144
145 private class TestListener implements ApplicationListener {
146 private ApplicationEvent event;
147
148 @Override
149 public void event(ApplicationEvent event) {
150 this.event = event;
151 }
152 }
153
154 private class TestStore extends ApplicationStoreAdapter {
155
156 private Application app;
157 private ApplicationState state;
158
159 @Override
160 public Application create(InputStream appDescStream) {
Ray Milkey47c95412017-09-15 10:40:48 -0700161 app = DefaultApplication.builder()
162 .withAppId(APP_ID)
163 .withVersion(VER)
164 .withTitle(TITLE)
165 .withDescription(DESC)
166 .withOrigin(ORIGIN)
167 .withCategory(CATEGORY)
168 .withUrl(URL)
169 .withReadme(README)
170 .withIcon(ICON)
171 .withRole(ROLE)
172 .withPermissions(PERMS)
173 .withFeaturesRepo(Optional.of(FURL))
174 .withFeatures(FEATURES)
175 .withRequiredApps(APPS)
176 .build();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800177 state = INSTALLED;
178 delegate.notify(new ApplicationEvent(APP_INSTALLED, app));
179 return app;
180 }
181
182 @Override
183 public Set<Application> getApplications() {
184 return app != null ? ImmutableSet.of(app) : ImmutableSet.of();
185 }
186
187 @Override
188 public Application getApplication(ApplicationId appId) {
189 return app;
190 }
191
192 @Override
193 public void remove(ApplicationId appId) {
194 delegate.notify(new ApplicationEvent(APP_UNINSTALLED, app));
195 app = null;
196 state = null;
197 }
198
199 @Override
200 public ApplicationState getState(ApplicationId appId) {
201 return state;
202 }
203
204 @Override
205 public void activate(ApplicationId appId) {
206 state = ApplicationState.ACTIVE;
207 delegate.notify(new ApplicationEvent(APP_ACTIVATED, app));
208 }
209
210 @Override
211 public void deactivate(ApplicationId appId) {
212 state = INSTALLED;
213 delegate.notify(new ApplicationEvent(APP_DEACTIVATED, app));
214 }
Thomas Vachuska761f0042015-11-11 19:10:17 -0800215
216 @Override
217 public ApplicationId getId(String name) {
218 return new DefaultApplicationId(0, name);
219 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800220 }
221
222 private class TestFeaturesService extends FeaturesServiceAdapter {
223 private URI uri;
224 private Set<String> features = new HashSet<>();
225
226 @Override
227 public void addRepository(URI uri) throws Exception {
228 this.uri = uri;
229 }
230
231 @Override
232 public void removeRepository(URI uri) throws Exception {
233 this.uri = null;
234 }
235
236 @Override
237 public void installFeature(String name) throws Exception {
238 features.add(name);
239 }
240
241 @Override
242 public void uninstallFeature(String name) throws Exception {
243 features.remove(name);
244 }
245 }
246
Arnav Jainefb57e52019-07-09 14:24:10 -0700247}