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; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 32 | |
| 33 | import java.io.InputStream; |
| 34 | import java.net.URI; |
| 35 | import java.util.HashSet; |
| 36 | import java.util.Optional; |
| 37 | import java.util.Set; |
| 38 | |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 39 | import static org.junit.Assert.*; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 40 | import static org.onosproject.app.ApplicationEvent.Type.*; |
| 41 | import static org.onosproject.app.ApplicationState.ACTIVE; |
| 42 | import static org.onosproject.app.ApplicationState.INSTALLED; |
| 43 | import static org.onosproject.app.DefaultApplicationDescriptionTest.*; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 44 | import static org.onosproject.net.NetTestTools.injectEventDispatcher; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Test of the application manager implementation. |
| 48 | */ |
| 49 | public 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 Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 56 | private boolean deactivated = false; |
| 57 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 58 | @Before |
| 59 | public void setUp() { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 60 | injectEventDispatcher(mgr, new TestEventDispatcher()); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 61 | 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 Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 93 | mgr.registerDeactivateHook(app.id(), this::deactivateHook); |
| 94 | } |
| 95 | |
| 96 | private void deactivateHook() { |
| 97 | deactivated = true; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 98 | } |
| 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 Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 112 | assertFalse("preDeactivate hook wrongly called", deactivated); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 113 | } |
| 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 Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 120 | assertTrue("preDeactivate hook not called", deactivated); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 121 | } |
| 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 Milkey | 47c9541 | 2017-09-15 10:40:48 -0700 | [diff] [blame] | 140 | 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 Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 156 | 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 Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 194 | |
| 195 | @Override |
| 196 | public ApplicationId getId(String name) { |
| 197 | return new DefaultApplicationId(0, name); |
| 198 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 199 | } |
| 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 | |
Ray Milkey | 47c9541 | 2017-09-15 10:40:48 -0700 | [diff] [blame] | 226 | } |