Skip to content

Commit

Permalink
Improve test
Browse files Browse the repository at this point in the history
  • Loading branch information
jemacineiras committed Sep 26, 2024
1 parent e44a9b5 commit 6e3bc79
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 38 deletions.
2 changes: 1 addition & 1 deletion multiapi-engine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.sngular</groupId>
<artifactId>multiapi-engine</artifactId>
<version>6.0.0</version>
<version>6.0.0</version>
<packaging>jar</packaging>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,28 +60,28 @@ import com.fasterxml.jackson.annotation.JsonValue;

public enum ${schema.className} {
<#list schema.fieldObjectList as field>
<#list field.enumValues as key, value>
<#if key?matches("[0-9].*")>
${"_"+key}(${value})<#sep>,
<#else>
${key}(${value})<#sep>,
</#if>
</#list>;
<#list field.enumValues as key, value>
<#if key?matches("[0-9].*")>
${"_"+key}(${value})<#sep>,
<#else>
${key}(${value})<#sep>,
</#if>
</#list>;

private ${field.dataType.innerType?cap_first} value;

${schema.className?cap_first}(${field.dataType.innerType?cap_first} value) {
this.value = value;
${schema.className?cap_first}(${field.dataType.innerType?cap_first} value) {
this.value = value;
}

@JsonValue
public ${field.dataType.innerType?cap_first} getValue() {
return value;
return value;
}

@Override
public String toString() {
return String.valueOf(value);
return String.valueOf(value);
}
</#list>
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.assertj.core.api.Assertions;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -492,7 +492,7 @@ private static Boolean commonTest(
Assertions.assertThat(targetModelException).isNotEmptyDirectory();
TestUtils.validateFiles(expectedExceptionFiles, targetModelException);
}
} catch (final IOException e) {
} catch (final URISyntaxException e) {
result = Boolean.FALSE;
}
return result;
Expand All @@ -509,7 +509,7 @@ private static boolean modelTest(final Path resultPath, final List<String> expec
Assertions.assertThat(targetModelFolder).isNotEmptyDirectory();
TestUtils.validateFiles(expectedModelFiles, targetModelFolder);
}
} catch (final IOException e) {
} catch (final URISyntaxException e) {
result = Boolean.FALSE;
}
return result;
Expand All @@ -526,7 +526,7 @@ private static boolean customValidatorTest(final Path resultPath, final List<Str
Assertions.assertThat(targetCustomValidatorFolder).isNotEmptyDirectory();
TestUtils.validateFiles(expectedValidatorFiles, targetCustomValidatorFolder);
}
} catch (final IOException e) {
} catch (final URISyntaxException e) {
result = Boolean.FALSE;
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@

package com.sngular.api.generator.plugin.openapi;

import static org.assertj.core.api.Assertions.assertThat;
import com.sngular.api.generator.plugin.common.model.TypeConstants.TimeType;
import com.sngular.api.generator.plugin.openapi.parameter.SpecFile;
import com.sngular.api.generator.test.utils.TestUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;

import com.sngular.api.generator.plugin.common.model.TypeConstants.TimeType;
import com.sngular.api.generator.plugin.openapi.parameter.SpecFile;
import com.sngular.api.generator.test.utils.TestUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import static org.assertj.core.api.Assertions.assertThat;

@Slf4j
public final class OpenApiGeneratorFixtures {
Expand Down Expand Up @@ -1550,7 +1550,7 @@ private static Boolean commonTest(
assertThat(targetModelException).isNotEmptyDirectory();
TestUtils.validateFiles(expectedExceptionFiles, targetModelException);
}
} catch (IOException | NullPointerException e) {
} catch (URISyntaxException | NullPointerException e) {
result = Boolean.FALSE;
log.error(e.getLocalizedMessage());
}
Expand All @@ -1568,7 +1568,7 @@ private static Boolean customValidatorTest(final Path resultPath, final List<Str
assertThat(targetCustomValidatorFolder).isNotEmptyDirectory();
TestUtils.validateFiles(expectedValidatorFiles, targetCustomValidatorFolder);
}
} catch (IOException | NullPointerException e) {
} catch (URISyntaxException | NullPointerException e) {
result = Boolean.FALSE;
log.error(e.getLocalizedMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@

package com.sngular.api.generator.test.utils;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

import static org.assertj.core.api.Assertions.assertThat;

public class TestUtils {

public static void validateFiles(final List<String> expectedFiles, final File targetDirectory) throws IOException {
InputStream reader1;
InputStream reader2;
public static void validateFiles(final List<String> expectedFiles, final File targetDirectory) throws URISyntaxException {
File reader1;
File reader2;

List<File> outputFiles = new ArrayList<>(List.of(Objects.requireNonNull(targetDirectory.listFiles())));
outputFiles.removeIf(File::isDirectory);
Expand All @@ -30,17 +29,17 @@ public static void validateFiles(final List<String> expectedFiles, final File ta
.overridingErrorMessage("Wrong Number of files %d vs %d: %s", outputFiles.size(), expectedFiles.size(), outputFiles)
.hasSize(expectedFiles.size());
for (int i = 0; i < outputFiles.size(); i++) {
reader1 = new FileInputStream(outputFiles.get(i));
reader1 = outputFiles.get(i);
assertThat(reader1).overridingErrorMessage("Generated file %s should not be null", outputFiles.get(i)).isNotNull();
final String sourceName = expectedFiles.get(i);
reader2 = TestUtils.resourceAsFile(sourceName);
assertThat(reader2).overridingErrorMessage("Expected file %s should not be null", sourceName).isNotNull();
int finalI = i;
assertThat(reader1).as(() -> "Unexpected content for file " + outputFiles.get(finalI)).hasSameContentAs(reader2);
assertThat(reader1)
.hasSameTextualContentAs(reader2);
}
}

public static InputStream resourceAsFile(String resourceName) {
return TestUtils.class.getClassLoader().getResourceAsStream(resourceName);
public static File resourceAsFile(String resourceName) throws URISyntaxException {
return Paths.get(TestUtils.class.getClassLoader().getResource(resourceName).toURI()).toFile();
}
}

0 comments on commit 6e3bc79

Please sign in to comment.