Thomas Vachuska | d404c51 | 2014-10-23 14:19:46 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | d404c51 | 2014-10-23 14:19:46 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | d404c51 | 2014-10-23 14:19:46 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | d404c51 | 2014-10-23 14:19:46 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.codec; |
Thomas Vachuska | d404c51 | 2014-10-23 14:19:46 -0700 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import com.google.common.collect.ImmutableList; |
| 22 | import org.junit.Test; |
| 23 | |
| 24 | import java.util.List; |
| 25 | import java.util.Objects; |
| 26 | |
| 27 | import static org.junit.Assert.assertEquals; |
| 28 | |
| 29 | /** |
| 30 | * Test of the base JSON codec abstraction. |
| 31 | */ |
| 32 | public class JsonCodecTest { |
| 33 | |
| 34 | private static class Foo { |
| 35 | final String name; |
| 36 | |
| 37 | Foo(String name) { |
| 38 | this.name = name; |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public int hashCode() { |
| 43 | return Objects.hash(name); |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public boolean equals(Object obj) { |
| 48 | if (this == obj) { |
| 49 | return true; |
| 50 | } |
| 51 | if (obj == null || getClass() != obj.getClass()) { |
| 52 | return false; |
| 53 | } |
| 54 | final Foo other = (Foo) obj; |
| 55 | return Objects.equals(this.name, other.name); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | private static class FooCodec extends JsonCodec<Foo> { |
| 60 | @Override |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 61 | public ObjectNode encode(Foo entity, CodecContext context) { |
| 62 | return context.mapper().createObjectNode().put("name", entity.name); |
Thomas Vachuska | d404c51 | 2014-10-23 14:19:46 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @Override |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 66 | public Foo decode(ObjectNode json, CodecContext context) { |
Thomas Vachuska | d404c51 | 2014-10-23 14:19:46 -0700 | [diff] [blame] | 67 | return new Foo(json.get("name").asText()); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void encode() { |
| 73 | Foo f1 = new Foo("foo"); |
| 74 | Foo f2 = new Foo("bar"); |
| 75 | FooCodec codec = new FooCodec(); |
| 76 | ImmutableList<Foo> entities = ImmutableList.of(f1, f2); |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 77 | ArrayNode json = codec.encode(entities, new TestContext()); |
| 78 | List<Foo> foos = codec.decode(json, new TestContext()); |
Thomas Vachuska | d404c51 | 2014-10-23 14:19:46 -0700 | [diff] [blame] | 79 | assertEquals("incorrect encode/decode", entities, foos); |
| 80 | } |
| 81 | |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 82 | private class TestContext implements CodecContext { |
| 83 | private ObjectMapper mapper = new ObjectMapper(); |
| 84 | @Override |
| 85 | public ObjectMapper mapper() { |
| 86 | return mapper; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public <T> JsonCodec<T> codec(Class<T> entityClass) { |
| 91 | return null; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public <T> T get(Class<T> serviceClass) { |
| 96 | return null; |
| 97 | } |
| 98 | } |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 99 | } |