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