blob: 9631bd327452d1bfcf31ad6945945c9e57753ef0 [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;
Thomas Vachuska8044c092015-08-04 11:06:41 -070019import com.google.common.io.Files;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080020import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
Thomas Vachuskae18a3302015-06-23 12:48:28 -070023import org.onlab.util.Tools;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080024import org.onosproject.app.ApplicationEvent;
25import org.onosproject.app.ApplicationStoreDelegate;
26import org.onosproject.common.app.ApplicationArchive;
27import org.onosproject.core.Application;
28import org.onosproject.core.ApplicationId;
29import org.onosproject.core.ApplicationIdStoreAdapter;
30import org.onosproject.core.DefaultApplicationId;
Thomas Vachuska8044c092015-08-04 11:06:41 -070031import org.onosproject.core.Permission;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080032
Thomas Vachuskae18a3302015-06-23 12:48:28 -070033import java.io.File;
34import java.io.IOException;
Thomas Vachuskae18a3302015-06-23 12:48:28 -070035
Thomas Vachuska02aeb032015-01-06 22:36:30 -080036import static org.junit.Assert.assertEquals;
Thomas Vachuska8044c092015-08-04 11:06:41 -070037import static org.onosproject.app.ApplicationEvent.Type.*;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080038import static org.onosproject.app.ApplicationState.ACTIVE;
39import static org.onosproject.app.ApplicationState.INSTALLED;
40
41/**
42 * Test of the trivial application store implementation.
43 */
44public class SimpleApplicationStoreTest {
45
Thomas Vachuska8044c092015-08-04 11:06:41 -070046 static final File STORE = Files.createTempDir();
Thomas Vachuskae18a3302015-06-23 12:48:28 -070047
48 private TestApplicationStore store = new TestApplicationStore();
Thomas Vachuska02aeb032015-01-06 22:36:30 -080049 private TestDelegate delegate = new TestDelegate();
Thomas Vachuskae18a3302015-06-23 12:48:28 -070050 private static final Object LOCK = new Object();
Thomas Vachuska02aeb032015-01-06 22:36:30 -080051
52 @Before
53 public void setUp() {
54 store.idStore = new TestIdStore();
Thomas Vachuska8044c092015-08-04 11:06:41 -070055 store.setRootPath(STORE.getAbsolutePath());
Thomas Vachuska02aeb032015-01-06 22:36:30 -080056 store.setDelegate(delegate);
57 store.activate();
58 }
59
60 @After
Thomas Vachuskae18a3302015-06-23 12:48:28 -070061 public void tearDown() throws IOException {
Thomas Vachuska8044c092015-08-04 11:06:41 -070062 if (STORE.exists()) {
63 Tools.removeDirectory(STORE);
Thomas Vachuskae18a3302015-06-23 12:48:28 -070064 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -080065 store.deactivate();
66 }
67
68 private Application createTestApp() {
Thomas Vachuskae18a3302015-06-23 12:48:28 -070069 synchronized (LOCK) {
70 return store.create(ApplicationArchive.class.getResourceAsStream("app.zip"));
71 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -080072 }
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 Yoona7841ed2015-05-15 02:51:08 +0900117 ImmutableSet<Permission> permissions = ImmutableSet.of(Permission.FLOWRULE_WRITE);
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800118 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 Vachuskae18a3302015-06-23 12:48:28 -0700145
146 private class TestApplicationStore extends SimpleApplicationStore {
147 @Override
148 public void setRootPath(String root) {
149 super.setRootPath(root);
150 }
151 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800152}