blob: a99fd2160e58722faa5679ca61820deae574ba44 [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) {
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +0900141 app = new DefaultApplication(APP_ID, VER, DESC, ORIGIN, ROLE, PERMS,
Thomas Vachuska761f0042015-11-11 19:10:17 -0800142 Optional.of(FURL), FEATURES, ImmutableList.of());
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800143 state = INSTALLED;
144 delegate.notify(new ApplicationEvent(APP_INSTALLED, app));
145 return app;
146 }
147
148 @Override
149 public Set<Application> getApplications() {
150 return app != null ? ImmutableSet.of(app) : ImmutableSet.of();
151 }
152
153 @Override
154 public Application getApplication(ApplicationId appId) {
155 return app;
156 }
157
158 @Override
159 public void remove(ApplicationId appId) {
160 delegate.notify(new ApplicationEvent(APP_UNINSTALLED, app));
161 app = null;
162 state = null;
163 }
164
165 @Override
166 public ApplicationState getState(ApplicationId appId) {
167 return state;
168 }
169
170 @Override
171 public void activate(ApplicationId appId) {
172 state = ApplicationState.ACTIVE;
173 delegate.notify(new ApplicationEvent(APP_ACTIVATED, app));
174 }
175
176 @Override
177 public void deactivate(ApplicationId appId) {
178 state = INSTALLED;
179 delegate.notify(new ApplicationEvent(APP_DEACTIVATED, app));
180 }
Thomas Vachuska761f0042015-11-11 19:10:17 -0800181
182 @Override
183 public ApplicationId getId(String name) {
184 return new DefaultApplicationId(0, name);
185 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800186 }
187
188 private class TestFeaturesService extends FeaturesServiceAdapter {
189 private URI uri;
190 private Set<String> features = new HashSet<>();
191
192 @Override
193 public void addRepository(URI uri) throws Exception {
194 this.uri = uri;
195 }
196
197 @Override
198 public void removeRepository(URI uri) throws Exception {
199 this.uri = null;
200 }
201
202 @Override
203 public void installFeature(String name) throws Exception {
204 features.add(name);
205 }
206
207 @Override
208 public void uninstallFeature(String name) throws Exception {
209 features.remove(name);
210 }
211 }
212
213}