blob: 29353a5c78d8f6a2476127578d3849537ddafe69 [file] [log] [blame]
Thomas Vachuska51979282015-04-07 18:49:17 -07001/*
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.rest;
17
18import com.google.common.collect.ImmutableSet;
Thomas Vachuska51979282015-04-07 18:49:17 -070019import org.junit.Before;
20import org.junit.Test;
21import org.onlab.osgi.ServiceDirectory;
22import org.onlab.osgi.TestServiceDirectory;
23import org.onlab.rest.BaseResource;
24import org.onosproject.cfg.ComponentConfigAdapter;
25import org.onosproject.cfg.ComponentConfigService;
26import org.onosproject.cfg.ConfigProperty;
27
Jian Li9d616492016-03-09 10:52:49 -080028import javax.ws.rs.BadRequestException;
29import javax.ws.rs.client.Entity;
30import javax.ws.rs.client.WebTarget;
Thomas Vachuska51979282015-04-07 18:49:17 -070031import java.util.Set;
32
33import static org.hamcrest.MatcherAssert.assertThat;
34import static org.hamcrest.Matchers.containsString;
35import static org.hamcrest.Matchers.not;
36import static org.junit.Assert.assertEquals;
37import static org.onosproject.cfg.ConfigProperty.Type.INTEGER;
38import static org.onosproject.cfg.ConfigProperty.Type.STRING;
39import static org.onosproject.cfg.ConfigProperty.defineProperty;
40
41/**
42 * Test of the component config REST API.
43 */
44public class ComponentConfigWebResourceTest extends ResourceTest {
45
46 private TestConfigManager service;
47
48 @Before
Jian Li9d616492016-03-09 10:52:49 -080049 public void setUpMock() {
Thomas Vachuska51979282015-04-07 18:49:17 -070050 service = new TestConfigManager();
51 ServiceDirectory testDirectory =
52 new TestServiceDirectory()
53 .add(ComponentConfigService.class, service);
54 BaseResource.setServiceDirectory(testDirectory);
55 }
56
57 @Test
58 public void getAllConfigs() {
Jian Li9d616492016-03-09 10:52:49 -080059 WebTarget wt = target();
60 String response = wt.path("configuration").request().get(String.class);
Thomas Vachuska51979282015-04-07 18:49:17 -070061 assertThat(response, containsString("\"foo\":"));
62 assertThat(response, containsString("\"bar\":"));
63 }
64
65 @Test
66 public void getConfigs() {
Jian Li9d616492016-03-09 10:52:49 -080067 WebTarget wt = target();
68 String response = wt.path("configuration/foo").request().get(String.class);
Thomas Vachuska51979282015-04-07 18:49:17 -070069 assertThat(response, containsString("{\"foo\":"));
70 assertThat(response, not(containsString("{\"bar\":")));
71 }
72
73 @Test
74 public void setConfigs() {
Jian Li9d616492016-03-09 10:52:49 -080075 WebTarget wt = target();
Thomas Vachuska51979282015-04-07 18:49:17 -070076 try {
Jian Li9d616492016-03-09 10:52:49 -080077 wt.path("configuration/foo").request().post(
78 Entity.json("{ \"k\" : \"v\" }"), String.class);
79 } catch (BadRequestException e) {
Thomas Vachuska51979282015-04-07 18:49:17 -070080 assertEquals("incorrect key", "foo", service.component);
81 assertEquals("incorrect key", "k", service.name);
82 assertEquals("incorrect value", "v", service.value);
83 }
84 }
85
86 @Test
87 public void unsetConfigs() {
Jian Li9d616492016-03-09 10:52:49 -080088 WebTarget wt = target();
Thomas Vachuska51979282015-04-07 18:49:17 -070089 try {
Jian Li9d616492016-03-09 10:52:49 -080090 // TODO: this needs to be revised later. Do you really need to
91 // contain any entry inside delete request? Why not just use put then?
92 wt.path("configuration/foo").request().delete();
93 } catch (BadRequestException e) {
Thomas Vachuska51979282015-04-07 18:49:17 -070094 assertEquals("incorrect key", "foo", service.component);
95 assertEquals("incorrect key", "k", service.name);
96 assertEquals("incorrect value", null, service.value);
97 }
98 }
99
100
101 private class TestConfigManager extends ComponentConfigAdapter {
102
103 private String component;
104 private String name;
105 private String value;
106
107 @Override
108 public Set<String> getComponentNames() {
109 return ImmutableSet.of("foo", "bar");
110 }
111
112 @Override
113 public void setProperty(String componentName, String name, String value) {
114 this.component = componentName;
115 this.name = name;
116 this.value = value;
117 }
118
119 @Override
120 public void unsetProperty(String componentName, String name) {
121 this.component = componentName;
122 this.name = name;
123 this.value = null;
124 }
125
126 @Override
127 public Set<ConfigProperty> getProperties(String componentName) {
128 return ImmutableSet.of(defineProperty("k1", STRING, "d1", "dv1"),
129 defineProperty("k2", INTEGER, "d2", "321"));
130 }
131 }
132}