blob: 29006c4b0cc49d55f966dbce40c31e83939e3e24 [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 */
16package org.onosproject.store.trivial.impl;
17
18import com.google.common.collect.ImmutableSet;
19import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
22import org.onosproject.app.ApplicationEvent;
23import org.onosproject.app.ApplicationStoreDelegate;
24import org.onosproject.common.app.ApplicationArchive;
25import org.onosproject.core.Application;
26import org.onosproject.core.ApplicationId;
27import org.onosproject.core.ApplicationIdStoreAdapter;
28import org.onosproject.core.DefaultApplicationId;
29import org.onosproject.core.Permission;
30
31import static org.junit.Assert.assertEquals;
32import static org.onosproject.app.ApplicationEvent.Type.*;
33import static org.onosproject.app.ApplicationState.ACTIVE;
34import static org.onosproject.app.ApplicationState.INSTALLED;
35
36/**
37 * Test of the trivial application store implementation.
38 */
39public class SimpleApplicationStoreTest {
40
41 private SimpleApplicationStore store = new SimpleApplicationStore();
42 private TestDelegate delegate = new TestDelegate();
43
44 @Before
45 public void setUp() {
46 store.idStore = new TestIdStore();
47 store.setDelegate(delegate);
48 store.activate();
49 }
50
51 @After
52 public void tearDown() {
53 store.deactivate();
54 }
55
56 private Application createTestApp() {
57 return store.create(ApplicationArchive.class.getResourceAsStream("app.zip"));
58 }
59
60 @Test
61 public void create() {
62 Application app = createTestApp();
63 assertEquals("incorrect name", "org.foo.app", app.id().name());
64 assertEquals("incorrect app count", 1, store.getApplications().size());
65 assertEquals("incorrect app", app, store.getApplication(app.id()));
66 assertEquals("incorrect app state", INSTALLED, store.getState(app.id()));
67 assertEquals("incorrect event type", APP_INSTALLED, delegate.event.type());
68 assertEquals("incorrect event app", app, delegate.event.subject());
69 }
70
71 @Test
72 public void remove() {
73 Application app = createTestApp();
74 store.remove(app.id());
75 assertEquals("incorrect app count", 0, store.getApplications().size());
76 assertEquals("incorrect event type", APP_UNINSTALLED, delegate.event.type());
77 assertEquals("incorrect event app", app, delegate.event.subject());
78 }
79
80 @Test
81 public void activate() {
82 Application app = createTestApp();
83 store.activate(app.id());
84 assertEquals("incorrect app count", 1, store.getApplications().size());
85 assertEquals("incorrect app state", ACTIVE, store.getState(app.id()));
86 assertEquals("incorrect event type", APP_ACTIVATED, delegate.event.type());
87 assertEquals("incorrect event app", app, delegate.event.subject());
88 }
89
90 @Test
91 public void deactivate() {
92 Application app = createTestApp();
93 store.deactivate(app.id());
94 assertEquals("incorrect app count", 1, store.getApplications().size());
95 assertEquals("incorrect app state", INSTALLED, store.getState(app.id()));
96 assertEquals("incorrect event type", APP_DEACTIVATED, delegate.event.type());
97 assertEquals("incorrect event app", app, delegate.event.subject());
98 }
99
100 @Test
101 public void permissions() {
102 Application app = createTestApp();
103 ImmutableSet<Permission> permissions = ImmutableSet.of(new Permission() {
104 });
105 store.setPermissions(app.id(), permissions);
106 assertEquals("incorrect app perms", 1, store.getPermissions(app.id()).size());
107 assertEquals("incorrect app state", INSTALLED, store.getState(app.id()));
108 assertEquals("incorrect event type", APP_PERMISSIONS_CHANGED, delegate.event.type());
109 assertEquals("incorrect event app", app, delegate.event.subject());
110 }
111
112 private class TestIdStore extends ApplicationIdStoreAdapter {
113 @Override
114 public ApplicationId registerApplication(String name) {
115 return new DefaultApplicationId(1, name);
116 }
117
118 @Override
119 public ApplicationId getAppId(String name) {
120 return new DefaultApplicationId(1, name);
121 }
122 }
123
124 private class TestDelegate implements ApplicationStoreDelegate {
125 private ApplicationEvent event;
126
127 @Override
128 public void notify(ApplicationEvent event) {
129 this.event = event;
130 }
131 }
132}