ONOS-1470 Added unit tests for component config REST API.
Change-Id: Id08c629cefff1a1276e6be7c07ea62e4ae89468a
Showing
1 changed file
with
128 additions
and
0 deletions
1 | +/* | ||
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 | + */ | ||
16 | +package org.onosproject.rest; | ||
17 | + | ||
18 | +import com.google.common.collect.ImmutableSet; | ||
19 | +import com.sun.jersey.api.client.UniformInterfaceException; | ||
20 | +import com.sun.jersey.api.client.WebResource; | ||
21 | +import org.junit.Before; | ||
22 | +import org.junit.Test; | ||
23 | +import org.onlab.osgi.ServiceDirectory; | ||
24 | +import org.onlab.osgi.TestServiceDirectory; | ||
25 | +import org.onlab.rest.BaseResource; | ||
26 | +import org.onosproject.cfg.ComponentConfigAdapter; | ||
27 | +import org.onosproject.cfg.ComponentConfigService; | ||
28 | +import org.onosproject.cfg.ConfigProperty; | ||
29 | + | ||
30 | +import java.util.Set; | ||
31 | + | ||
32 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
33 | +import static org.hamcrest.Matchers.containsString; | ||
34 | +import static org.hamcrest.Matchers.not; | ||
35 | +import static org.junit.Assert.assertEquals; | ||
36 | +import static org.onosproject.cfg.ConfigProperty.Type.INTEGER; | ||
37 | +import static org.onosproject.cfg.ConfigProperty.Type.STRING; | ||
38 | +import static org.onosproject.cfg.ConfigProperty.defineProperty; | ||
39 | + | ||
40 | +/** | ||
41 | + * Test of the component config REST API. | ||
42 | + */ | ||
43 | +public 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 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment