-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SDK-580: Return structured_postal_address when postal_address is missing
- Loading branch information
Showing
5 changed files
with
233 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
yoti-sdk-impl/src/main/java/com/yoti/api/client/spi/remote/AddressTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.yoti.api.client.spi.remote; | ||
|
||
import java.util.Map; | ||
|
||
import com.yoti.api.attributes.AttributeConstants.HumanProfileAttributes; | ||
import com.yoti.api.client.Attribute; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
class AddressTransformer { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(AttributeListConverter.class); | ||
|
||
private AddressTransformer() {} | ||
|
||
static final AddressTransformer newInstance() { | ||
return new AddressTransformer(); | ||
} | ||
|
||
public Attribute<String> transform(Attribute<?> structuredAddress) { | ||
Attribute<String> transformedAddress = null; | ||
try { | ||
Attribute<Map<?, ?>> address = (Attribute<Map<?, ?>>) structuredAddress; | ||
if (address.getValue() != null) { | ||
Object formattedAddress = address.getValue().get(HumanProfileAttributes.Keys.FORMATTED_ADDRESS); | ||
if (formattedAddress != null) { | ||
transformedAddress = new SimpleAttribute(HumanProfileAttributes.POSTAL_ADDRESS, | ||
String.valueOf(formattedAddress), | ||
structuredAddress.getSources(), | ||
structuredAddress.getVerifiers(), | ||
structuredAddress.getAnchors()); | ||
} | ||
} | ||
} catch (Exception e) { | ||
LOG.warn("Failed to transform attribute '{}' in place of missing '{}' due to '{}'", structuredAddress.getName(), | ||
HumanProfileAttributes.POSTAL_ADDRESS, e.getMessage()); | ||
} | ||
return transformedAddress; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
yoti-sdk-impl/src/test/java/com/yoti/api/client/spi/remote/AddressTransformerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.yoti.api.client.spi.remote; | ||
|
||
import static java.util.Arrays.asList; | ||
|
||
import static org.hamcrest.Matchers.hasItems; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.nullValue; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import com.yoti.api.attributes.AttributeConstants.HumanProfileAttributes; | ||
import com.yoti.api.client.Anchor; | ||
import com.yoti.api.client.Attribute; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class AddressTransformerTest { | ||
|
||
private static final String SOME_FORMATTED_ADDRESS = "someFormattedAddress"; | ||
|
||
@InjectMocks AddressTransformer testObj; | ||
|
||
@Mock Attribute<Map> structuredAddressMock; | ||
@Mock Attribute<Object> badAttributeMock; | ||
@Mock Anchor sourceAnchorMock; | ||
@Mock Anchor verifierAnchorMock; | ||
|
||
@Test | ||
public void shouldReturnNullWhenAddressValueIsNull() { | ||
Attribute<String> result = testObj.transform(structuredAddressMock); | ||
|
||
assertThat(result, is(nullValue())); | ||
} | ||
|
||
@Test | ||
public void shouldReturnNullWhenFormattedAddressIsNull() { | ||
when(structuredAddressMock.getValue()).thenReturn(new HashMap()); | ||
|
||
Attribute<String> result = testObj.transform(structuredAddressMock); | ||
|
||
assertThat(result, is(nullValue())); | ||
} | ||
|
||
@Test | ||
public void shouldReturnNullWhenTheresAnException() { | ||
when(badAttributeMock.getValue()).thenReturn(new Object()); | ||
|
||
Attribute<String> result = testObj.transform(badAttributeMock); | ||
|
||
assertThat(result, is(nullValue())); | ||
} | ||
|
||
@Test | ||
public void shouldReturnTransformedAddress() { | ||
Map<String, String> addressMap = new HashMap<>(); | ||
addressMap.put(HumanProfileAttributes.Keys.FORMATTED_ADDRESS, SOME_FORMATTED_ADDRESS); | ||
when(structuredAddressMock.getValue()).thenReturn(addressMap); | ||
when(structuredAddressMock.getSources()).thenReturn(asList(sourceAnchorMock)); | ||
when(structuredAddressMock.getVerifiers()).thenReturn(asList(verifierAnchorMock)); | ||
when(structuredAddressMock.getAnchors()).thenReturn(asList(sourceAnchorMock, verifierAnchorMock)); | ||
|
||
Attribute<String> result = testObj.transform(structuredAddressMock); | ||
|
||
assertThat(result.getName(), Matchers.is(HumanProfileAttributes.POSTAL_ADDRESS)); | ||
assertThat(result.getValue(), is(SOME_FORMATTED_ADDRESS)); | ||
assertThat(result.getSources(), hasItems(sourceAnchorMock)); | ||
assertThat(result.getVerifiers(), hasItems(verifierAnchorMock)); | ||
assertThat(result.getAnchors(), hasItems(sourceAnchorMock, verifierAnchorMock)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters