Created
September 15, 2018 15:15
-
-
Save mageddo/4ce1fd9be21b3e5d22986fae596004e8 to your computer and use it in GitHub Desktop.
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
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.lang.reflect.InvocationTargetException; | |
import java.nio.file.Files; | |
import java.text.SimpleDateFormat; | |
import java.time.temporal.ChronoUnit; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Date; | |
import java.util.HashSet; | |
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.Set; | |
import java.util.function.Function; | |
import java.util.stream.Collectors; | |
import org.apache.commons.beanutils.PropertyUtils; | |
import org.hamcrest.CoreMatchers; | |
import org.junit.Assert; | |
import org.springframework.core.io.ClassPathResource; | |
public class TestUtils { | |
public static ClassPathResource getDBResource(String fileName) { | |
return getClassPathResource(String.format("db/%s", fileName)); | |
} | |
public static ClassPathResource getClassPathResource(String fileName) { | |
return new ClassPathResource(fileName); | |
} | |
/** | |
* Retorna a primeira mensagem de erro do servidor ou null se não houverem | |
* <p> | |
* pega da session (se for redirect ou do HTML se for diferente de redirect) | |
*/ | |
public static void assertValue(String field, Object expected, Object current) { | |
if (current instanceof Date) { | |
Assert.assertThat( | |
String.format("Field: %s", field), | |
toStringTimestamp((Date) current), | |
CoreMatchers.equalTo(toStringTimestamp((Date) expected))); | |
} else { | |
Assert.assertThat( | |
String.format("Field: %s", field), | |
current, | |
CoreMatchers.equalTo(expected)); | |
} | |
} | |
/** | |
* Faz assert das duas datas aceitando uma folga entre current e expected em minutos | |
* <p> | |
* expected date deve ser maior | |
* | |
* @param field | |
* @param expected newer date | |
* @param current older date | |
* @param maxMinutes | |
*/ | |
public static void assertMaxMinutesDivergence(String field, Date expected, Date current, Integer maxMinutes) { | |
long interval = Math.abs(ChronoUnit.MINUTES.between(current.toInstant(), expected.toInstant())); | |
Assert.assertTrue( | |
String.format("Field: %s \nA diferença máxima em minutos tem que ser <= %d, current=%d, dataAtual=%s, dataEsperada=%s ", field, | |
maxMinutes, interval, current.toString(), expected.toString()), | |
interval <= maxMinutes); | |
} | |
public static String toStringTimestamp(Date d) { | |
if (d == null) | |
return ""; | |
return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS").format(d); | |
} | |
public static void compareAllAttributes(final Object expected, final Object actual) | |
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { | |
compareSpecificAttributes(expected, actual, null); | |
} | |
public static void compareAllAttributes(final Object expected, final Object actual, | |
AssertionInterface assertionInterface) throws IllegalAccessException, | |
InvocationTargetException, NoSuchMethodException { | |
compareSpecificAttributes(expected, actual, null, assertionInterface); | |
} | |
/** | |
* Compara os atributos passados, pode se passar uma interface que será chamada para validar cada campo | |
* | |
* @param expected | |
* @param actual | |
* @param fieldsToVerify | |
* @param assertionInterface | |
* @throws IllegalAccessException | |
* @throws NoSuchMethodException | |
* @throws InvocationTargetException | |
*/ | |
public static void compareSpecificAttributes(Object expected, Object actual, List<String> fieldsToVerify, | |
AssertionInterface assertionInterface) throws IllegalAccessException, NoSuchMethodException, | |
InvocationTargetException { | |
if (fieldsToVerify == null) { | |
fieldsToVerify = new ArrayList<>(PropertyUtils.describe(expected).keySet()); | |
} | |
for (final String key : fieldsToVerify) { | |
final Object expectedValue = PropertyUtils.getProperty(expected, key); | |
final Object currentValue = PropertyUtils.getProperty(actual, key); | |
assertionInterface.doAssert(key, expectedValue, currentValue); | |
} | |
} | |
public static void compareAllButExclude(final Object expected, final Object actual, final String... excludeFields) | |
throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { | |
} | |
/** | |
* Compara todos menos os passados | |
* | |
* @param expected | |
* @param actual | |
* @param excludeFields | |
* @throws IllegalAccessException | |
* @throws NoSuchMethodException | |
* @throws InvocationTargetException | |
*/ | |
public static void compareAllInListButExclude(final Object expected, final Object actual, | |
final List<String> expectedProps, final String... excludeFields) throws IllegalAccessException, | |
NoSuchMethodException, InvocationTargetException { | |
final List<String> listExcludeFields = Arrays.asList(excludeFields); | |
expectedProps.removeAll(listExcludeFields); | |
compareSpecificAttributes(expected, actual, expectedProps); | |
} | |
public static void compareSpecificAttributes(final Object expected, final Object actual, | |
List<String> fieldsToVerify) | |
throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { | |
compareSpecificAttributes(expected, actual, fieldsToVerify, (key, expectedValue, currentValue) -> { | |
assertValue(key, expectedValue, currentValue); | |
}); | |
} | |
/** | |
* Extrai um set de Ids a partir de uma lista de objetos | |
* | |
* @param actual lista de objectos cujos ids serão extraídos | |
* @param getIdMethod método que deve ser usado para obter os ids | |
* @param <T> tipo do objeto tendo o id extraído | |
* @return Set de ids | |
*/ | |
public static <T> Set<Integer> toIdSet(final List<T> actual, | |
final Function<T, Integer> getIdMethod) { | |
final List<Integer> actualIds = actual.stream() | |
.map(getIdMethod) | |
.collect(Collectors.toCollection(LinkedList::new)); | |
return new HashSet<>(actualIds); | |
} | |
/** | |
* Cria um arquivo com base nos dados do stream passado | |
* @param in | |
* @return | |
* @throws IOException | |
*/ | |
public static File createTMPFile(InputStream in) throws IOException { | |
final File file = Files.createTempFile("ATM", "").toFile(); | |
try(final FileOutputStream out = new FileOutputStream(file)){ | |
for(int b; (b = in.read()) !=-1 ; ){ | |
out.write(b); | |
} | |
}finally { | |
in.close(); | |
} | |
return file; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment