001/* 002 * Licensed under the Apache License, Version 2.0 (the "License"); 003 * you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, 010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 011 * See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014package org.gbif.api.model.predicate; 015 016import com.fasterxml.jackson.databind.ObjectMapper; 017import com.fasterxml.jackson.databind.module.SimpleModule; 018import java.io.File; 019import java.io.IOException; 020import org.gbif.api.model.common.search.SearchParameter; 021import org.gbif.api.model.event.search.EventSearchParameter; 022import org.gbif.api.model.occurrence.search.OccurrenceSearchParameter; 023import org.junit.jupiter.api.Assertions; 024import org.junit.jupiter.api.Test; 025 026/** Test cases and examples of serialization or predicates using a mixing. */ 027public class PredicateDeSerTest { 028 029 private static final ObjectMapper MAPPER = new ObjectMapper(); 030 031 static { 032 MAPPER.registerModule( 033 new SimpleModule() 034 .addDeserializer( 035 SearchParameter.class, new SearchParameter.SearchParameterDeserializer()) 036 .addDeserializer( 037 OccurrenceSearchParameter.class, 038 new OccurrenceSearchParameter.OccurrenceSearchParameterDeserializer()) 039 .addDeserializer( 040 EventSearchParameter.class, 041 new EventSearchParameter.EventSearchParameterDeserializer())); 042 } 043 044 private File getTestFile(String predicateFile) { 045 return new File(getClass().getResource("/predicate/" + predicateFile).getFile()); 046 } 047 048 @Test 049 public void deserTest() throws IOException { 050 EqualsPredicate<OccurrenceSearchParameter> eq = 051 new EqualsPredicate<>(OccurrenceSearchParameter.OCCURRENCE_STATUS, "present", false); 052 System.out.println(MAPPER.writeValueAsString(eq)); 053 054 assertPredicate("is_null.json"); 055 assertPredicate("conjunction.json"); 056 assertPredicate("within.json"); 057 assertPredicate("equals_catalog_number.json"); 058 assertPredicate("equals_date_range.json"); 059 assertPredicate("like_catalog_number.json"); 060 assertPredicate("and_with_not.json"); 061 assertPredicate("conjunction_with_in.json"); 062 assertPredicate("complex_conjunction_with_in.json"); 063 assertPredicate("range.json"); 064 assertPredicate("distance.json"); 065 assertPredicate("is_null_event.json"); 066 } 067 068 @Test 069 public void eventSearchParameterTest() throws IOException { 070 ObjectMapper eventsMapper = 071 new ObjectMapper() 072 .registerModule( 073 new SimpleModule() 074 .addDeserializer( 075 SearchParameter.class, 076 new EventSearchParameter.EventSearchParameterDeserializer()) 077 .addDeserializer( 078 EventSearchParameter.class, 079 new EventSearchParameter.EventSearchParameterDeserializer())); 080 081 Predicate predicate = eventsMapper.readValue(getTestFile("equals_event.json"), Predicate.class); 082 Assertions.assertEquals( 083 EventSearchParameter.YEAR, ((EqualsPredicate<EventSearchParameter>) predicate).getKey()); 084 } 085 086 private void assertPredicate(String fileName) throws IOException { 087 Predicate predicate = MAPPER.readValue(getTestFile(fileName), Predicate.class); 088 Assertions.assertNotNull(predicate); 089 } 090}