Thomas Vachuska | 5197928 | 2015-04-07 18:49:17 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Thomas Vachuska | 5197928 | 2015-04-07 18:49:17 -0700 | [diff] [blame] | 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 | */ |
Jian Li | 8ae9120 | 2016-03-24 14:36:16 -0700 | [diff] [blame] | 16 | package org.onosproject.rest.resources; |
Thomas Vachuska | 5197928 | 2015-04-07 18:49:17 -0700 | [diff] [blame] | 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
Thomas Vachuska | 5197928 | 2015-04-07 18:49:17 -0700 | [diff] [blame] | 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.osgi.ServiceDirectory; |
| 22 | import org.onlab.osgi.TestServiceDirectory; |
Thomas Vachuska | 5197928 | 2015-04-07 18:49:17 -0700 | [diff] [blame] | 23 | import org.onosproject.cfg.ComponentConfigAdapter; |
| 24 | import org.onosproject.cfg.ComponentConfigService; |
| 25 | import org.onosproject.cfg.ConfigProperty; |
| 26 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 27 | import javax.ws.rs.BadRequestException; |
| 28 | import javax.ws.rs.client.Entity; |
| 29 | import javax.ws.rs.client.WebTarget; |
Thomas Vachuska | 5197928 | 2015-04-07 18:49:17 -0700 | [diff] [blame] | 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 |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 48 | public void setUpMock() { |
Thomas Vachuska | 5197928 | 2015-04-07 18:49:17 -0700 | [diff] [blame] | 49 | service = new TestConfigManager(); |
| 50 | ServiceDirectory testDirectory = |
| 51 | new TestServiceDirectory() |
| 52 | .add(ComponentConfigService.class, service); |
Ray Milkey | 094a135 | 2018-01-22 14:03:54 -0800 | [diff] [blame] | 53 | setServiceDirectory(testDirectory); |
Thomas Vachuska | 5197928 | 2015-04-07 18:49:17 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | @Test |
| 57 | public void getAllConfigs() { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 58 | WebTarget wt = target(); |
| 59 | String response = wt.path("configuration").request().get(String.class); |
Thomas Vachuska | 5197928 | 2015-04-07 18:49:17 -0700 | [diff] [blame] | 60 | assertThat(response, containsString("\"foo\":")); |
| 61 | assertThat(response, containsString("\"bar\":")); |
| 62 | } |
| 63 | |
| 64 | @Test |
| 65 | public void getConfigs() { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 66 | WebTarget wt = target(); |
| 67 | String response = wt.path("configuration/foo").request().get(String.class); |
Thomas Vachuska | 5197928 | 2015-04-07 18:49:17 -0700 | [diff] [blame] | 68 | assertThat(response, containsString("{\"foo\":")); |
| 69 | assertThat(response, not(containsString("{\"bar\":"))); |
| 70 | } |
| 71 | |
| 72 | @Test |
| 73 | public void setConfigs() { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 74 | WebTarget wt = target(); |
Thomas Vachuska | 5197928 | 2015-04-07 18:49:17 -0700 | [diff] [blame] | 75 | try { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 76 | wt.path("configuration/foo").request().post( |
| 77 | Entity.json("{ \"k\" : \"v\" }"), String.class); |
| 78 | } catch (BadRequestException e) { |
Thomas Vachuska | 5197928 | 2015-04-07 18:49:17 -0700 | [diff] [blame] | 79 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 87 | WebTarget wt = target(); |
Thomas Vachuska | 5197928 | 2015-04-07 18:49:17 -0700 | [diff] [blame] | 88 | try { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 89 | // 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 Vachuska | 5197928 | 2015-04-07 18:49:17 -0700 | [diff] [blame] | 93 | 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 | } |