blob: 6ff02e302299b04767884713b60e600619bccf6f [file] [log] [blame]
Thomas Vachuska51979282015-04-07 18:49:17 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Thomas Vachuska51979282015-04-07 18:49:17 -07003 *
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 */
Jian Li8ae91202016-03-24 14:36:16 -070016package org.onosproject.rest.resources;
Thomas Vachuska51979282015-04-07 18:49:17 -070017
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;
Thomas Vachuska51979282015-04-07 18:49:17 -070023import org.onosproject.cfg.ComponentConfigAdapter;
24import org.onosproject.cfg.ComponentConfigService;
25import org.onosproject.cfg.ConfigProperty;
26
Jian Li9d616492016-03-09 10:52:49 -080027import javax.ws.rs.BadRequestException;
28import javax.ws.rs.client.Entity;
29import javax.ws.rs.client.WebTarget;
Thomas Vachuska51979282015-04-07 18:49:17 -070030import java.util.Set;
31
32import static org.hamcrest.MatcherAssert.assertThat;
33import static org.hamcrest.Matchers.containsString;
34import static org.hamcrest.Matchers.not;
35import static org.junit.Assert.assertEquals;
36import static org.onosproject.cfg.ConfigProperty.Type.INTEGER;
37import static org.onosproject.cfg.ConfigProperty.Type.STRING;
38import static org.onosproject.cfg.ConfigProperty.defineProperty;
39
40/**
41 * Test of the component config REST API.
42 */
43public class ComponentConfigWebResourceTest extends ResourceTest {
44
45 private TestConfigManager service;
46
47 @Before
Jian Li9d616492016-03-09 10:52:49 -080048 public void setUpMock() {
Thomas Vachuska51979282015-04-07 18:49:17 -070049 service = new TestConfigManager();
50 ServiceDirectory testDirectory =
51 new TestServiceDirectory()
52 .add(ComponentConfigService.class, service);
Ray Milkey094a1352018-01-22 14:03:54 -080053 setServiceDirectory(testDirectory);
Thomas Vachuska51979282015-04-07 18:49:17 -070054 }
55
56 @Test
57 public void getAllConfigs() {
Jian Li9d616492016-03-09 10:52:49 -080058 WebTarget wt = target();
59 String response = wt.path("configuration").request().get(String.class);
Thomas Vachuska51979282015-04-07 18:49:17 -070060 assertThat(response, containsString("\"foo\":"));
61 assertThat(response, containsString("\"bar\":"));
62 }
63
64 @Test
65 public void getConfigs() {
Jian Li9d616492016-03-09 10:52:49 -080066 WebTarget wt = target();
67 String response = wt.path("configuration/foo").request().get(String.class);
Thomas Vachuska51979282015-04-07 18:49:17 -070068 assertThat(response, containsString("{\"foo\":"));
69 assertThat(response, not(containsString("{\"bar\":")));
70 }
71
72 @Test
73 public void setConfigs() {
Jian Li9d616492016-03-09 10:52:49 -080074 WebTarget wt = target();
Thomas Vachuska51979282015-04-07 18:49:17 -070075 try {
Jian Li9d616492016-03-09 10:52:49 -080076 wt.path("configuration/foo").request().post(
77 Entity.json("{ \"k\" : \"v\" }"), String.class);
78 } catch (BadRequestException e) {
Thomas Vachuska51979282015-04-07 18:49:17 -070079 assertEquals("incorrect key", "foo", service.component);
80 assertEquals("incorrect key", "k", service.name);
81 assertEquals("incorrect value", "v", service.value);
82 }
83 }
84
85 @Test
86 public void unsetConfigs() {
Jian Li9d616492016-03-09 10:52:49 -080087 WebTarget wt = target();
Thomas Vachuska51979282015-04-07 18:49:17 -070088 try {
Jian Li9d616492016-03-09 10:52:49 -080089 // TODO: this needs to be revised later. Do you really need to
90 // contain any entry inside delete request? Why not just use put then?
91 wt.path("configuration/foo").request().delete();
92 } catch (BadRequestException e) {
Thomas Vachuska51979282015-04-07 18:49:17 -070093 assertEquals("incorrect key", "foo", service.component);
94 assertEquals("incorrect key", "k", service.name);
95 assertEquals("incorrect value", null, service.value);
96 }
97 }
98
99
100 private class TestConfigManager extends ComponentConfigAdapter {
101
102 private String component;
103 private String name;
104 private String value;
105
106 @Override
107 public Set<String> getComponentNames() {
108 return ImmutableSet.of("foo", "bar");
109 }
110
111 @Override
112 public void setProperty(String componentName, String name, String value) {
113 this.component = componentName;
114 this.name = name;
115 this.value = value;
116 }
117
118 @Override
119 public void unsetProperty(String componentName, String name) {
120 this.component = componentName;
121 this.name = name;
122 this.value = null;
123 }
124
125 @Override
126 public Set<ConfigProperty> getProperties(String componentName) {
127 return ImmutableSet.of(defineProperty("k1", STRING, "d1", "dv1"),
128 defineProperty("k2", INTEGER, "d2", "321"));
129 }
130 }
131}