blob: 5528f269a9dd9b6d7d510dec2a35734f4946d0a9 [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;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080032
33import java.io.InputStream;
34import java.net.URI;
35import java.util.HashSet;
36import java.util.Optional;
37import java.util.Set;
38
Thomas Vachuskac65dd712015-11-04 17:19:10 -080039import static org.junit.Assert.*;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080040import static org.onosproject.app.ApplicationEvent.Type.*;
41import static org.onosproject.app.ApplicationState.ACTIVE;
42import static org.onosproject.app.ApplicationState.INSTALLED;
43import static org.onosproject.app.DefaultApplicationDescriptionTest.*;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070044import static org.onosproject.net.NetTestTools.injectEventDispatcher;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080045
46/**
47 * Test of the application manager implementation.
48 */
49public class ApplicationManagerTest {
50
51 public static final DefaultApplicationId APP_ID = new DefaultApplicationId(1, APP_NAME);
52
53 private ApplicationManager mgr = new ApplicationManager();
54 private ApplicationListener listener = new TestListener();
55
Thomas Vachuskac65dd712015-11-04 17:19:10 -080056 private boolean deactivated = false;
57
Thomas Vachuska02aeb032015-01-06 22:36:30 -080058 @Before
59 public void setUp() {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070060 injectEventDispatcher(mgr, new TestEventDispatcher());
Thomas Vachuska02aeb032015-01-06 22:36:30 -080061 mgr.featuresService = new TestFeaturesService();
62 mgr.store = new TestStore();
63 mgr.activate();
64 mgr.addListener(listener);
65 }
66
67 @After
68 public void tearDown() {
69 mgr.removeListener(listener);
70 mgr.deactivate();
71 }
72
73 private void validate(Application app) {
74 assertEquals("incorrect name", APP_NAME, app.id().name());
75 assertEquals("incorrect version", VER, app.version());
76 assertEquals("incorrect origin", ORIGIN, app.origin());
77
78 assertEquals("incorrect description", DESC, app.description());
79 assertEquals("incorrect features URI", FURL, app.featuresRepo().get());
80 assertEquals("incorrect features", FEATURES, app.features());
81 }
82
83 @Test
84 public void install() {
85 InputStream stream = ApplicationArchive.class.getResourceAsStream("app.zip");
86 Application app = mgr.install(stream);
87 validate(app);
88 assertEquals("incorrect features URI used", app.featuresRepo().get(),
89 ((TestFeaturesService) mgr.featuresService).uri);
90 assertEquals("incorrect app count", 1, mgr.getApplications().size());
91 assertEquals("incorrect app", app, mgr.getApplication(APP_ID));
92 assertEquals("incorrect app state", INSTALLED, mgr.getState(APP_ID));
Thomas Vachuskac65dd712015-11-04 17:19:10 -080093 mgr.registerDeactivateHook(app.id(), this::deactivateHook);
94 }
95
96 private void deactivateHook() {
97 deactivated = true;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080098 }
99
100 @Test
101 public void uninstall() {
102 install();
103 mgr.uninstall(APP_ID);
104 assertEquals("incorrect app count", 0, mgr.getApplications().size());
105 }
106
107 @Test
108 public void activate() {
109 install();
110 mgr.activate(APP_ID);
111 assertEquals("incorrect app state", ACTIVE, mgr.getState(APP_ID));
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800112 assertFalse("preDeactivate hook wrongly called", deactivated);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800113 }
114
115 @Test
116 public void deactivate() {
117 activate();
118 mgr.deactivate(APP_ID);
119 assertEquals("incorrect app state", INSTALLED, mgr.getState(APP_ID));
Thomas Vachuskac65dd712015-11-04 17:19:10 -0800120 assertTrue("preDeactivate hook not called", deactivated);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800121 }
122
123
124 private class TestListener implements ApplicationListener {
125 private ApplicationEvent event;
126
127 @Override
128 public void event(ApplicationEvent event) {
129 this.event = event;
130 }
131 }
132
133 private class TestStore extends ApplicationStoreAdapter {
134
135 private Application app;
136 private ApplicationState state;
137
138 @Override
139 public Application create(InputStream appDescStream) {
Ray Milkey47c95412017-09-15 10:40:48 -0700140 app = DefaultApplication.builder()
141 .withAppId(APP_ID)
142 .withVersion(VER)
143 .withTitle(TITLE)
144 .withDescription(DESC)
145 .withOrigin(ORIGIN)
146 .withCategory(CATEGORY)
147 .withUrl(URL)
148 .withReadme(README)
149 .withIcon(ICON)
150 .withRole(ROLE)
151 .withPermissions(PERMS)
152 .withFeaturesRepo(Optional.of(FURL))
153 .withFeatures(FEATURES)
154 .withRequiredApps(APPS)
155 .build();
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800156 state = INSTALLED;
157 delegate.notify(new ApplicationEvent(APP_INSTALLED, app));
158 return app;
159 }
160
161 @Override
162 public Set<Application> getApplications() {
163 return app != null ? ImmutableSet.of(app) : ImmutableSet.of();
164 }
165
166 @Override
167 public Application getApplication(ApplicationId appId) {
168 return app;
169 }
170
171 @Override
172 public void remove(ApplicationId appId) {
173 delegate.notify(new ApplicationEvent(APP_UNINSTALLED, app));
174 app = null;
175 state = null;
176 }
177
178 @Override
179 public ApplicationState getState(ApplicationId appId) {
180 return state;
181 }
182
183 @Override
184 public void activate(ApplicationId appId) {
185 state = ApplicationState.ACTIVE;
186 delegate.notify(new ApplicationEvent(APP_ACTIVATED, app));
187 }
188
189 @Override
190 public void deactivate(ApplicationId appId) {
191 state = INSTALLED;
192 delegate.notify(new ApplicationEvent(APP_DEACTIVATED, app));
193 }
Thomas Vachuska761f0042015-11-11 19:10:17 -0800194
195 @Override
196 public ApplicationId getId(String name) {
197 return new DefaultApplicationId(0, name);
198 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800199 }
200
201 private class TestFeaturesService extends FeaturesServiceAdapter {
202 private URI uri;
203 private Set<String> features = new HashSet<>();
204
205 @Override
206 public void addRepository(URI uri) throws Exception {
207 this.uri = uri;
208 }
209
210 @Override
211 public void removeRepository(URI uri) throws Exception {
212 this.uri = null;
213 }
214
215 @Override
216 public void installFeature(String name) throws Exception {
217 features.add(name);
218 }
219
220 @Override
221 public void uninstallFeature(String name) throws Exception {
222 features.remove(name);
223 }
224 }
225
Carmelo Cascone5bb59f22019-07-09 00:05:46 +0000226}