Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.app.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import org.junit.After; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onosproject.app.ApplicationEvent; |
| 23 | import org.onosproject.app.ApplicationListener; |
| 24 | import org.onosproject.app.ApplicationState; |
| 25 | import org.onosproject.app.ApplicationStoreAdapter; |
| 26 | import org.onosproject.common.app.ApplicationArchive; |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 27 | import org.onosproject.common.event.impl.TestEventDispatcher; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 28 | import org.onosproject.core.Application; |
| 29 | import org.onosproject.core.ApplicationId; |
| 30 | import org.onosproject.core.DefaultApplication; |
| 31 | import org.onosproject.core.DefaultApplicationId; |
Arnav Jain | efb57e5 | 2019-07-09 14:24:10 -0700 | [diff] [blame] | 32 | import org.onosproject.core.Version; |
| 33 | import org.onosproject.core.VersionServiceAdapter; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 34 | |
| 35 | import java.io.InputStream; |
| 36 | import java.net.URI; |
| 37 | import java.util.HashSet; |
| 38 | import java.util.Optional; |
| 39 | import java.util.Set; |
| 40 | |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 41 | import static org.junit.Assert.*; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 42 | import static org.onosproject.app.ApplicationEvent.Type.*; |
| 43 | import static org.onosproject.app.ApplicationState.ACTIVE; |
| 44 | import static org.onosproject.app.ApplicationState.INSTALLED; |
| 45 | import static org.onosproject.app.DefaultApplicationDescriptionTest.*; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 46 | import static org.onosproject.net.NetTestTools.injectEventDispatcher; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Test of the application manager implementation. |
| 50 | */ |
| 51 | public class ApplicationManagerTest { |
| 52 | |
| 53 | public static final DefaultApplicationId APP_ID = new DefaultApplicationId(1, APP_NAME); |
Arnav Jain | efb57e5 | 2019-07-09 14:24:10 -0700 | [diff] [blame] | 54 | private static final Version CORE_VERSION = Version.version(2, 1, "0", ""); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 55 | |
| 56 | private ApplicationManager mgr = new ApplicationManager(); |
| 57 | private ApplicationListener listener = new TestListener(); |
| 58 | |
Arnav Jain | efb57e5 | 2019-07-09 14:24:10 -0700 | [diff] [blame] | 59 | |
| 60 | |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 61 | private boolean deactivated = false; |
| 62 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 63 | @Before |
| 64 | public void setUp() { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 65 | injectEventDispatcher(mgr, new TestEventDispatcher()); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 66 | 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 Jain | efb57e5 | 2019-07-09 14:24:10 -0700 | [diff] [blame] | 89 | 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 Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 105 | 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 Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 114 | mgr.registerDeactivateHook(app.id(), this::deactivateHook); |
| 115 | } |
| 116 | |
| 117 | private void deactivateHook() { |
| 118 | deactivated = true; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 119 | } |
| 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 Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 133 | assertFalse("preDeactivate hook wrongly called", deactivated); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 134 | } |
| 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 Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 141 | assertTrue("preDeactivate hook not called", deactivated); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 142 | } |
| 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 Milkey | 47c9541 | 2017-09-15 10:40:48 -0700 | [diff] [blame] | 161 | 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 Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 177 | 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 Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 215 | |
| 216 | @Override |
| 217 | public ApplicationId getId(String name) { |
| 218 | return new DefaultApplicationId(0, name); |
| 219 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 220 | } |
| 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 Jain | efb57e5 | 2019-07-09 14:24:10 -0700 | [diff] [blame] | 247 | } |