From e611db2e598caf170847e03840efce4bca28a33b Mon Sep 17 00:00:00 2001 From: Lubos Racansky Date: Mon, 11 Dec 2023 09:20:01 +0100 Subject: [PATCH] Refactor to parametrized test --- .../http/common/headers/UserAgentTest.java | 157 +++++++++--------- 1 file changed, 83 insertions(+), 74 deletions(-) diff --git a/http-common/src/test/java/com/wultra/core/http/common/headers/UserAgentTest.java b/http-common/src/test/java/com/wultra/core/http/common/headers/UserAgentTest.java index 3a4584a..b5288f0 100644 --- a/http-common/src/test/java/com/wultra/core/http/common/headers/UserAgentTest.java +++ b/http-common/src/test/java/com/wultra/core/http/common/headers/UserAgentTest.java @@ -17,7 +17,11 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.stream.Stream; import java.util.Optional; @@ -31,82 +35,87 @@ */ class UserAgentTest { - private static final String[] USER_AGENTS = new String[] { - "PowerAuthNetworking/1.1.7 (en; cellular) com.wultra.app.Mobile-Token.wultra_test/2.0.0 (Apple; iOS/16.6.1; iphone12,3)", - "PowerAuthNetworking/1.2.1 (uk; wifi) com.wultra.android.mtoken.gdnexttest/1.0.0-gdnexttest (samsung; Android/13; SM-A047F)", - "PowerAuthNetworking/1.1.7 (en; unknown) com.wultra.app.MobileToken.wtest/2.0.0 (Apple; iOS/16.6.1; iphone10,6)", - "PowerAuthNetworking/1.1.7 (en; wifi) com.wultra.app.MobileToken.wtest/2.0.0 (Apple; iOS/16.7.1; iphone10,6)", - // MainBundle/Version PowerAuth2/Version (iOS Version, deviceString) - "PowerAuth2TestsHostApp-ios/1.0 PowerAuth2/1.7.8 (iOS 17.0, simulator)", - // PowerAuth2/Version (Android Version, Build.MANUFACTURER Build.MODEL) - "PowerAuth2/1.7.8 (Android 13, Google Pixel 4)", - "MobileToken/1.2.0 PowerAuth2/1.7.8 (iOS 15.7.9, iPhone9,3)" - }; - - private static final String DEVICES = """ - [ - { - "networkVersion": "1.1.7", - "language": "en", - "connection": "cellular", - "product": "com.wultra.app.Mobile-Token.wultra_test", - "version": "2.0.0", - "platform": "Apple", - "os": "iOS", - "osVersion": "16.6.1", - "model": "iphone12,3" - }, - { - "networkVersion": "1.2.1", - "language": "uk", - "connection": "wifi", - "product": "com.wultra.android.mtoken.gdnexttest", - "version": "1.0.0-gdnexttest", - "platform": "samsung", - "os": "Android", - "osVersion": "13", - "model": "SM-A047F" - }, - { - "networkVersion": "1.1.7", - "language": "en", - "connection": "unknown", - "product": "com.wultra.app.MobileToken.wtest", - "version": "2.0.0", - "platform": "Apple", - "os": "iOS", - "osVersion": "16.6.1", - "model": "iphone10,6" - }, - { - "networkVersion": "1.1.7", - "language": "en", - "connection": "wifi", - "product": "com.wultra.app.MobileToken.wtest", - "version": "2.0.0", - "platform": "Apple", - "os": "iOS", - "osVersion": "16.7.1", - "model": "iphone10,6" - } - ] - """; - - @Test - void testParse() throws Exception { - final UserAgent.Device[] expectedDevices = readDevices(); - for (int i = 0; i < USER_AGENTS.length; i++) { - final Optional deviceOptional = UserAgent.parse(USER_AGENTS[i]); - assertTrue(deviceOptional.isPresent()); + @ParameterizedTest + @MethodSource("provideUserAgents") + void testParse(final String userAgent, final UserAgent.Device expectedDevice) { + final Optional deviceOptional = UserAgent.parse(userAgent); + assertTrue(deviceOptional.isPresent()); + assertEquals(expectedDevice, deviceOptional.get()); + } - final UserAgent.Device expectedDevice = expectedDevices[i]; - assertEquals(expectedDevice, deviceOptional.get()); - } + private static Stream provideUserAgents() throws JsonProcessingException { + return Stream.of( + Arguments.of("PowerAuthNetworking/1.1.7 (en; cellular) com.wultra.app.Mobile-Token.wultra_test/2.0.0 (Apple; iOS/16.6.1; iphone12,3)", readDevice(""" + { + "networkVersion": "1.1.7", + "language": "en", + "connection": "cellular", + "product": "com.wultra.app.Mobile-Token.wultra_test", + "version": "2.0.0", + "platform": "Apple", + "os": "iOS", + "osVersion": "16.6.1", + "model": "iphone12,3" + } + """)), + Arguments.of("PowerAuthNetworking/1.2.1 (uk; wifi) com.wultra.android.mtoken.gdnexttest/1.0.0-gdnexttest (samsung; Android/13; SM-A047F)", readDevice(""" + { + "networkVersion": "1.2.1", + "language": "uk", + "connection": "wifi", + "product": "com.wultra.android.mtoken.gdnexttest", + "version": "1.0.0-gdnexttest", + "platform": "samsung", + "os": "Android", + "osVersion": "13", + "model": "SM-A047F" + } + """)), + Arguments.of("PowerAuthNetworking/1.1.7 (en; unknown) com.wultra.app.MobileToken.wtest/2.0.0 (Apple; iOS/16.6.1; iphone10,6)", readDevice(""" + { + "networkVersion": "1.1.7", + "language": "en", + "connection": "unknown", + "product": "com.wultra.app.MobileToken.wtest", + "version": "2.0.0", + "platform": "Apple", + "os": "iOS", + "osVersion": "16.6.1", + "model": "iphone10,6" + } + """)), + Arguments.of("PowerAuthNetworking/1.1.7 (en; wifi) com.wultra.app.MobileToken.wtest/2.0.0 (Apple; iOS/16.7.1; iphone10,6)", readDevice(""" + { + "networkVersion": "1.1.7", + "language": "en", + "connection": "wifi", + "product": "com.wultra.app.MobileToken.wtest", + "version": "2.0.0", + "platform": "Apple", + "os": "iOS", + "osVersion": "16.7.1", + "model": "iphone10,6" + } + """)), + // MainBundle/Version PowerAuth2/Version (iOS Version, deviceString) + // TODO Lubos + Arguments.of("PowerAuth2TestsHostApp-ios/1.0 PowerAuth2/1.7.8 (iOS 17.0, simulator)", readDevice(""" + {} + """)), + // PowerAuth2/Version (Android Version, Build.MANUFACTURER Build.MODEL) + // TODO Lubos + Arguments.of("PowerAuth2/1.7.8 (Android 13, Google Pixel 4)", readDevice(""" + {} + """)), + // TODO Lubos + Arguments.of("MobileToken/1.2.0 PowerAuth2/1.7.8 (iOS 15.7.9, iPhone9,3)", readDevice(""" + {} + """)) + ); } - private static UserAgent.Device[] readDevices() throws JsonProcessingException { - final ObjectMapper om = new ObjectMapper(); - return om.readValue(UserAgentTest.DEVICES, UserAgent.Device[].class); + private static UserAgent.Device readDevice(final String json) throws JsonProcessingException { + return new ObjectMapper().readValue(json, UserAgent.Device.class); } }