blob: 27996e54e783ffaac66392a7c8c54cf352131513 [file] [log] [blame]
Thomas Vachuska02aeb032015-01-06 22:36:30 -08001/*
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 Vachuskac97aa612015-06-23 16:00:18 -070016package org.onosproject.store.trivial;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080017
18import com.google.common.collect.ImmutableSet;
19import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
Thomas Vachuskae18a3302015-06-23 12:48:28 -070022import org.onlab.util.Tools;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080023import org.onosproject.app.ApplicationEvent;
24import org.onosproject.app.ApplicationStoreDelegate;
25import org.onosproject.common.app.ApplicationArchive;
26import org.onosproject.core.Application;
27import org.onosproject.core.ApplicationId;
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090028import org.onosproject.core.Permission;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080029import org.onosproject.core.ApplicationIdStoreAdapter;
30import org.onosproject.core.DefaultApplicationId;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080031
Thomas Vachuskae18a3302015-06-23 12:48:28 -070032import java.io.File;
33import java.io.IOException;
34import java.util.Random;
35
Thomas Vachuska02aeb032015-01-06 22:36:30 -080036import static org.junit.Assert.assertEquals;
Changhoon Yoonbdeb88a2015-05-12 20:35:31 +090037import static org.onosproject.app.ApplicationEvent.Type.APP_INSTALLED;
38import static org.onosproject.app.ApplicationEvent.Type.APP_DEACTIVATED;
39import static org.onosproject.app.ApplicationEvent.Type.APP_ACTIVATED;
40import static org.onosproject.app.ApplicationEvent.Type.APP_UNINSTALLED;
41import static org.onosproject.app.ApplicationEvent.Type.APP_PERMISSIONS_CHANGED;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080042import static org.onosproject.app.ApplicationState.ACTIVE;
43import static org.onosproject.app.ApplicationState.INSTALLED;
44
45/**
46 * Test of the trivial application store implementation.
47 */
48public class SimpleApplicationStoreTest {
49
Thomas Vachuskae18a3302015-06-23 12:48:28 -070050 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 Vachuska02aeb032015-01-06 22:36:30 -080054 private TestDelegate delegate = new TestDelegate();
Thomas Vachuskae18a3302015-06-23 12:48:28 -070055 private static final Object LOCK = new Object();
Thomas Vachuska02aeb032015-01-06 22:36:30 -080056
57 @Before
58 public void setUp() {
59 store.idStore = new TestIdStore();
Thomas Vachuskae18a3302015-06-23 12:48:28 -070060 store.setRootPath(STORE);
Thomas Vachuska02aeb032015-01-06 22:36:30 -080061 store.setDelegate(delegate);
62 store.activate();
63 }
64
65 @After
Thomas Vachuskae18a3302015-06-23 12:48:28 -070066 public void tearDown() throws IOException {
67 if (new File(ROOT).exists()) {
68 Tools.removeDirectory(ROOT);
69 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -080070 store.deactivate();
71 }
72
73 private Application createTestApp() {
Thomas Vachuskae18a3302015-06-23 12:48:28 -070074 synchronized (LOCK) {
75 return store.create(ApplicationArchive.class.getResourceAsStream("app.zip"));
76 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -080077 }
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 Yoona7841ed2015-05-15 02:51:08 +0900122 ImmutableSet<Permission> permissions = ImmutableSet.of(Permission.FLOWRULE_WRITE);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800123 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 Vachuskae18a3302015-06-23 12:48:28 -0700150
151 private class TestApplicationStore extends SimpleApplicationStore {
152 @Override
153 public void setRootPath(String root) {
154 super.setRootPath(root);
155 }
156 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800157}