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 | */ |
Thomas Vachuska | c97aa61 | 2015-06-23 16:00:18 -0700 | [diff] [blame^] | 16 | package org.onosproject.store.trivial; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import org.junit.After; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
Thomas Vachuska | e18a330 | 2015-06-23 12:48:28 -0700 | [diff] [blame] | 22 | import org.onlab.util.Tools; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 23 | import org.onosproject.app.ApplicationEvent; |
| 24 | import org.onosproject.app.ApplicationStoreDelegate; |
| 25 | import org.onosproject.common.app.ApplicationArchive; |
| 26 | import org.onosproject.core.Application; |
| 27 | import org.onosproject.core.ApplicationId; |
Changhoon Yoon | bdeb88a | 2015-05-12 20:35:31 +0900 | [diff] [blame] | 28 | import org.onosproject.core.Permission; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 29 | import org.onosproject.core.ApplicationIdStoreAdapter; |
| 30 | import org.onosproject.core.DefaultApplicationId; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 31 | |
Thomas Vachuska | e18a330 | 2015-06-23 12:48:28 -0700 | [diff] [blame] | 32 | import java.io.File; |
| 33 | import java.io.IOException; |
| 34 | import java.util.Random; |
| 35 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 36 | import static org.junit.Assert.assertEquals; |
Changhoon Yoon | bdeb88a | 2015-05-12 20:35:31 +0900 | [diff] [blame] | 37 | import static org.onosproject.app.ApplicationEvent.Type.APP_INSTALLED; |
| 38 | import static org.onosproject.app.ApplicationEvent.Type.APP_DEACTIVATED; |
| 39 | import static org.onosproject.app.ApplicationEvent.Type.APP_ACTIVATED; |
| 40 | import static org.onosproject.app.ApplicationEvent.Type.APP_UNINSTALLED; |
| 41 | import static org.onosproject.app.ApplicationEvent.Type.APP_PERMISSIONS_CHANGED; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 42 | import static org.onosproject.app.ApplicationState.ACTIVE; |
| 43 | import static org.onosproject.app.ApplicationState.INSTALLED; |
| 44 | |
| 45 | /** |
| 46 | * Test of the trivial application store implementation. |
| 47 | */ |
| 48 | public class SimpleApplicationStoreTest { |
| 49 | |
Thomas Vachuska | e18a330 | 2015-06-23 12:48:28 -0700 | [diff] [blame] | 50 | static final String ROOT = "/tmp/app-junit/"; |
| 51 | static final String STORE = ROOT + new Random().nextInt(1000) + "/foo"; |
| 52 | |
| 53 | private TestApplicationStore store = new TestApplicationStore(); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 54 | private TestDelegate delegate = new TestDelegate(); |
Thomas Vachuska | e18a330 | 2015-06-23 12:48:28 -0700 | [diff] [blame] | 55 | private static final Object LOCK = new Object(); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 56 | |
| 57 | @Before |
| 58 | public void setUp() { |
| 59 | store.idStore = new TestIdStore(); |
Thomas Vachuska | e18a330 | 2015-06-23 12:48:28 -0700 | [diff] [blame] | 60 | store.setRootPath(STORE); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 61 | store.setDelegate(delegate); |
| 62 | store.activate(); |
| 63 | } |
| 64 | |
| 65 | @After |
Thomas Vachuska | e18a330 | 2015-06-23 12:48:28 -0700 | [diff] [blame] | 66 | public void tearDown() throws IOException { |
| 67 | if (new File(ROOT).exists()) { |
| 68 | Tools.removeDirectory(ROOT); |
| 69 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 70 | store.deactivate(); |
| 71 | } |
| 72 | |
| 73 | private Application createTestApp() { |
Thomas Vachuska | e18a330 | 2015-06-23 12:48:28 -0700 | [diff] [blame] | 74 | synchronized (LOCK) { |
| 75 | return store.create(ApplicationArchive.class.getResourceAsStream("app.zip")); |
| 76 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Test |
| 80 | public void create() { |
| 81 | Application app = createTestApp(); |
| 82 | assertEquals("incorrect name", "org.foo.app", app.id().name()); |
| 83 | assertEquals("incorrect app count", 1, store.getApplications().size()); |
| 84 | assertEquals("incorrect app", app, store.getApplication(app.id())); |
| 85 | assertEquals("incorrect app state", INSTALLED, store.getState(app.id())); |
| 86 | assertEquals("incorrect event type", APP_INSTALLED, delegate.event.type()); |
| 87 | assertEquals("incorrect event app", app, delegate.event.subject()); |
| 88 | } |
| 89 | |
| 90 | @Test |
| 91 | public void remove() { |
| 92 | Application app = createTestApp(); |
| 93 | store.remove(app.id()); |
| 94 | assertEquals("incorrect app count", 0, store.getApplications().size()); |
| 95 | assertEquals("incorrect event type", APP_UNINSTALLED, delegate.event.type()); |
| 96 | assertEquals("incorrect event app", app, delegate.event.subject()); |
| 97 | } |
| 98 | |
| 99 | @Test |
| 100 | public void activate() { |
| 101 | Application app = createTestApp(); |
| 102 | store.activate(app.id()); |
| 103 | assertEquals("incorrect app count", 1, store.getApplications().size()); |
| 104 | assertEquals("incorrect app state", ACTIVE, store.getState(app.id())); |
| 105 | assertEquals("incorrect event type", APP_ACTIVATED, delegate.event.type()); |
| 106 | assertEquals("incorrect event app", app, delegate.event.subject()); |
| 107 | } |
| 108 | |
| 109 | @Test |
| 110 | public void deactivate() { |
| 111 | Application app = createTestApp(); |
| 112 | store.deactivate(app.id()); |
| 113 | assertEquals("incorrect app count", 1, store.getApplications().size()); |
| 114 | assertEquals("incorrect app state", INSTALLED, store.getState(app.id())); |
| 115 | assertEquals("incorrect event type", APP_DEACTIVATED, delegate.event.type()); |
| 116 | assertEquals("incorrect event app", app, delegate.event.subject()); |
| 117 | } |
| 118 | |
| 119 | @Test |
| 120 | public void permissions() { |
| 121 | Application app = createTestApp(); |
Changhoon Yoon | a7841ed | 2015-05-15 02:51:08 +0900 | [diff] [blame] | 122 | ImmutableSet<Permission> permissions = ImmutableSet.of(Permission.FLOWRULE_WRITE); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 123 | store.setPermissions(app.id(), permissions); |
| 124 | assertEquals("incorrect app perms", 1, store.getPermissions(app.id()).size()); |
| 125 | assertEquals("incorrect app state", INSTALLED, store.getState(app.id())); |
| 126 | assertEquals("incorrect event type", APP_PERMISSIONS_CHANGED, delegate.event.type()); |
| 127 | assertEquals("incorrect event app", app, delegate.event.subject()); |
| 128 | } |
| 129 | |
| 130 | private class TestIdStore extends ApplicationIdStoreAdapter { |
| 131 | @Override |
| 132 | public ApplicationId registerApplication(String name) { |
| 133 | return new DefaultApplicationId(1, name); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public ApplicationId getAppId(String name) { |
| 138 | return new DefaultApplicationId(1, name); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | private class TestDelegate implements ApplicationStoreDelegate { |
| 143 | private ApplicationEvent event; |
| 144 | |
| 145 | @Override |
| 146 | public void notify(ApplicationEvent event) { |
| 147 | this.event = event; |
| 148 | } |
| 149 | } |
Thomas Vachuska | e18a330 | 2015-06-23 12:48:28 -0700 | [diff] [blame] | 150 | |
| 151 | private class TestApplicationStore extends SimpleApplicationStore { |
| 152 | @Override |
| 153 | public void setRootPath(String root) { |
| 154 | super.setRootPath(root); |
| 155 | } |
| 156 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 157 | } |