blob: 1ce31ac3339829b7c4e51754a53f553e5f8d9eb6 [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;
27import org.onosproject.core.Application;
28import org.onosproject.core.ApplicationId;
29import org.onosproject.core.DefaultApplication;
30import org.onosproject.core.DefaultApplicationId;
Thomas Vachuska36002e62015-05-19 16:12:29 -070031import org.onosproject.common.event.impl.TestEventDispatcher;
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
39import static org.junit.Assert.assertEquals;
40import 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
56 @Before
57 public void setUp() {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070058 injectEventDispatcher(mgr, new TestEventDispatcher());
Thomas Vachuska02aeb032015-01-06 22:36:30 -080059 mgr.featuresService = new TestFeaturesService();
60 mgr.store = new TestStore();
61 mgr.activate();
62 mgr.addListener(listener);
63 }
64
65 @After
66 public void tearDown() {
67 mgr.removeListener(listener);
68 mgr.deactivate();
69 }
70
71 private void validate(Application app) {
72 assertEquals("incorrect name", APP_NAME, app.id().name());
73 assertEquals("incorrect version", VER, app.version());
74 assertEquals("incorrect origin", ORIGIN, app.origin());
75
76 assertEquals("incorrect description", DESC, app.description());
77 assertEquals("incorrect features URI", FURL, app.featuresRepo().get());
78 assertEquals("incorrect features", FEATURES, app.features());
79 }
80
81 @Test
82 public void install() {
83 InputStream stream = ApplicationArchive.class.getResourceAsStream("app.zip");
84 Application app = mgr.install(stream);
85 validate(app);
86 assertEquals("incorrect features URI used", app.featuresRepo().get(),
87 ((TestFeaturesService) mgr.featuresService).uri);
88 assertEquals("incorrect app count", 1, mgr.getApplications().size());
89 assertEquals("incorrect app", app, mgr.getApplication(APP_ID));
90 assertEquals("incorrect app state", INSTALLED, mgr.getState(APP_ID));
91 }
92
93 @Test
94 public void uninstall() {
95 install();
96 mgr.uninstall(APP_ID);
97 assertEquals("incorrect app count", 0, mgr.getApplications().size());
98 }
99
100 @Test
101 public void activate() {
102 install();
103 mgr.activate(APP_ID);
104 assertEquals("incorrect app state", ACTIVE, mgr.getState(APP_ID));
105 }
106
107 @Test
108 public void deactivate() {
109 activate();
110 mgr.deactivate(APP_ID);
111 assertEquals("incorrect app state", INSTALLED, mgr.getState(APP_ID));
112 }
113
114
115 private class TestListener implements ApplicationListener {
116 private ApplicationEvent event;
117
118 @Override
119 public void event(ApplicationEvent event) {
120 this.event = event;
121 }
122 }
123
124 private class TestStore extends ApplicationStoreAdapter {
125
126 private Application app;
127 private ApplicationState state;
128
129 @Override
130 public Application create(InputStream appDescStream) {
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +0900131 app = new DefaultApplication(APP_ID, VER, DESC, ORIGIN, ROLE, PERMS,
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800132 Optional.of(FURL), FEATURES);
133 state = INSTALLED;
134 delegate.notify(new ApplicationEvent(APP_INSTALLED, app));
135 return app;
136 }
137
138 @Override
139 public Set<Application> getApplications() {
140 return app != null ? ImmutableSet.of(app) : ImmutableSet.of();
141 }
142
143 @Override
144 public Application getApplication(ApplicationId appId) {
145 return app;
146 }
147
148 @Override
149 public void remove(ApplicationId appId) {
150 delegate.notify(new ApplicationEvent(APP_UNINSTALLED, app));
151 app = null;
152 state = null;
153 }
154
155 @Override
156 public ApplicationState getState(ApplicationId appId) {
157 return state;
158 }
159
160 @Override
161 public void activate(ApplicationId appId) {
162 state = ApplicationState.ACTIVE;
163 delegate.notify(new ApplicationEvent(APP_ACTIVATED, app));
164 }
165
166 @Override
167 public void deactivate(ApplicationId appId) {
168 state = INSTALLED;
169 delegate.notify(new ApplicationEvent(APP_DEACTIVATED, app));
170 }
171 }
172
173 private class TestFeaturesService extends FeaturesServiceAdapter {
174 private URI uri;
175 private Set<String> features = new HashSet<>();
176
177 @Override
178 public void addRepository(URI uri) throws Exception {
179 this.uri = uri;
180 }
181
182 @Override
183 public void removeRepository(URI uri) throws Exception {
184 this.uri = null;
185 }
186
187 @Override
188 public void installFeature(String name) throws Exception {
189 features.add(name);
190 }
191
192 @Override
193 public void uninstallFeature(String name) throws Exception {
194 features.remove(name);
195 }
196 }
197
198}