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