From 4ee25d5a2d88c5c77be834f26681b9a08de44f4e Mon Sep 17 00:00:00 2001 From: Baikov Dmitrii <44803026+D-Baykov@users.noreply.github.com> Date: Thu, 15 Oct 2020 13:06:34 +0300 Subject: [PATCH] PROX-461: Add IpGenerator (#34) --- build_utils | 2 +- pom.xml | 2 +- .../common/utils/generator/IpGenerator.java | 35 +++++++++++++++++++ .../utils/generator/IpGeneratorTest.java | 26 ++++++++++++++ 4 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/rbkmoney/adapter/common/utils/generator/IpGenerator.java create mode 100644 src/test/java/com/rbkmoney/adapter/common/utils/generator/IpGeneratorTest.java diff --git a/build_utils b/build_utils index 34f432a..f42e059 160000 --- a/build_utils +++ b/build_utils @@ -1 +1 @@ -Subproject commit 34f432a1e8e0adedbba23ecd29c1eb2412a8d416 +Subproject commit f42e059d9ec93826ba4ad23232eed8ce67bd5486 diff --git a/pom.xml b/pom.xml index 75ecd51..fe217ff 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ adapter-common-lib - 0.0.27 + 0.0.28 jar adapter-common-lib diff --git a/src/main/java/com/rbkmoney/adapter/common/utils/generator/IpGenerator.java b/src/main/java/com/rbkmoney/adapter/common/utils/generator/IpGenerator.java new file mode 100644 index 0000000..a23e952 --- /dev/null +++ b/src/main/java/com/rbkmoney/adapter/common/utils/generator/IpGenerator.java @@ -0,0 +1,35 @@ +package com.rbkmoney.adapter.common.utils.generator; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.util.StringUtils; + +import java.net.Inet6Address; +import java.net.InetAddress; +import java.net.UnknownHostException; + +@Slf4j +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class IpGenerator { + + public static final String DEFAULT_IP_ADDRESS = "127.0.0.1"; + + public static String checkAndGenerate(String ip) { + return checkAndGenerate(ip, DEFAULT_IP_ADDRESS); + } + + public static String checkAndGenerate(String ip, String defaultIpAddress) { + try { + InetAddress address = InetAddress.getByName(ip); + if (StringUtils.isEmpty(ip) || address instanceof Inet6Address) { + return defaultIpAddress; + } + } catch (UnknownHostException e) { + log.error("Error when convert ipAddress: {}", ip, e); + return defaultIpAddress; + } + return ip; + } + +} diff --git a/src/test/java/com/rbkmoney/adapter/common/utils/generator/IpGeneratorTest.java b/src/test/java/com/rbkmoney/adapter/common/utils/generator/IpGeneratorTest.java new file mode 100644 index 0000000..b606b34 --- /dev/null +++ b/src/test/java/com/rbkmoney/adapter/common/utils/generator/IpGeneratorTest.java @@ -0,0 +1,26 @@ +package com.rbkmoney.adapter.common.utils.generator; + +import org.junit.Assert; +import org.junit.Test; + +public final class IpGeneratorTest { + + public static final String IP_V4 = "123.0.0.2"; + + @Test + public void checkAndGenerate() { + String expectedIp = "127.0.0.1"; + String resultIp = IpGenerator.checkAndGenerate("2001:0db8:85a3:0000:0000:8a2e:0370:7334"); + Assert.assertEquals(expectedIp, resultIp); + + resultIp = IpGenerator.checkAndGenerate(""); + Assert.assertEquals(expectedIp, resultIp); + + resultIp = IpGenerator.checkAndGenerate(null); + Assert.assertEquals(expectedIp, resultIp); + + resultIp = IpGenerator.checkAndGenerate(IP_V4); + Assert.assertEquals(IP_V4, resultIp); + } + +}