Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016 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 | |
| 17 | package org.onosproject.segmentrouting.config; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.packet.MacAddress; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.TestApplicationId; |
| 27 | import org.onosproject.net.config.Config; |
| 28 | import org.onosproject.net.config.ConfigApplyDelegate; |
| 29 | import org.onosproject.segmentrouting.SegmentRoutingService; |
| 30 | import java.util.Set; |
| 31 | |
| 32 | import static org.hamcrest.Matchers.is; |
| 33 | import static org.junit.Assert.assertThat; |
| 34 | import static org.junit.Assert.assertTrue; |
| 35 | |
| 36 | /** |
| 37 | * Tests for class {@link SegmentRoutingAppConfig}. |
| 38 | */ |
| 39 | public class SegmentRoutingAppConfigTest { |
| 40 | private static final ApplicationId APP_ID = |
| 41 | new TestApplicationId(SegmentRoutingService.SR_APP_ID); |
| 42 | |
| 43 | private SegmentRoutingAppConfig config; |
| 44 | private MacAddress routerMac1; |
| 45 | private MacAddress routerMac2; |
| 46 | private MacAddress routerMac3; |
| 47 | |
| 48 | /** |
| 49 | * Initialize test related variables. |
| 50 | * |
| 51 | * @throws Exception |
| 52 | */ |
| 53 | @Before |
| 54 | public void setUp() throws Exception { |
| 55 | String jsonString = "{" + |
| 56 | "\"vRouterMacs\" : [" + |
| 57 | " \"00:00:00:00:00:01\"," + |
| 58 | " \"00:00:00:00:00:02\"" + |
| 59 | "]}"; |
| 60 | |
| 61 | routerMac1 = MacAddress.valueOf("00:00:00:00:00:01"); |
| 62 | routerMac2 = MacAddress.valueOf("00:00:00:00:00:02"); |
| 63 | routerMac3 = MacAddress.valueOf("00:00:00:00:00:03"); |
| 64 | |
| 65 | ApplicationId subject = APP_ID; |
| 66 | String key = SegmentRoutingService.SR_APP_ID; |
| 67 | ObjectMapper mapper = new ObjectMapper(); |
| 68 | JsonNode jsonNode = mapper.readTree(jsonString); |
| 69 | ConfigApplyDelegate delegate = new MockDelegate(); |
| 70 | |
| 71 | config = new SegmentRoutingAppConfig(); |
| 72 | config.init(subject, key, jsonNode, mapper, delegate); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Tests vRouters getter. |
| 77 | * |
| 78 | * @throws Exception |
| 79 | */ |
| 80 | @Test |
| 81 | public void testVRouters() throws Exception { |
| 82 | assertTrue(config.isValid()); |
| 83 | |
| 84 | Set<MacAddress> vRouters = config.vRouterMacs(); |
| 85 | assertThat(vRouters.size(), is(2)); |
| 86 | assertTrue(vRouters.contains(routerMac1)); |
| 87 | assertTrue(vRouters.contains(routerMac2)); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Tests vRouters setter. |
| 92 | * |
| 93 | * @throws Exception |
| 94 | */ |
| 95 | @Test |
| 96 | public void testSetVRouters() throws Exception { |
| 97 | ImmutableSet.Builder<MacAddress> builder = ImmutableSet.builder(); |
| 98 | builder.add(routerMac3); |
| 99 | config.setVRouterMacs(builder.build()); |
| 100 | |
| 101 | Set<MacAddress> macs = config.vRouterMacs(); |
| 102 | assertThat(macs.size(), is(1)); |
| 103 | assertTrue(macs.contains(routerMac3)); |
| 104 | } |
| 105 | |
| 106 | private class MockDelegate implements ConfigApplyDelegate { |
| 107 | @Override |
| 108 | public void onApply(Config config) { |
| 109 | } |
| 110 | } |
| 111 | } |