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 | |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onosproject.app.ApplicationEvent; |
| 24 | import org.onosproject.app.ApplicationListener; |
| 25 | import org.onosproject.app.ApplicationState; |
| 26 | import org.onosproject.app.ApplicationStoreAdapter; |
| 27 | import org.onosproject.common.app.ApplicationArchive; |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 28 | import org.onosproject.common.event.impl.TestEventDispatcher; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 29 | import org.onosproject.core.Application; |
| 30 | import org.onosproject.core.ApplicationId; |
| 31 | import org.onosproject.core.DefaultApplication; |
| 32 | import org.onosproject.core.DefaultApplicationId; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 33 | |
| 34 | import java.io.InputStream; |
| 35 | import java.net.URI; |
| 36 | import java.util.HashSet; |
| 37 | import java.util.Optional; |
| 38 | import java.util.Set; |
| 39 | |
Thomas Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 40 | import static org.junit.Assert.*; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 41 | import static org.onosproject.app.ApplicationEvent.Type.*; |
| 42 | import static org.onosproject.app.ApplicationState.ACTIVE; |
| 43 | import static org.onosproject.app.ApplicationState.INSTALLED; |
| 44 | import static org.onosproject.app.DefaultApplicationDescriptionTest.*; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 45 | import static org.onosproject.net.NetTestTools.injectEventDispatcher; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Test of the application manager implementation. |
| 49 | */ |
| 50 | public 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 Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 57 | private boolean deactivated = false; |
| 58 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 59 | @Before |
| 60 | public void setUp() { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 61 | injectEventDispatcher(mgr, new TestEventDispatcher()); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 62 | 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 Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 94 | mgr.registerDeactivateHook(app.id(), this::deactivateHook); |
| 95 | } |
| 96 | |
| 97 | private void deactivateHook() { |
| 98 | deactivated = true; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 99 | } |
| 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 Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 113 | assertFalse("preDeactivate hook wrongly called", deactivated); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 114 | } |
| 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 Vachuska | c65dd71 | 2015-11-04 17:19:10 -0800 | [diff] [blame] | 121 | assertTrue("preDeactivate hook not called", deactivated); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 122 | } |
| 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) { |
Simon Hunt | afae2f7 | 2016-03-04 21:18:23 -0800 | [diff] [blame] | 141 | app = new DefaultApplication(APP_ID, VER, TITLE, DESC, ORIGIN, CATEGORY, |
Jian Li | c35415d | 2016-01-14 17:22:31 -0800 | [diff] [blame] | 142 | URL, README, ICON, ROLE, PERMS, |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 143 | Optional.of(FURL), FEATURES, ImmutableList.of()); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 144 | state = INSTALLED; |
| 145 | delegate.notify(new ApplicationEvent(APP_INSTALLED, app)); |
| 146 | return app; |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public Set<Application> getApplications() { |
| 151 | return app != null ? ImmutableSet.of(app) : ImmutableSet.of(); |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public Application getApplication(ApplicationId appId) { |
| 156 | return app; |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void remove(ApplicationId appId) { |
| 161 | delegate.notify(new ApplicationEvent(APP_UNINSTALLED, app)); |
| 162 | app = null; |
| 163 | state = null; |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public ApplicationState getState(ApplicationId appId) { |
| 168 | return state; |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public void activate(ApplicationId appId) { |
| 173 | state = ApplicationState.ACTIVE; |
| 174 | delegate.notify(new ApplicationEvent(APP_ACTIVATED, app)); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public void deactivate(ApplicationId appId) { |
| 179 | state = INSTALLED; |
| 180 | delegate.notify(new ApplicationEvent(APP_DEACTIVATED, app)); |
| 181 | } |
Thomas Vachuska | 761f004 | 2015-11-11 19:10:17 -0800 | [diff] [blame] | 182 | |
| 183 | @Override |
| 184 | public ApplicationId getId(String name) { |
| 185 | return new DefaultApplicationId(0, name); |
| 186 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | private class TestFeaturesService extends FeaturesServiceAdapter { |
| 190 | private URI uri; |
| 191 | private Set<String> features = new HashSet<>(); |
| 192 | |
| 193 | @Override |
| 194 | public void addRepository(URI uri) throws Exception { |
| 195 | this.uri = uri; |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public void removeRepository(URI uri) throws Exception { |
| 200 | this.uri = null; |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public void installFeature(String name) throws Exception { |
| 205 | features.add(name); |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public void uninstallFeature(String name) throws Exception { |
| 210 | features.remove(name); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | } |