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 org.hamcrest.Description; |
| 21 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onosproject.cluster.NodeId; |
| 25 | import org.onosproject.codec.JsonCodec; |
| 26 | import org.onosproject.mastership.MastershipTerm; |
| 27 | |
| 28 | import java.io.IOException; |
| 29 | import java.io.InputStream; |
| 30 | |
| 31 | import static org.hamcrest.MatcherAssert.assertThat; |
| 32 | import static org.hamcrest.Matchers.is; |
| 33 | import static org.hamcrest.Matchers.notNullValue; |
| 34 | |
| 35 | /** |
| 36 | * Unit tests for mastership term codec. |
| 37 | */ |
| 38 | public class MastershipTermCodecTest { |
| 39 | |
| 40 | MockCodecContext context; |
| 41 | JsonCodec<MastershipTerm> mastershipTermCodec; |
| 42 | |
| 43 | /** |
| 44 | * Sets up for each test. Creates a context and fetches the mastership term |
| 45 | * codec. |
| 46 | */ |
| 47 | @Before |
| 48 | public void setUp() { |
| 49 | context = new MockCodecContext(); |
| 50 | mastershipTermCodec = context.codec(MastershipTerm.class); |
| 51 | assertThat(mastershipTermCodec, notNullValue()); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Tests encoding of a mastership term object. |
| 56 | */ |
| 57 | @Test |
| 58 | public void testMastershipTermEncode() { |
| 59 | NodeId masterNodeId = NodeId.nodeId("1"); |
| 60 | long termNumber = 10; |
| 61 | |
| 62 | MastershipTerm mastershipTerm = MastershipTerm.of(masterNodeId, termNumber); |
| 63 | ObjectNode mastershipTermJson = mastershipTermCodec.encode(mastershipTerm, context); |
| 64 | assertThat(mastershipTermJson, MastershipTermJsonMatcher.matchesMastershipTerm(mastershipTerm)); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Tests decoding of mastership term JSON object. |
| 69 | */ |
| 70 | @Test |
| 71 | public void testMastershipTermDecode() throws IOException { |
| 72 | MastershipTerm mastershipTerm = getMastershipTerm("MastershipTerm.json"); |
| 73 | |
| 74 | assertThat(mastershipTerm.master().id(), is("1")); |
| 75 | assertThat(mastershipTerm.termNumber(), is(10L)); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Hamcrest matcher for mastership term. |
| 80 | */ |
| 81 | private static final class MastershipTermJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 82 | |
| 83 | private final MastershipTerm mastershipTerm; |
| 84 | |
| 85 | private MastershipTermJsonMatcher(MastershipTerm mastershipTerm) { |
| 86 | this.mastershipTerm = mastershipTerm; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 91 | |
| 92 | // check node identifier of master |
| 93 | String jsonNodeId = jsonNode.get("master").asText(); |
| 94 | String nodeId = mastershipTerm.master().id(); |
| 95 | if (!jsonNodeId.equals(nodeId)) { |
| 96 | description.appendText("master's node id was " + jsonNodeId); |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | // check term number |
| 101 | long jsonTermNumber = jsonNode.get("termNumber").asLong(); |
| 102 | long termNumber = mastershipTerm.termNumber(); |
| 103 | if (jsonTermNumber != termNumber) { |
| 104 | description.appendText("term number was " + jsonTermNumber); |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void describeTo(Description description) { |
| 113 | description.appendText(mastershipTerm.toString()); |
| 114 | } |
| 115 | |
| 116 | static MastershipTermJsonMatcher matchesMastershipTerm(MastershipTerm mastershipTerm) { |
| 117 | return new MastershipTermJsonMatcher(mastershipTerm); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Reads in a mastership term from the given resource and decodes it. |
| 123 | * |
| 124 | * @param resourceName resource to use to read the JSON for the rule |
| 125 | * @return decoded mastership term object |
| 126 | * @throws IOException if processing the resource fails |
| 127 | */ |
| 128 | private MastershipTerm getMastershipTerm(String resourceName) throws IOException { |
| 129 | InputStream jsonStream = MastershipTermCodecTest.class.getResourceAsStream(resourceName); |
| 130 | JsonNode json = context.mapper().readTree(jsonStream); |
| 131 | assertThat(json, notNullValue()); |
| 132 | MastershipTerm mastershipTerm = mastershipTermCodec.decode((ObjectNode) json, context); |
| 133 | assertThat(mastershipTerm, notNullValue()); |
| 134 | return mastershipTerm; |
| 135 | } |
| 136 | } |