Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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.d.config; |
| 17 | |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 18 | import static org.hamcrest.Matchers.contains; |
| 19 | import static org.hamcrest.Matchers.equalTo; |
| 20 | import static org.hamcrest.Matchers.instanceOf; |
| 21 | import static org.hamcrest.Matchers.is; |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 22 | import static org.junit.Assert.*; |
| 23 | import static org.onosproject.d.config.DeviceResourceIds.DCS_NAMESPACE; |
| 24 | import static org.onosproject.d.config.DeviceResourceIds.DEVICES_NAME; |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 25 | import static org.onosproject.d.config.DeviceResourceIds.DEVICE_ID_KL_NAME; |
| 26 | import static org.onosproject.d.config.DeviceResourceIds.DEVICE_NAME; |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 27 | import static org.onosproject.d.config.DeviceResourceIds.ROOT_NAME; |
| 28 | |
| 29 | import org.junit.Before; |
| 30 | import org.junit.Test; |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 31 | import org.onosproject.net.DeviceId; |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 32 | import org.onosproject.yang.model.KeyLeaf; |
| 33 | import org.onosproject.yang.model.ListKey; |
| 34 | import org.onosproject.yang.model.NodeKey; |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 35 | import org.onosproject.yang.model.ResourceId; |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 36 | import org.onosproject.yang.model.SchemaId; |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 37 | |
| 38 | public class DeviceResourceIdsTest { |
| 39 | |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 40 | static final DeviceId DID_A = DeviceId.deviceId("test:a"); |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 41 | |
| 42 | static final ResourceId DEVICES = ResourceId.builder() |
| 43 | .addBranchPointSchema(ROOT_NAME, DCS_NAMESPACE) |
| 44 | .addBranchPointSchema(DEVICES_NAME, DCS_NAMESPACE) |
| 45 | .build(); |
| 46 | |
| 47 | ResourceId ridA; |
| 48 | ResourceId ridAcopy; |
| 49 | |
| 50 | @Before |
| 51 | public void setUp() throws Exception { |
| 52 | ridA = DeviceResourceIds.toResourceId(DID_A); |
| 53 | ridAcopy = ridA.copyBuilder().build(); |
| 54 | } |
| 55 | |
| 56 | @Test |
| 57 | public void testToDeviceId() throws CloneNotSupportedException { |
| 58 | ResourceId ridAchild = ridA.copyBuilder() |
| 59 | .addBranchPointSchema("some", "ns") |
| 60 | .addBranchPointSchema("random", "ns") |
| 61 | .addBranchPointSchema("child", "ns") |
| 62 | .build(); |
| 63 | |
| 64 | assertEquals(DID_A, DeviceResourceIds.toDeviceId(ridAchild)); |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 65 | |
| 66 | NodeKey<?> nodeKey = ridA.nodeKeys().get(2); |
| 67 | assertThat(nodeKey, is(instanceOf(ListKey.class))); |
| 68 | assertThat(nodeKey.schemaId(), is(equalTo(new SchemaId(DEVICE_NAME, DCS_NAMESPACE)))); |
| 69 | ListKey listKey = (ListKey) nodeKey; |
| 70 | assertThat(listKey.keyLeafs(), is(contains(new KeyLeaf(DEVICE_ID_KL_NAME, DCS_NAMESPACE, DID_A.toString())))); |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Test |
| 74 | public void testDeviceRootNode() { |
| 75 | assertTrue(DeviceResourceIds.isDeviceRootNode(ridA)); |
| 76 | |
| 77 | assertFalse(DeviceResourceIds.isRootOrDevicesNode(ridA)); |
| 78 | } |
| 79 | |
Yuta HIGUCHI | e057dee | 2017-09-15 13:56:10 -0700 | [diff] [blame] | 80 | @Test |
| 81 | public void testDeviceSubtreeTest() { |
| 82 | ResourceId absDevice = ResourceId.builder() |
| 83 | .addBranchPointSchema(ROOT_NAME, DCS_NAMESPACE) |
| 84 | .addBranchPointSchema(DEVICES_NAME, DCS_NAMESPACE) |
| 85 | .addBranchPointSchema(DEVICE_NAME, DCS_NAMESPACE) |
| 86 | .addKeyLeaf(DEVICE_ID_KL_NAME, DCS_NAMESPACE, DID_A.toString()) |
| 87 | .build(); |
| 88 | |
| 89 | NodeKey<?> deviceKey = absDevice.nodeKeys().get(2); |
| 90 | assertThat(deviceKey, is(instanceOf(ListKey.class))); |
| 91 | assertThat(deviceKey.schemaId().namespace(), is(equalTo(DCS_NAMESPACE))); |
| 92 | assertThat(deviceKey.schemaId().name(), is(equalTo(DEVICE_NAME))); |
| 93 | assertTrue(DeviceResourceIds.isUnderDeviceRootNode(absDevice)); |
| 94 | } |
| 95 | |
| 96 | @Test |
| 97 | public void testDeviceSubtreeEventTest() { |
| 98 | // root relative ResourceId used by DynamicConfigEvent |
| 99 | ResourceId evtDevice = ResourceId.builder() |
| 100 | .addBranchPointSchema(DEVICES_NAME, DCS_NAMESPACE) |
| 101 | .addBranchPointSchema(DEVICE_NAME, DCS_NAMESPACE) |
| 102 | .addKeyLeaf(DEVICE_ID_KL_NAME, DCS_NAMESPACE, DID_A.toString()) |
| 103 | .build(); |
| 104 | |
| 105 | NodeKey<?> deviceKey = evtDevice.nodeKeys().get(1); |
| 106 | assertThat(deviceKey, is(instanceOf(ListKey.class))); |
| 107 | assertThat(deviceKey.schemaId().namespace(), is(equalTo(DCS_NAMESPACE))); |
| 108 | assertThat(deviceKey.schemaId().name(), is(equalTo(DEVICE_NAME))); |
| 109 | assertTrue(DeviceResourceIds.isUnderDeviceRootNode(evtDevice)); |
| 110 | } |
| 111 | |
Yuta HIGUCHI | 2405782 | 2017-08-02 15:03:51 -0700 | [diff] [blame] | 112 | } |