Skip to content

Commit

Permalink
#83: Improved Test Data Mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Pfotenhauer committed May 28, 2019
1 parent 53af251 commit db4f490
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 81 deletions.
21 changes: 9 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xceptance</groupId>
Expand Down Expand Up @@ -30,7 +31,7 @@
<organizationUrl>https://www.xceptance.com</organizationUrl>
<roles>
<role>architect</role>
<role>developer</role>
<role>developer</role>
</roles>
</developer>
</developers>
Expand Down Expand Up @@ -203,7 +204,6 @@
<artifactId>snakeyaml</artifactId>
<version>1.24</version>
</dependency>

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
Expand All @@ -219,12 +219,6 @@
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>

<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-junit4</artifactId>
Expand All @@ -240,18 +234,21 @@
<artifactId>allure-selenide</artifactId>
<version>${allure.version}</version>
</dependency>

<!-- pinned dependency since it was not not available in all life cycle steps anymore -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.1-jre</version>
</dependency>

<dependency>
<groupId>com.codeborne</groupId>
<artifactId>selenide</artifactId>
<version>5.2.3</version>
<exclusions>
<exclusion>
<groupId>net.lightbody.bmp</groupId>
<artifactId>browsermob-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,19 @@ public static List<Map<String, String>> readFile(InputStream inputStream)
Map<String, String> newDataSet = new HashMap<>();
for (Entry<String, JsonElement> entry : dataSet.entrySet())
{
newDataSet.put(entry.getKey(), entry.getValue().getAsString());
JsonElement element = entry.getValue();
if (element.isJsonNull())
{
newDataSet.put(entry.getKey(), null);
}
else if (element.isJsonArray() || element.isJsonObject())
{
newDataSet.put(entry.getKey(), element.toString());
}
else
{
newDataSet.put(entry.getKey(), element.getAsString());
}
}
data.add(newDataSet);
}
Expand Down
26 changes: 19 additions & 7 deletions src/main/java/com/xceptance/neodymium/util/DataUtils.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.xceptance.neodymium.util;

import java.util.Iterator;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.text.RandomStringGenerator;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class DataUtils
{
Expand Down Expand Up @@ -60,15 +63,24 @@ public static String randomPassword()
* @param <T>
* the inferred class
* @param clazz
* A reference to an clazz that should be instaciated and filled from test data
* A reference to an clazz that should be instantiated and filled from test data
* @return an instance of the class provided
*/
public static <T> T get(final Class<T> clazz)
{
// just use what we have and ignore if we have more, so we can build up different objects
// with just the data that fits
final ObjectMapper m = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return m.convertValue(Neodymium.getData(), clazz);
Map<String, String> data = Neodymium.getData();

JsonObject jsonObject = new JsonObject();
JsonParser parser = new JsonParser();

// iterate over every data entry and parse the entries to prepare complex structures for object mapping
for (Iterator<String> iterator = data.keySet().iterator(); iterator.hasNext();)
{
String key = (String) iterator.next();
jsonObject.add(key, parser.parse(data.get(key)));
}

return new Gson().fromJson(jsonObject, clazz);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
package com.xceptance.neodymium.testclasses.datautils;

import java.text.MessageFormat;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.xceptance.neodymium.NeodymiumRunner;
import com.xceptance.neodymium.module.statement.testdata.DataSet;
import com.xceptance.neodymium.util.DataUtils;
import com.xceptance.neodymium.util.Neodymium;

@RunWith(NeodymiumRunner.class)
public class DataUtilsTests
{
private static final String NIL = "not in list";

@Test
@DataSet(id = "asString")
public void testAsString() throws Exception
{
Map<String, String> data = Neodymium.getData();
data.clear();
data.put("nullValue", null);
data.put("empty", "");
data.put("value", "value");

// expect IllegalArgumentException
expectIAE(() -> {
DataUtils.asString(null);
Expand All @@ -46,20 +40,12 @@ public void testAsString() throws Exception
Assert.assertEquals(null, DataUtils.asString("", null));
Assert.assertEquals(null, DataUtils.asString("nullValue", null));
Assert.assertEquals(null, DataUtils.asString(NIL, null));

}

@Test
@DataSet(id = "asInt")
public void testAsInt() throws Exception
{
Map<String, String> data = Neodymium.getData();
data.clear();
data.put("nullValue", null);
data.put("empty", "");
data.put("positiveValue", "3");
data.put("negativeValue", "-3");
data.put("zeroValue", "0");

// expect IllegalArgumentException
expectIAE(() -> {
DataUtils.asInt(null);
Expand Down Expand Up @@ -89,16 +75,9 @@ public void testAsInt() throws Exception
}

@Test
@DataSet(id = "asLong")
public void testAsLong() throws Exception
{
Map<String, String> data = Neodymium.getData();
data.clear();
data.put("nullValue", null);
data.put("empty", "");
data.put("positiveValue", "3");
data.put("negativeValue", "-3");
data.put("zeroValue", "0");

// expect IllegalArgumentException
expectIAE(() -> {
DataUtils.asLong(null);
Expand Down Expand Up @@ -128,16 +107,9 @@ public void testAsLong() throws Exception
}

@Test
@DataSet(id = "asFloat")
public void testAsFloat() throws Exception
{
Map<String, String> data = Neodymium.getData();
data.clear();
data.put("nullValue", null);
data.put("empty", "");
data.put("positiveValue", "3.3f");
data.put("negativeValue", "-3.3");
data.put("zeroValue", ".0");

// expect IllegalArgumentException
expectIAE(() -> {
DataUtils.asFloat(null);
Expand Down Expand Up @@ -167,16 +139,9 @@ public void testAsFloat() throws Exception
}

@Test
@DataSet(id = "asDouble")
public void testAsDouble() throws Exception
{
Map<String, String> data = Neodymium.getData();
data.clear();
data.put("nullValue", null);
data.put("empty", "");
data.put("positiveValue", "3.3d");
data.put("negativeValue", "-3.3");
data.put("zeroValue", ".0");

// expect IllegalArgumentException
expectIAE(() -> {
DataUtils.asDouble(null);
Expand Down Expand Up @@ -206,15 +171,9 @@ public void testAsDouble() throws Exception
}

@Test
@DataSet(id = "asBoolean")
public void testAsBoolean() throws Exception
{
Map<String, String> data = Neodymium.getData();
data.clear();
data.put("nullValue", null);
data.put("empty", "");
data.put("positiveValue", "true");
data.put("negativeValue", "FaLsE");

// expect IllegalArgumentException
expectIAE(() -> {
DataUtils.asBool(null);
Expand All @@ -240,21 +199,28 @@ public void testAsBoolean() throws Exception
}

@Test
@DataSet(id = "asObject")
public void testGetClass() throws Exception
{
Map<String, String> data = Neodymium.getData();
data.clear();
data.put("cardNumber", "4111111111111111");
data.put("ccv", "123");
data.put("month", "10");
data.put("year", "2018");

TestCreditCard creditCard = DataUtils.get(TestCreditCard.class);

Assert.assertEquals("4111111111111111", creditCard.getCardNumber());
Assert.assertEquals("123", creditCard.getCcv());
Assert.assertEquals(10, creditCard.getMonth());
Assert.assertEquals(2018, creditCard.getYear());
TestCompoundClass testClass = DataUtils.get(TestCompoundClass.class);

Assert.assertEquals("1234567890", testClass.getClubCardNumber());
Assert.assertEquals("4111111111111111", testClass.getCreditCard().getCardNumber());
Assert.assertEquals("123", testClass.getCreditCard().getCcv());
Assert.assertEquals(10, testClass.getCreditCard().getMonth());
Assert.assertEquals(2018, testClass.getCreditCard().getYear());
Assert.assertEquals(23, testClass.getAge());
Assert.assertEquals(3, testClass.getNames().size());
Assert.assertEquals("abc", testClass.getNames().get(0));
Assert.assertEquals("def", testClass.getNames().get(1));
Assert.assertEquals("ghi", testClass.getNames().get(2));
Assert.assertEquals(2, testClass.getPersons().size());
Assert.assertEquals("a", testClass.getPersons().get(0).getFirstName());
Assert.assertEquals("b", testClass.getPersons().get(0).getLastName());
Assert.assertEquals("c", testClass.getPersons().get(1).getFirstName());
Assert.assertEquals("d", testClass.getPersons().get(1).getLastName());
Assert.assertEquals("value", testClass.getKeyValueMap().get("key"));
Assert.assertEquals(TestCompoundClass.Level.HIGH, testClass.getLevel());
}

private void expectIAE(Runnable function)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.xceptance.neodymium.testclasses.datautils;

import java.util.List;
import java.util.Map;

public class TestCompoundClass
{
enum Level
{
LOW,
MEDIUM,
HIGH
}

String clubCardNumber;

int age;

TestCreditCard creditCard;

List<String> names;

List<TestPerson> persons;

Map<String, String> keyValueMap;

Level level;

public List<TestPerson> getPersons()
{
return persons;
}

public void setPersons(List<TestPerson> persons)
{
this.persons = persons;
}

public List<String> getNames()
{
return names;
}

public void setNames(List<String> names)
{
this.names = names;
}

public int getAge()
{
return age;
}

public void setAge(int age)
{
this.age = age;
}

public String getClubCardNumber()
{
return clubCardNumber;
}

public void setClubCardNumber(String clubCardNumber)
{
this.clubCardNumber = clubCardNumber;
}

public TestCreditCard getCreditCard()
{
return creditCard;
}

public void setCreditCard(TestCreditCard creditCard)
{
this.creditCard = creditCard;
}

public Map<String, String> getKeyValueMap()
{
return keyValueMap;
}

public void setKeyValueMap(Map<String, String> keyValueMap)
{
this.keyValueMap = keyValueMap;
}

public Level getLevel()
{
return level;
}

public void setLevel(Level level)
{
this.level = level;
}
}
Loading

0 comments on commit db4f490

Please sign in to comment.