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