blob: ebe5f8e04aba43ea15f242884c027eddde6dccae [file] [log] [blame]
Thomas Vachuskad404c512014-10-23 14:19:46 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuskad404c512014-10-23 14:19:46 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuskad404c512014-10-23 14:19:46 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuskad404c512014-10-23 14:19:46 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.codec;
Thomas Vachuskad404c512014-10-23 14:19:46 -070017
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import com.google.common.collect.ImmutableList;
22import org.junit.Test;
23
24import java.util.List;
25import java.util.Objects;
26
27import static org.junit.Assert.assertEquals;
28
29/**
30 * Test of the base JSON codec abstraction.
31 */
32public 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 Vachuskaca60f2b2014-11-06 01:34:28 -080061 public ObjectNode encode(Foo entity, CodecContext context) {
62 return context.mapper().createObjectNode().put("name", entity.name);
Thomas Vachuskad404c512014-10-23 14:19:46 -070063 }
64
65 @Override
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080066 public Foo decode(ObjectNode json, CodecContext context) {
Thomas Vachuskad404c512014-10-23 14:19:46 -070067 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 Vachuskaca60f2b2014-11-06 01:34:28 -080077 ArrayNode json = codec.encode(entities, new TestContext());
78 List<Foo> foos = codec.decode(json, new TestContext());
Thomas Vachuskad404c512014-10-23 14:19:46 -070079 assertEquals("incorrect encode/decode", entities, foos);
80 }
81
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080082 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'Connorabafb502014-12-02 22:26:20 -080099}