001/* 002 * Copyright 2021 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.dwc.terms.jackson; 017 018import org.gbif.dwc.terms.DwcTerm; 019import org.gbif.dwc.terms.GbifTerm; 020import org.gbif.dwc.terms.Term; 021import org.gbif.dwc.terms.UnknownTerm; 022 023import java.io.IOException; 024import java.util.HashMap; 025import java.util.Map; 026 027import org.junit.jupiter.api.Test; 028 029import com.fasterxml.jackson.databind.ObjectMapper; 030import com.fasterxml.jackson.databind.annotation.JsonDeserialize; 031import com.fasterxml.jackson.databind.annotation.JsonSerialize; 032 033import static org.junit.jupiter.api.Assertions.assertEquals; 034 035public class TermDeserializerTest { 036 037 /** 038 * Test class resembling a verbatim occurrence field map that causes json serde trouble. 039 */ 040 static class Occ { 041 private Term term; 042 private Map<Term, String> data = new HashMap<>(); 043 044 @JsonSerialize(keyUsing = TermKeySerializer.class) 045 @JsonDeserialize(keyUsing = TermKeyDeserializer.class) 046 public Map<Term, String> getData() { 047 return data; 048 } 049 050 public void setData(Map<Term, String> data) { 051 this.data = data; 052 } 053 054 public Term getTerm() { 055 return term; 056 } 057 058 public void setTerm(Term term) { 059 this.term = term; 060 } 061 062 @Override 063 public boolean equals(Object o) { 064 if (this == o) return true; 065 if (!(o instanceof Occ)) return false; 066 067 Occ occ = (Occ) o; 068 069 if (!data.equals(occ.data)) return false; 070 if (!term.equals(occ.term)) return false; 071 072 return true; 073 } 074 075 @Override 076 public int hashCode() { 077 int result = term.hashCode(); 078 result = 31 * result + data.hashCode(); 079 return result; 080 } 081 082 } 083 084 @Test 085 public void testTermSerde() throws IOException { 086 ObjectMapper mapper = new ObjectMapper(); 087 088 for (Term t : DwcTerm.values()) { 089 String json = mapper.writeValueAsString(t); 090 assertEquals(t, mapper.readValue(json, Term.class)); 091 } 092 093 for (Term t : GbifTerm.values()) { 094 String json = mapper.writeValueAsString(t); 095 assertEquals(t, mapper.readValue(json, Term.class)); 096 } 097 } 098 099 @Test 100 public void testTermMap() throws IOException { 101 ObjectMapper mapper = new ObjectMapper(); 102 int counter = 0; 103 Map<Term, Integer> terms = new HashMap<>(); 104 for (Term t : DwcTerm.values()) { 105 terms.put(t, counter++); 106 } 107 108 for (Term t : GbifTerm.values()) { 109 terms.put(t, counter++); 110 } 111 112 String json = mapper.writeValueAsString(terms); 113 assertEquals(terms.size(), mapper.readValue(json, HashMap.class).size()); 114 } 115 116 @Test 117 public void testOcc() throws IOException { 118 ObjectMapper mapper = new ObjectMapper(); 119 120 final Term custom = UnknownTerm.build("http://me.com/mum", "mum"); 121 Occ o = new Occ(); 122 o.setTerm(custom); 123 o.getData().put(custom, "custom"); 124 o.getData().put(GbifTerm.ageInDays, "21"); 125 126 for (Term t : DwcTerm.values()) { 127 o.getData().put(t, t.simpleName()); 128 } 129 130 String json = mapper.writeValueAsString(o); 131 Occ o2 = mapper.readValue(json, Occ.class); 132 System.out.println(o.equals(o2)); 133 134 assertEquals(o, mapper.readValue(json, Occ.class)); 135 } 136}