|
| 1 | +/* |
| 2 | + * Copyright 2024 Basis Technology Corp. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.basistech.rosette.apimodel.recordsimilarity; |
| 17 | + |
| 18 | + |
| 19 | +import com.basistech.rosette.apimodel.jackson.ApiModelMixinModule; |
| 20 | +import com.basistech.rosette.apimodel.recordsimilarity.records.AddressField; |
| 21 | +import com.basistech.rosette.apimodel.recordsimilarity.records.DateField; |
| 22 | +import com.basistech.rosette.apimodel.recordsimilarity.records.NameField; |
| 23 | +import com.basistech.rosette.apimodel.recordsimilarity.records.RecordFieldType; |
| 24 | +import com.basistech.util.ISO15924; |
| 25 | +import com.basistech.util.LanguageCode; |
| 26 | +import com.basistech.util.NEConstants; |
| 27 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 28 | +import com.fasterxml.jackson.databind.MapperFeature; |
| 29 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 30 | +import com.fasterxml.jackson.databind.SerializationFeature; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | + |
| 33 | +import java.util.List; |
| 34 | +import java.util.Map; |
| 35 | + |
| 36 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 37 | + |
| 38 | +public class RecordSimilarityResponseTest { |
| 39 | + |
| 40 | + private static final ObjectMapper MAPPER = ApiModelMixinModule.setupObjectMapper(new ObjectMapper()); |
| 41 | + |
| 42 | + private static final String EXPECTED_JSON = "{\"fields\":{\"addr\":{\"type\":\"rni_address\",\"weight\":0.3},\"dob\":{\"type\":\"rni_date\",\"weight\":0.2},\"primaryName\":{\"type\":\"rni_name\",\"weight\":0.5}},\"results\":[{\"explainInfo\":{\"leftOnlyFields\":[\"addr\"],\"scoredFields\":{\"dob\":{\"calculatedWeight\":0.2857142857142857,\"finalScore\":0.74,\"rawScore\":0.8,\"weight\":0.5},\"primaryName\":{\"calculatedWeight\":0.7142857142857143,\"details\":\"any details\",\"finalScore\":0.85,\"rawScore\":0.99,\"weight\":0.5}}},\"left\":{\"addr\":{\"address\":\"123 Roadlane Ave\"},\"dob\":{\"date\":\"1993-04-16\"},\"primaryName\":{\"entityType\":\"PERSON\",\"language\":\"eng\",\"languageOfOrigin\":\"eng\",\"script\":\"Latn\",\"text\":\"Ethan R\"}},\"right\":{\"dob\":\"1993-04-16\",\"primaryName\":{\"text\":\"Seth R\"}},\"score\":0.87},{\"error\":\"Field foo not found in field mapping\",\"left\":{\"addr\":{\"address\":\"123 Roadlane Ave\"},\"dob\":{\"date\":\"1993-04-16\"},\"primaryName\":{\"entityType\":\"PERSON\",\"language\":\"eng\",\"languageOfOrigin\":\"eng\",\"script\":\"Latn\",\"text\":\"Ethan R\"}},\"right\":{\"dob\":\"1993-04-16\",\"primaryName\":{\"text\":\"Seth R\"}}}]}"; |
| 43 | + |
| 44 | + private static final RecordSimilarityResponse EXPECTED_RESPONSE; |
| 45 | + |
| 46 | + static { |
| 47 | + RecordSimilarityResponse temp; |
| 48 | + try { |
| 49 | + temp = RecordSimilarityResponse.builder() |
| 50 | + .fields(Map.of("primaryName", RecordSimilarityFieldInfo.builder() |
| 51 | + .type(RecordFieldType.NAME) |
| 52 | + .weight(0.5) |
| 53 | + .build(), |
| 54 | + "dob", RecordSimilarityFieldInfo.builder() |
| 55 | + .type(RecordFieldType.DATE) |
| 56 | + .weight(0.2) |
| 57 | + .build(), |
| 58 | + "addr", RecordSimilarityFieldInfo.builder() |
| 59 | + .type(RecordFieldType.ADDRESS) |
| 60 | + .weight(0.3) |
| 61 | + .build())) |
| 62 | + .results(List.of(RecordSimilarityResult.builder() |
| 63 | + .score(0.87) |
| 64 | + .left(Map.of("primaryName", NameField.FieldedName.builder() |
| 65 | + .text("Ethan R") |
| 66 | + .language(LanguageCode.ENGLISH) |
| 67 | + .entityType(NEConstants.toString(NEConstants.NE_TYPE_PERSON)) |
| 68 | + .languageOfOrigin(LanguageCode.ENGLISH) |
| 69 | + .script(ISO15924.Latn) |
| 70 | + .build(), |
| 71 | + "dob", DateField.FieldedDate.builder() |
| 72 | + .date("1993-04-16") |
| 73 | + .build(), |
| 74 | + "addr", AddressField.FieldedAddress.builder() |
| 75 | + .address("123 Roadlane Ave") |
| 76 | + .build())) |
| 77 | + .right(Map.of("primaryName", NameField.FieldedName.builder() |
| 78 | + .text("Seth R") |
| 79 | + .build(), |
| 80 | + "dob", DateField.UnfieldedDate.builder() |
| 81 | + .date("1993-04-16") |
| 82 | + .build())) |
| 83 | + .explainInfo(RecordSimilarityExplainInfo.builder() |
| 84 | + .leftOnlyFields(List.of("addr")) |
| 85 | + .scoredFields(Map.of("dob", RecordSimilarityFieldExplainInfo.builder() |
| 86 | + .weight(0.5) |
| 87 | + .calculatedWeight(0.2857142857142857) |
| 88 | + .rawScore(0.8) |
| 89 | + .finalScore(0.74) |
| 90 | + .build(), |
| 91 | + "primaryName", |
| 92 | + RecordSimilarityFieldExplainInfo.builder() |
| 93 | + .weight(0.5) |
| 94 | + .calculatedWeight(0.7142857142857143) |
| 95 | + .rawScore(0.99) |
| 96 | + .finalScore(0.85) |
| 97 | + .details(MAPPER.readTree("\"any details\"")) |
| 98 | + .build() |
| 99 | + )) |
| 100 | + .build()) |
| 101 | + .build(), |
| 102 | + RecordSimilarityResult.builder() |
| 103 | + .left(Map.of("primaryName", NameField.FieldedName.builder() |
| 104 | + .text("Ethan R") |
| 105 | + .language(LanguageCode.ENGLISH) |
| 106 | + .entityType(NEConstants.toString(NEConstants.NE_TYPE_PERSON)) |
| 107 | + .languageOfOrigin(LanguageCode.ENGLISH) |
| 108 | + .script(ISO15924.Latn) |
| 109 | + .build(), |
| 110 | + "dob", DateField.FieldedDate.builder() |
| 111 | + .date("1993-04-16") |
| 112 | + .build(), |
| 113 | + "addr", AddressField.FieldedAddress.builder() |
| 114 | + .address("123 Roadlane Ave") |
| 115 | + .build())) |
| 116 | + .right(Map.of("primaryName", NameField.FieldedName.builder() |
| 117 | + .text("Seth R") |
| 118 | + .build(), |
| 119 | + "dob", DateField.UnfieldedDate.builder() |
| 120 | + .date("1993-04-16") |
| 121 | + .build())) |
| 122 | + .error("Field foo not found in field mapping") |
| 123 | + .build())) |
| 124 | + .build(); |
| 125 | + } catch (JsonProcessingException e) { |
| 126 | + temp = RecordSimilarityResponse.builder().build(); |
| 127 | + } |
| 128 | + EXPECTED_RESPONSE = temp; |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testDeserialization() throws JsonProcessingException { |
| 133 | + final RecordSimilarityResponse response = MAPPER.readValue(EXPECTED_JSON, RecordSimilarityResponse.class); |
| 134 | + assertEquals(EXPECTED_RESPONSE, response); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void testSerialization() throws JsonProcessingException { |
| 139 | + // For testing, force ordering |
| 140 | + MAPPER.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY); |
| 141 | + MAPPER.enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS); |
| 142 | + assertEquals(EXPECTED_JSON, MAPPER.writeValueAsString(EXPECTED_RESPONSE)); |
| 143 | + } |
| 144 | +} |
0 commit comments