-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add new generator for state codes
- Loading branch information
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
src/main/java/com/endava/cats/generator/format/impl/StateCodeGenerator.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.endava.cats.generator.format.impl; | ||
|
||
import com.endava.cats.generator.format.api.DataFormat; | ||
import com.endava.cats.generator.format.api.OpenAPIFormat; | ||
import com.endava.cats.generator.format.api.PropertySanitizer; | ||
import com.endava.cats.generator.format.api.ValidDataFormatGenerator; | ||
import com.endava.cats.util.CatsUtil; | ||
import io.swagger.v3.oas.models.media.Schema; | ||
import jakarta.inject.Singleton; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Generates valid state codes. | ||
*/ | ||
@Singleton | ||
public class StateCodeGenerator implements ValidDataFormatGenerator, OpenAPIFormat { | ||
|
||
@Override | ||
public boolean appliesTo(String format, String propertyName) { | ||
return "statecode".equalsIgnoreCase(PropertySanitizer.sanitize(format)) || | ||
PropertySanitizer.sanitize(propertyName).endsWith("statecode"); | ||
} | ||
|
||
@Override | ||
public List<String> matchingFormats() { | ||
return List.of("state-code", "stateCode"); | ||
} | ||
|
||
@Override | ||
public Object generate(Schema<?> schema) { | ||
String generated = CatsUtil.faker().address().stateAbbr(); | ||
|
||
return DataFormat.matchesPatternOrNull(schema, generated); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/test/java/com/endava/cats/generator/format/impl/StateCodeGeneratorTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.endava.cats.generator.format.impl; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.swagger.v3.oas.models.media.Schema; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
|
||
@QuarkusTest | ||
class StateCodeGeneratorTest { | ||
|
||
@Test | ||
void shouldGenerate() { | ||
StateCodeGenerator stateCodeGenerator = new StateCodeGenerator(); | ||
Assertions.assertThat(stateCodeGenerator.generate(new Schema<>()).toString()).hasSize(2); | ||
} | ||
|
||
@ParameterizedTest | ||
@CsvSource({"state-code,true", "stateCode,true", "other,false"}) | ||
void shouldApplyToFormat(String format, boolean expected) { | ||
StateCodeGenerator stateCodeGenerator = new StateCodeGenerator(); | ||
Assertions.assertThat(stateCodeGenerator.appliesTo(format, "")).isEqualTo(expected); | ||
} | ||
|
||
@ParameterizedTest | ||
@CsvSource({"stateCode,true", "state-code,true", "state_code,true", "other,false"}) | ||
void shouldApplyToPropertyName(String property, boolean expected) { | ||
StateCodeGenerator stateCodeGenerator = new StateCodeGenerator(); | ||
Assertions.assertThat(stateCodeGenerator.appliesTo("", property)).isEqualTo(expected); | ||
} | ||
} |