Skip to content

Commit

Permalink
add failing test for @SchemaSwap when used in apt. fabric8io#4350
Browse files Browse the repository at this point in the history
  • Loading branch information
xRodney committed Aug 25, 2022
1 parent 2403614 commit 20be2a8
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
@@ -0,0 +1,29 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.crd.generator.schemaswaps;

import io.fabric8.crd.generator.annotation.SchemaSwap;
import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.model.annotation.Group;
import io.fabric8.kubernetes.model.annotation.Version;

@Version("v1")
@Group("acme.com")
@SchemaSwap(originalType = SchemaSwapSpec.SomeObject.class, fieldName = "shouldBeString", targetType = String.class)
@SchemaSwap(originalType = SchemaSwapSpec.AnotherObject.class, fieldName = "shouldBeInt", targetType = Integer.class)
public class MultipleSchemaSwaps extends CustomResource<SchemaSwapSpec, Void> {

}
@@ -0,0 +1,70 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.crd.generator.schemaswaps;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.junit.jupiter.api.Test;

import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition;
import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionVersion;
import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps;
import io.fabric8.kubernetes.client.utils.Serialization;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

class SchemaSwapCRDTest {

@Test
void testCrd() {
CustomResourceDefinition d = Serialization.unmarshal(getClass().getClassLoader()
.getResourceAsStream("META-INF/fabric8/multipleschemaswaps.acme.com-v1.yml"),
CustomResourceDefinition.class);
assertNotNull(d);

CustomResourceDefinitionVersion v1 = d.getSpec().getVersions().get(0);
assertNotNull(v1);
assertEquals("v1", v1.getName());
Map<String, JSONSchemaProps> spec = v1.getSchema().getOpenAPIV3Schema().getProperties().get("spec").getProperties();
assertNotNull(spec);

// 'first' is replaced by SchemaSwap from int to string
JSONSchemaProps first = spec.get("first");
Map<String, JSONSchemaProps> firstProps = first.getProperties();
assertNotNull(firstProps);
JSONSchemaProps firstProperty = firstProps.get("shouldBeString");
assertEquals("string", firstProperty.getType());

// 'second' is replaced by the same SchemaSwap that is applied multiple times
JSONSchemaProps second = spec.get("second");
Map<String, JSONSchemaProps> secondProps = second.getProperties();
assertNotNull(secondProps);
JSONSchemaProps secondProperty = secondProps.get("shouldBeString");
assertEquals("string", secondProperty.getType());

// 'third' is replaced by the another SchemaSwap
JSONSchemaProps third = spec.get("third");
Map<String, JSONSchemaProps> thirdProps = third.getProperties();
assertNotNull(thirdProps);
JSONSchemaProps thirdProperty = thirdProps.get("shouldBeInt");
assertEquals("integer", thirdProperty.getType());
}

}
@@ -0,0 +1,30 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.crd.generator.schemaswaps;

public class SchemaSwapSpec {
private SomeObject first;
private SomeObject second;
private AnotherObject third;

static class SomeObject {
private int shouldBeString;
}

static class AnotherObject {
private String shouldBeInt;
}
}

0 comments on commit 20be2a8

Please sign in to comment.