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