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.Rank;
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;
029
030/**
031 * Test {@link Rank} serde using LicenseJsonSerializer and LicenseJsonDeserializer.
032 *
033 */
034public class RankSerdeTest {
035
036  private static final ObjectMapper MAPPER = new ObjectMapper();
037
038  @Test
039  public void testRoundtrip() throws IOException {
040
041    for (Rank r : Rank.values()) {
042      RankWrapper rank = new RankWrapper(r);
043      String json = MAPPER.writeValueAsString(rank);
044      assertEquals(rank.rank, MAPPER.readValue(json, RankWrapper.class).rank);
045    }
046  }
047
048  public static class RankWrapper {
049    @JsonSerialize(using = RankSerde.RankJsonSerializer.class)
050    @JsonDeserialize(using = RankSerde.RankJsonDeserializer.class)
051    public Rank rank;
052
053    public RankWrapper(){}
054
055    public RankWrapper(Rank rank){
056      this.rank = rank;
057    }
058  }
059}