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.api.vocabulary.License; 019 020import java.io.IOException; 021 022import org.junit.jupiter.api.Test; 023 024import com.fasterxml.jackson.databind.ObjectMapper; 025import com.fasterxml.jackson.databind.annotation.JsonDeserialize; 026import com.fasterxml.jackson.databind.annotation.JsonSerialize; 027 028import static org.junit.jupiter.api.Assertions.assertEquals; 029import static org.junit.jupiter.api.Assertions.assertTrue; 030 031/** 032 * Test {@link License} serde using LicenseJsonSerializer and LicenseJsonDeserializer. 033 */ 034public class LicenseSerdeTest { 035 036 private static final ObjectMapper MAPPER = new ObjectMapper(); 037 038 @Test 039 public void testLicenseSerde() throws IOException { 040 LicenseWrapper license = new LicenseWrapper(License.CC0_1_0, "cc0"); 041 String json = MAPPER.writeValueAsString(license); 042 043 LicenseWrapper rebuiltLicense = MAPPER.readValue(json, LicenseWrapper.class); 044 assertEquals(license.getLicense(), rebuiltLicense.getLicense()); 045 } 046 047 /** 048 * Check the 3 cases where we can not serialize a URL for the license UNSPECIFIED, UNSUPPORTED and null. 049 */ 050 @Test 051 public void testLicenseSerdeNoUrl() throws IOException { 052 LicenseWrapper license = new LicenseWrapper(License.UNSPECIFIED, "cc0"); 053 String json = MAPPER.writeValueAsString(license); 054 assertTrue(json.contains("\"license\":\"unspecified\"")); 055 LicenseWrapper rebuiltLicense = MAPPER.readValue(json, LicenseWrapper.class); 056 assertEquals(license.getLicense(), rebuiltLicense.getLicense()); 057 058 license = new LicenseWrapper(License.UNSUPPORTED, "cc0"); 059 json = MAPPER.writeValueAsString(license); 060 assertTrue(json.contains("\"license\":\"unsupported\"")); 061 rebuiltLicense = MAPPER.readValue(json, LicenseWrapper.class); 062 assertEquals(license.getLicense(), rebuiltLicense.getLicense()); 063 064 license = new LicenseWrapper(null, "cc0"); 065 json = MAPPER.writeValueAsString(license); 066 assertTrue(json.contains("\"license\":null")); 067 rebuiltLicense = MAPPER.readValue(json, LicenseWrapper.class); 068 assertEquals(license.getLicense(), rebuiltLicense.getLicense()); 069 } 070 071 @Test 072 public void testFromLicenseName() throws IOException { 073 String json = "{\"license\":\"CC0_1_0\",\"text\":\"cc0\"}"; 074 LicenseWrapper rebuiltLicense = MAPPER.readValue(json, LicenseWrapper.class); 075 assertEquals(License.CC0_1_0, rebuiltLicense.getLicense()); 076 } 077 078 public static class LicenseWrapper { 079 private License license; 080 private String text; 081 082 public LicenseWrapper(){} 083 084 public LicenseWrapper(License license, String text){ 085 this.license = license; 086 this.text = text; 087 } 088 089 @JsonSerialize(using = LicenseSerde.LicenseJsonSerializer.class) 090 @JsonDeserialize(using = LicenseSerde.LicenseJsonDeserializer.class) 091 public License getLicense() { 092 return license; 093 } 094 095 public void setLicense(License license) { 096 this.license = license; 097 } 098 099 public String getText() { 100 return text; 101 } 102 103 public void setText(String text) { 104 this.text = text; 105 } 106 } 107}