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.codec.JsonCodec; |
| 25 | import org.onosproject.net.MastershipRole; |
| 26 | |
| 27 | import java.io.IOException; |
| 28 | import java.io.InputStream; |
| 29 | |
| 30 | import static org.hamcrest.MatcherAssert.assertThat; |
| 31 | import static org.hamcrest.Matchers.is; |
| 32 | import static org.hamcrest.Matchers.notNullValue; |
| 33 | import static org.onosproject.net.MastershipRole.MASTER; |
| 34 | |
| 35 | /** |
| 36 | * Unit tests for mastership role codec. |
| 37 | */ |
| 38 | public final class MastershipRoleCodecTest { |
| 39 | |
| 40 | MockCodecContext context; |
| 41 | JsonCodec<MastershipRole> mastershipRoleCodec; |
| 42 | |
| 43 | /** |
| 44 | * Sets up for each test. Creates a context and fetches the mastership role |
| 45 | * codec. |
| 46 | */ |
| 47 | @Before |
| 48 | public void setUp() { |
| 49 | context = new MockCodecContext(); |
| 50 | mastershipRoleCodec = context.codec(MastershipRole.class); |
| 51 | assertThat(mastershipRoleCodec, notNullValue()); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Tests encoding of a mastership role object. |
| 56 | */ |
| 57 | @Test |
| 58 | public void testMastershipRoleEncode() { |
| 59 | MastershipRole mastershipRole = MASTER; |
| 60 | ObjectNode mastershipRoleJson = mastershipRoleCodec.encode(mastershipRole, context); |
| 61 | assertThat(mastershipRoleJson, MastershipRoleJsonMatcher.matchesMastershipRole(mastershipRole)); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Tests decoding of mastership role JSON object. |
| 66 | */ |
| 67 | @Test |
| 68 | public void testMastershipRoleDecode() throws IOException { |
| 69 | MastershipRole mastershipRole = getMastershipRole("MastershipRole.json"); |
| 70 | |
| 71 | assertThat(mastershipRole, is(MASTER)); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Hamcrest matcher for mastership role. |
| 76 | */ |
| 77 | private static final class MastershipRoleJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 78 | |
| 79 | private final MastershipRole mastershipRole; |
| 80 | |
| 81 | private MastershipRoleJsonMatcher(MastershipRole mastershipRole) { |
| 82 | this.mastershipRole = mastershipRole; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 87 | |
| 88 | // check mastership role |
| 89 | String jsonRole = jsonNode.get("role").asText(); |
| 90 | String role = mastershipRole.name(); |
| 91 | if (!jsonRole.equals(role)) { |
| 92 | description.appendText("mastership role was " + jsonRole); |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public void describeTo(Description description) { |
| 101 | description.appendText(mastershipRole.toString()); |
| 102 | } |
| 103 | |
| 104 | static MastershipRoleJsonMatcher matchesMastershipRole(MastershipRole mastershipRole) { |
| 105 | return new MastershipRoleJsonMatcher(mastershipRole); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Reads in a mastership role from the given resource and decodes it. |
| 111 | * |
| 112 | * @param resourceName resource to use to read the JSON for the rule |
| 113 | * @return decoded mastership term object |
| 114 | * @throws IOException if processing the resource fails |
| 115 | */ |
| 116 | private MastershipRole getMastershipRole(String resourceName) throws IOException { |
| 117 | InputStream jsonStream = MastershipRoleCodecTest.class.getResourceAsStream(resourceName); |
| 118 | JsonNode json = context.mapper().readTree(jsonStream); |
| 119 | assertThat(json, notNullValue()); |
| 120 | MastershipRole mastershipRole = mastershipRoleCodec.decode((ObjectNode) json, context); |
| 121 | assertThat(mastershipRole, notNullValue()); |
| 122 | return mastershipRole; |
| 123 | } |
| 124 | } |