Skip to content

Commit

Permalink
feat: Add new generator for state codes
Browse files Browse the repository at this point in the history
  • Loading branch information
en-milie committed May 29, 2024
1 parent e63f9f4 commit 408c889
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
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);
}
}
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);
}
}

0 comments on commit 408c889

Please sign in to comment.