001/*
002 * Copyright 2020 Global Biodiversity Information Facility (GBIF)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.gbif.api.jackson;
017
018import org.gbif.dwc.terms.DwcTerm;
019import org.gbif.dwc.terms.Term;
020
021import java.io.IOException;
022import java.util.AbstractMap;
023import java.util.Arrays;
024import java.util.Collections;
025import java.util.List;
026import java.util.Map;
027
028import org.junit.jupiter.api.Test;
029
030import com.fasterxml.jackson.databind.ObjectMapper;
031
032import static org.junit.jupiter.api.Assertions.assertEquals;
033
034/**
035 * Unit tests for {@link MapEntrySerde}.
036 */
037public class MapEntrySerdeTest {
038
039  private static final ObjectMapper MAPPER = new ObjectMapper();
040
041  @Test
042  public void testMapEntrySerde() throws IOException {
043
044    MapEntryWrapper kv = new MapEntryWrapper("mykey", 18);
045    String json = MAPPER.writeValueAsString(kv);
046
047    MapEntryWrapper rebuiltKeyValue = MAPPER.readValue(json, MapEntryWrapper.class);
048    assertEquals(kv.getSingleElement().getValue(), rebuiltKeyValue.getSingleElement().getValue());
049
050    MapEntryListTermWrapper kvl = new MapEntryListTermWrapper(DwcTerm.country, 19);
051    json = MAPPER.writeValueAsString(kvl);
052
053    MapEntryListTermWrapper rebuiltKeyValueList = MAPPER.readValue(json, MapEntryListTermWrapper.class);
054    assertEquals(2, rebuiltKeyValueList.getListTerm().size());
055  }
056
057  /**
058   * For testing purpose only.
059   */
060  static class MapEntryWrapper {
061
062    private Map.Entry<Object, Object> singleElement;
063
064    public MapEntryWrapper() {}
065
066    public MapEntryWrapper(Object key, Object value){
067      singleElement = new AbstractMap.SimpleImmutableEntry<>(key, value);
068    }
069
070    public Map.Entry<Object, Object> getSingleElement() {
071      return singleElement;
072    }
073
074    public void setSingleElement(Map.Entry<Object, Object> singleElement) {
075      this.singleElement = singleElement;
076    }
077  }
078
079  static class MapEntryListTermWrapper {
080
081    private List<Map.Entry<Term, Object>> listTerm;
082
083    public MapEntryListTermWrapper() {}
084
085    public MapEntryListTermWrapper(Term key, Object value){
086      listTerm = Collections.unmodifiableList(
087        Arrays.asList(
088          new AbstractMap.SimpleImmutableEntry<>(key, value),
089          new AbstractMap.SimpleImmutableEntry<>(key, value)));
090    }
091
092    public List<Map.Entry<Term, Object>> getListTerm() {
093      return listTerm;
094    }
095
096    public void setListTerm(List<Map.Entry<Term, Object>> listTerm) {
097      this.listTerm = listTerm;
098    }
099  }
100}