blob: 4edba503b33b7c20eecb33684352f280369d5099 [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;
19import com.sun.jersey.api.client.UniformInterfaceException;
20import com.sun.jersey.api.client.WebResource;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.osgi.ServiceDirectory;
24import org.onlab.osgi.TestServiceDirectory;
25import org.onlab.rest.BaseResource;
26import org.onosproject.cfg.ComponentConfigAdapter;
27import org.onosproject.cfg.ComponentConfigService;
28import org.onosproject.cfg.ConfigProperty;
29
30import 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
48 public void setUp() {
49 service = new TestConfigManager();
50 ServiceDirectory testDirectory =
51 new TestServiceDirectory()
52 .add(ComponentConfigService.class, service);
53 BaseResource.setServiceDirectory(testDirectory);
54 }
55
56 @Test
57 public void getAllConfigs() {
58 WebResource rs = resource();
59 String response = rs.path("configuration").get(String.class);
60 assertThat(response, containsString("\"foo\":"));
61 assertThat(response, containsString("\"bar\":"));
62 }
63
64 @Test
65 public void getConfigs() {
66 WebResource rs = resource();
67 String response = rs.path("configuration/foo").get(String.class);
68 assertThat(response, containsString("{\"foo\":"));
69 assertThat(response, not(containsString("{\"bar\":")));
70 }
71
72 @Test
73 public void setConfigs() {
74 WebResource rs = resource();
75 try {
76 rs.path("configuration/foo").post(String.class, "{ \"k\" : \"v\" }");
77 } catch (UniformInterfaceException e) {
78 assertEquals("incorrect key", "foo", service.component);
79 assertEquals("incorrect key", "k", service.name);
80 assertEquals("incorrect value", "v", service.value);
81 }
82 }
83
84 @Test
85 public void unsetConfigs() {
86 WebResource rs = resource();
87 try {
88 rs.path("configuration/foo").delete(String.class, "{ \"k\" : \"v\" }");
89 } catch (UniformInterfaceException e) {
90 assertEquals("incorrect key", "foo", service.component);
91 assertEquals("incorrect key", "k", service.name);
92 assertEquals("incorrect value", null, service.value);
93 }
94 }
95
96
97 private class TestConfigManager extends ComponentConfigAdapter {
98
99 private String component;
100 private String name;
101 private String value;
102
103 @Override
104 public Set<String> getComponentNames() {
105 return ImmutableSet.of("foo", "bar");
106 }
107
108 @Override
109 public void setProperty(String componentName, String name, String value) {
110 this.component = componentName;
111 this.name = name;
112 this.value = value;
113 }
114
115 @Override
116 public void unsetProperty(String componentName, String name) {
117 this.component = componentName;
118 this.name = name;
119 this.value = null;
120 }
121
122 @Override
123 public Set<ConfigProperty> getProperties(String componentName) {
124 return ImmutableSet.of(defineProperty("k1", STRING, "d1", "dv1"),
125 defineProperty("k2", INTEGER, "d2", "321"));
126 }
127 }
128}