blob: 92b82e959770dc59e4d279b41c2f979ba2e4c495 [file] [log] [blame]
Thomas Vachuska02aeb032015-01-06 22:36:30 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska02aeb032015-01-06 22:36:30 -08003 *
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;
sangyun-han6d33e802016-08-05 13:36:33 +090029import org.onosproject.app.ApplicationIdStoreAdapter;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080030import org.onosproject.core.DefaultApplicationId;
Changhoon Yoonb856b812015-08-10 03:47:19 +090031import org.onosproject.security.AppPermission;
32import org.onosproject.security.Permission;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080033
Thomas Vachuskae18a3302015-06-23 12:48:28 -070034import java.io.File;
35import java.io.IOException;
Thomas Vachuskae18a3302015-06-23 12:48:28 -070036
Thomas Vachuska02aeb032015-01-06 22:36:30 -080037import static org.junit.Assert.assertEquals;
Thomas Vachuska8044c092015-08-04 11:06:41 -070038import static org.onosproject.app.ApplicationEvent.Type.*;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080039import static org.onosproject.app.ApplicationState.ACTIVE;
40import static org.onosproject.app.ApplicationState.INSTALLED;
41
42/**
43 * Test of the trivial application store implementation.
44 */
45public class SimpleApplicationStoreTest {
46
Thomas Vachuska8044c092015-08-04 11:06:41 -070047 static final File STORE = Files.createTempDir();
Thomas Vachuskae18a3302015-06-23 12:48:28 -070048
49 private TestApplicationStore store = new TestApplicationStore();
Thomas Vachuska02aeb032015-01-06 22:36:30 -080050 private TestDelegate delegate = new TestDelegate();
Thomas Vachuskae18a3302015-06-23 12:48:28 -070051 private static final Object LOCK = new Object();
Thomas Vachuska02aeb032015-01-06 22:36:30 -080052
53 @Before
54 public void setUp() {
55 store.idStore = new TestIdStore();
Thomas Vachuska8044c092015-08-04 11:06:41 -070056 store.setRootPath(STORE.getAbsolutePath());
Thomas Vachuska02aeb032015-01-06 22:36:30 -080057 store.setDelegate(delegate);
58 store.activate();
59 }
60
61 @After
Thomas Vachuskae18a3302015-06-23 12:48:28 -070062 public void tearDown() throws IOException {
Thomas Vachuska8044c092015-08-04 11:06:41 -070063 if (STORE.exists()) {
64 Tools.removeDirectory(STORE);
Thomas Vachuskae18a3302015-06-23 12:48:28 -070065 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -080066 store.deactivate();
67 }
68
69 private Application createTestApp() {
Thomas Vachuskae18a3302015-06-23 12:48:28 -070070 synchronized (LOCK) {
71 return store.create(ApplicationArchive.class.getResourceAsStream("app.zip"));
72 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -080073 }
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 Yoonb856b812015-08-10 03:47:19 +0900118 ImmutableSet<Permission> permissions =
119 ImmutableSet.of(new Permission(AppPermission.class.getName(), "FLOWRULE_WRITE"));
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800120 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 Vachuskae18a3302015-06-23 12:48:28 -0700147
148 private class TestApplicationStore extends SimpleApplicationStore {
149 @Override
150 public void setRootPath(String root) {
151 super.setRootPath(root);
152 }
153 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800154}