|
| 1 | +package com.algolia.search.serialization; |
| 2 | + |
| 3 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 4 | + |
| 5 | +import com.algolia.search.Defaults; |
| 6 | +import com.algolia.search.models.rules.AutomaticFacetFilter; |
| 7 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 8 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 9 | +import java.util.List; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +public class TestAutomaticFacetFilterDeserializer { |
| 13 | + |
| 14 | + @Test |
| 15 | + void testDeserializer() throws JsonProcessingException { |
| 16 | + String json = "{\"facet\":\"brand_name\",\"disjunctive\":true,\"score\":10}"; |
| 17 | + AutomaticFacetFilter automaticFacetFilter = |
| 18 | + Defaults.getObjectMapper().readValue(json, AutomaticFacetFilter.class); |
| 19 | + |
| 20 | + assertThat(automaticFacetFilter.getFacet()).isEqualTo("brand_name"); |
| 21 | + assertThat(automaticFacetFilter.getDisjunctive()).isTrue(); |
| 22 | + assertThat(automaticFacetFilter.getScore()).isEqualTo(10); |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + void testDeserializerOneAttribute() throws JsonProcessingException { |
| 27 | + String json = "{\"facet\":\"brand_name\"}"; |
| 28 | + AutomaticFacetFilter automaticFacetFilter = |
| 29 | + Defaults.getObjectMapper().readValue(json, AutomaticFacetFilter.class); |
| 30 | + |
| 31 | + assertThat(automaticFacetFilter.getFacet()).isEqualTo("brand_name"); |
| 32 | + assertThat(automaticFacetFilter.getDisjunctive()).isFalse(); |
| 33 | + assertThat(automaticFacetFilter.getScore()).isNull(); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void testDeserializerLegacy() throws JsonProcessingException { |
| 38 | + String json = "\"brand_name\""; |
| 39 | + AutomaticFacetFilter automaticFacetFilter = |
| 40 | + Defaults.getObjectMapper().readValue(json, AutomaticFacetFilter.class); |
| 41 | + |
| 42 | + assertThat(automaticFacetFilter.getFacet()).isEqualTo("brand_name"); |
| 43 | + assertThat(automaticFacetFilter.getDisjunctive()).isFalse(); |
| 44 | + assertThat(automaticFacetFilter.getScore()).isNull(); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void testDeserializerLegacyList() throws JsonProcessingException { |
| 49 | + String json = "[\"lastname\",\"firstname\"]"; |
| 50 | + List<AutomaticFacetFilter> deserialized = |
| 51 | + Defaults.getObjectMapper() |
| 52 | + .readValue(json, new TypeReference<List<AutomaticFacetFilter>>() {}); |
| 53 | + |
| 54 | + AutomaticFacetFilter lastname = deserialized.get(0); |
| 55 | + assertThat(lastname.getFacet()).isEqualTo("lastname"); |
| 56 | + assertThat(lastname.getDisjunctive()).isFalse(); |
| 57 | + assertThat(lastname.getScore()).isNull(); |
| 58 | + |
| 59 | + AutomaticFacetFilter firstname = deserialized.get(1); |
| 60 | + assertThat(firstname.getFacet()).isEqualTo("firstname"); |
| 61 | + assertThat(firstname.getDisjunctive()).isFalse(); |
| 62 | + assertThat(firstname.getScore()).isNull(); |
| 63 | + } |
| 64 | +} |
0 commit comments