Thomas Vachuska | 5197928 | 2015-04-07 18:49:17 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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; |
| 23 | import org.onlab.rest.BaseResource; |
| 24 | import org.onosproject.cfg.ComponentConfigAdapter; |
| 25 | import org.onosproject.cfg.ComponentConfigService; |
| 26 | import org.onosproject.cfg.ConfigProperty; |
| 27 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 28 | import javax.ws.rs.BadRequestException; |
| 29 | import javax.ws.rs.client.Entity; |
| 30 | import javax.ws.rs.client.WebTarget; |
Thomas Vachuska | 5197928 | 2015-04-07 18:49:17 -0700 | [diff] [blame] | 31 | import java.util.Set; |
| 32 | |
| 33 | import static org.hamcrest.MatcherAssert.assertThat; |
| 34 | import static org.hamcrest.Matchers.containsString; |
| 35 | import static org.hamcrest.Matchers.not; |
| 36 | import static org.junit.Assert.assertEquals; |
| 37 | import static org.onosproject.cfg.ConfigProperty.Type.INTEGER; |
| 38 | import static org.onosproject.cfg.ConfigProperty.Type.STRING; |
| 39 | import static org.onosproject.cfg.ConfigProperty.defineProperty; |
| 40 | |
| 41 | /** |
| 42 | * Test of the component config REST API. |
| 43 | */ |
| 44 | public class ComponentConfigWebResourceTest extends ResourceTest { |
| 45 | |
| 46 | private TestConfigManager service; |
| 47 | |
| 48 | @Before |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 49 | public void setUpMock() { |
Thomas Vachuska | 5197928 | 2015-04-07 18:49:17 -0700 | [diff] [blame] | 50 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 59 | WebTarget wt = target(); |
| 60 | String response = wt.path("configuration").request().get(String.class); |
Thomas Vachuska | 5197928 | 2015-04-07 18:49:17 -0700 | [diff] [blame] | 61 | assertThat(response, containsString("\"foo\":")); |
| 62 | assertThat(response, containsString("\"bar\":")); |
| 63 | } |
| 64 | |
| 65 | @Test |
| 66 | public void getConfigs() { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 67 | WebTarget wt = target(); |
| 68 | String response = wt.path("configuration/foo").request().get(String.class); |
Thomas Vachuska | 5197928 | 2015-04-07 18:49:17 -0700 | [diff] [blame] | 69 | assertThat(response, containsString("{\"foo\":")); |
| 70 | assertThat(response, not(containsString("{\"bar\":"))); |
| 71 | } |
| 72 | |
| 73 | @Test |
| 74 | public void setConfigs() { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 75 | WebTarget wt = target(); |
Thomas Vachuska | 5197928 | 2015-04-07 18:49:17 -0700 | [diff] [blame] | 76 | try { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 77 | wt.path("configuration/foo").request().post( |
| 78 | Entity.json("{ \"k\" : \"v\" }"), String.class); |
| 79 | } catch (BadRequestException e) { |
Thomas Vachuska | 5197928 | 2015-04-07 18:49:17 -0700 | [diff] [blame] | 80 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 88 | WebTarget wt = target(); |
Thomas Vachuska | 5197928 | 2015-04-07 18:49:17 -0700 | [diff] [blame] | 89 | try { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 90 | // 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 Vachuska | 5197928 | 2015-04-07 18:49:17 -0700 | [diff] [blame] | 94 | 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 | } |