use of io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps in project kubernetes-client by fabric8io.
the class CRGeneratorRunner method generate.
public List<WritableCRCompilationUnit> generate(CustomResourceDefinition crd, String basePackageName) {
CustomResourceDefinitionSpec crSpec = crd.getSpec();
String crName = crSpec.getNames().getKind();
String group = crSpec.getGroup();
List<WritableCRCompilationUnit> writableCUs = new ArrayList<>(crSpec.getVersions().size());
for (CustomResourceDefinitionVersion crdv : crSpec.getVersions()) {
String version = crdv.getName();
String pkg = Optional.ofNullable(basePackageName).map(p -> p + "." + version).orElse(version);
AbstractJSONSchema2Pojo specGenerator = null;
String prefix = crName;
if (config.getPrefixStrategy() == Config.Prefix.NEVER) {
prefix = "";
}
JSONSchemaProps spec = crdv.getSchema().getOpenAPIV3Schema().getProperties().get("spec");
if (spec != null) {
String suffix = (config.getSuffixStrategy() != Config.Suffix.NEVER) ? "Spec" : "";
specGenerator = AbstractJSONSchema2Pojo.fromJsonSchema("spec", spec, pkg, prefix, suffix, config);
}
AbstractJSONSchema2Pojo statusGenerator = null;
JSONSchemaProps status = crdv.getSchema().getOpenAPIV3Schema().getProperties().get("status");
if (status != null) {
String suffix = (config.getSuffixStrategy() != Config.Suffix.NEVER) ? "Status" : "";
statusGenerator = AbstractJSONSchema2Pojo.fromJsonSchema("status", status, pkg, prefix, suffix, config);
}
AbstractJSONSchema2Pojo crGenerator = new JCRObject(pkg, crName, group, version, prefix + "Spec", prefix + "Status", specGenerator != null, statusGenerator != null, crdv.getStorage(), crdv.getServed(), config);
List<GeneratorResult.ClassResult> classResults = validateAndAggregate(crGenerator, specGenerator, statusGenerator);
writableCUs.add(new WritableCRCompilationUnit(classResults));
}
return writableCUs;
}
use of io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps in project kubernetes-client by fabric8io.
the class CustomResourceDefinitionTest method readSchema.
JSONSchemaProps readSchema() throws IOException {
ObjectMapper mapper = new ObjectMapper();
final URL resource = getClass().getResource("/test-crd-validation-schema.json");
final JSONSchemaProps jsonSchemaProps = mapper.readValue(resource, JSONSchemaProps.class);
return jsonSchemaProps;
}
use of io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps in project kubernetes-client by fabric8io.
the class GeneratorTest method testObjectOfObjectsWithAlwaysPrefix.
@Test
void testObjectOfObjectsWithAlwaysPrefix() {
// Arrange
Config config = new Config(null, Config.Prefix.ALWAYS, null, null, null, null);
Map<String, JSONSchemaProps> props = new HashMap<>();
JSONSchemaProps newObj = new JSONSchemaProps();
newObj.setType("object");
props.put("o1", newObj);
JObject obj = new JObject(null, "t", props, null, false, "My", "", config, null);
// Act
GeneratorResult res = obj.generateJava();
// Assert
assertEquals(2, res.getTopLevelClasses().size());
assertEquals("MyO1", res.getTopLevelClasses().get(0).getName());
assertEquals("MyT", res.getTopLevelClasses().get(1).getName());
}
use of io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps in project kubernetes-client by fabric8io.
the class GeneratorTest method testObjectOfPrimitives.
@Test
void testObjectOfPrimitives() {
// Arrange
Map<String, JSONSchemaProps> props = new HashMap<>();
JSONSchemaProps newBool = new JSONSchemaProps();
newBool.setType("boolean");
props.put("o1", newBool);
JObject obj = new JObject("v1alpha1", "t", props, null, false, "", "", defaultConfig, null);
// Act
GeneratorResult res = obj.generateJava();
// Assert
assertEquals("v1alpha1.T", obj.getType());
assertEquals(1, res.getTopLevelClasses().size());
assertEquals("T", res.getTopLevelClasses().get(0).getName());
Optional<ClassOrInterfaceDeclaration> clz = res.getTopLevelClasses().get(0).getCompilationUnit().getClassByName("T");
assertTrue(clz.isPresent());
assertEquals(1, clz.get().getFields().size());
assertTrue(clz.get().getFieldByName("o1").isPresent());
}
use of io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps in project kubernetes-client by fabric8io.
the class GeneratorTest method testDefaultEnum.
@Test
void testDefaultEnum() {
// Arrange
Map<String, JSONSchemaProps> props = new HashMap<>();
JSONSchemaProps newEnum = new JSONSchemaProps();
newEnum.setType("string");
List<JsonNode> enumValues = new ArrayList<>();
enumValues.add(new TextNode("foo"));
enumValues.add(new TextNode("bar"));
enumValues.add(new TextNode("baz"));
props.put("e1", newEnum);
JEnum enu = new JEnum("t", enumValues, defaultConfig, null);
// Act
GeneratorResult res = enu.generateJava();
// Assert
assertEquals("T", enu.getType());
assertEquals(1, res.getInnerClasses().size());
assertEquals("T", res.getInnerClasses().get(0).getName());
Optional<EnumDeclaration> en = res.getInnerClasses().get(0).getCompilationUnit().getEnumByName("T");
assertTrue(en.isPresent());
assertEquals(3, en.get().getEntries().size());
assertEquals("FOO", en.get().getEntries().get(0).getName().asString());
assertEquals("BAR", en.get().getEntries().get(1).getName().asString());
assertEquals("BAZ", en.get().getEntries().get(2).getName().asString());
}
Aggregations