Jian Li | b68a2b0 | 2016-05-02 11:23:32 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jian Li | b68a2b0 | 2016-05-02 11:23:32 -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 | */ |
| 16 | package org.onosproject.codec.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import com.google.common.collect.ImmutableList; |
| 21 | import org.apache.commons.lang3.StringUtils; |
| 22 | import org.hamcrest.Description; |
| 23 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 24 | import org.junit.Before; |
| 25 | import org.junit.Test; |
| 26 | import org.onosproject.cluster.NodeId; |
| 27 | import org.onosproject.cluster.RoleInfo; |
| 28 | import org.onosproject.codec.JsonCodec; |
| 29 | |
| 30 | import java.io.IOException; |
| 31 | import java.io.InputStream; |
| 32 | import java.util.List; |
| 33 | |
| 34 | import static org.hamcrest.MatcherAssert.assertThat; |
| 35 | import static org.hamcrest.Matchers.is; |
| 36 | import static org.hamcrest.Matchers.notNullValue; |
| 37 | |
| 38 | /** |
| 39 | * Unit tests for role info codec. |
| 40 | */ |
| 41 | public final class RoleInfoCodecTest { |
| 42 | |
| 43 | MockCodecContext context; |
| 44 | JsonCodec<RoleInfo> roleInfoCodec; |
| 45 | |
| 46 | @Before |
| 47 | public void setUp() { |
| 48 | context = new MockCodecContext(); |
| 49 | roleInfoCodec = context.codec(RoleInfo.class); |
| 50 | assertThat(roleInfoCodec, notNullValue()); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Tests encoding of a role info object. |
| 55 | */ |
| 56 | @Test |
| 57 | public void testRoleInfoEncode() { |
| 58 | NodeId masterNodeId = NodeId.nodeId("1"); |
| 59 | NodeId backupNodeId1 = NodeId.nodeId("1"); |
| 60 | NodeId backupNodeId2 = NodeId.nodeId("2"); |
| 61 | NodeId backupNodeId3 = NodeId.nodeId("3"); |
| 62 | List<NodeId> backupNodeIds = |
| 63 | ImmutableList.of(backupNodeId1, backupNodeId2, backupNodeId3); |
| 64 | |
| 65 | RoleInfo roleInfo = new RoleInfo(masterNodeId, backupNodeIds); |
| 66 | ObjectNode roleInfoJson = roleInfoCodec.encode(roleInfo, context); |
| 67 | assertThat(roleInfoJson, RoleInfoJsonMatcher.matchesRoleInfo(roleInfo)); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Tests decoding of a role info JSON object. |
| 72 | */ |
| 73 | @Test |
| 74 | public void testRoleInfoDecode() throws IOException { |
| 75 | RoleInfo roleInfo = getRoleInfo("RoleInfo.json"); |
| 76 | |
| 77 | assertThat(roleInfo.backups().size(), is(3)); |
| 78 | |
| 79 | assertThat(roleInfo.master().id(), is("1")); |
| 80 | |
| 81 | List<NodeId> backups = roleInfo.backups(); |
| 82 | assertThat(backups.contains(NodeId.nodeId("2")), is(true)); |
| 83 | assertThat(backups.contains(NodeId.nodeId("3")), is(true)); |
| 84 | assertThat(backups.contains(NodeId.nodeId("4")), is(true)); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Hamcrest matcher for role info. |
| 89 | */ |
| 90 | private static final class RoleInfoJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 91 | |
| 92 | private final RoleInfo roleInfo; |
| 93 | |
| 94 | private RoleInfoJsonMatcher(RoleInfo roleInfo) { |
| 95 | this.roleInfo = roleInfo; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 100 | |
| 101 | // check master node identifier |
| 102 | String jsonNodeId = jsonNode.get("master") != null ? jsonNode.get("master").asText() : null; |
| 103 | String nodeId = roleInfo.master().id(); |
| 104 | if (!StringUtils.equals(jsonNodeId, nodeId)) { |
| 105 | description.appendText("master's node id was " + jsonNodeId); |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | // check backup nodes size |
| 110 | JsonNode jsonBackupNodeIds = jsonNode.get("backups"); |
| 111 | if (jsonBackupNodeIds.size() != roleInfo.backups().size()) { |
| 112 | description.appendText("backup nodes size was " + jsonBackupNodeIds.size()); |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | // check backup nodes' identifier |
| 118 | for (NodeId backupNodeId : roleInfo.backups()) { |
| 119 | boolean backupFound = false; |
| 120 | for (int idx = 0; idx < jsonBackupNodeIds.size(); idx++) { |
| 121 | if (backupNodeId.id().equals(jsonBackupNodeIds.get(idx).asText())) { |
| 122 | backupFound = true; |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | if (!backupFound) { |
| 127 | description.appendText("backup not found " + backupNodeId.id()); |
| 128 | return false; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public void describeTo(Description description) { |
| 137 | description.appendText(roleInfo.toString()); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Factory to allocate a role info. |
| 142 | * |
| 143 | * @param roleInfo role info object we are looking for |
| 144 | * @return matcher |
| 145 | */ |
| 146 | static RoleInfoJsonMatcher matchesRoleInfo(RoleInfo roleInfo) { |
| 147 | return new RoleInfoJsonMatcher(roleInfo); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Reads in a role info from the given resource and decodes it. |
| 153 | * |
| 154 | * @param resourceName resource to use to read the JSON for the rule |
| 155 | * @return decoded roleInfo |
| 156 | * @throws IOException if processing the resource fails |
| 157 | */ |
| 158 | private RoleInfo getRoleInfo(String resourceName) throws IOException { |
| 159 | InputStream jsonStream = RoleInfoCodecTest.class.getResourceAsStream(resourceName); |
| 160 | JsonNode json = context.mapper().readTree(jsonStream); |
| 161 | assertThat(json, notNullValue()); |
| 162 | RoleInfo roleInfo = roleInfoCodec.decode((ObjectNode) json, context); |
| 163 | assertThat(roleInfo, notNullValue()); |
| 164 | return roleInfo; |
| 165 | } |
| 166 | } |