Skip to content

Commit

Permalink
Refactor to parametrized test
Browse files Browse the repository at this point in the history
  • Loading branch information
banterCZ committed Dec 11, 2023
1 parent ddf71ed commit e611db2
Showing 1 changed file with 83 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<UserAgent.Device> deviceOptional = UserAgent.parse(USER_AGENTS[i]);
assertTrue(deviceOptional.isPresent());
@ParameterizedTest
@MethodSource("provideUserAgents")
void testParse(final String userAgent, final UserAgent.Device expectedDevice) {
final Optional<UserAgent.Device> deviceOptional = UserAgent.parse(userAgent);
assertTrue(deviceOptional.isPresent());
assertEquals(expectedDevice, deviceOptional.get());
}

final UserAgent.Device expectedDevice = expectedDevices[i];
assertEquals(expectedDevice, deviceOptional.get());
}
private static Stream<Arguments> 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);
}

}

0 comments on commit e611db2

Please sign in to comment.