Skip to content

Commit 4d03387

Browse files
committed
Ignore schemas that are expected to fail in tests
1 parent f2ce0ae commit 4d03387

File tree

2 files changed

+144
-85
lines changed

2 files changed

+144
-85
lines changed

operator-gen-maven-plugin/src/main/java/org/acme/read/crud/ResponseTypeMapper.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.Map.Entry;
77
import java.util.Objects;
88
import java.util.Optional;
9+
import java.util.function.Function;
910
import java.util.function.Predicate;
1011
import java.util.function.Supplier;
1112

@@ -19,6 +20,7 @@ public class ResponseTypeMapper implements CrudMapper {
1920

2021
private static final String DO_NOT_MATCH = "%%%%";
2122
private static final String MEDIATYPE_JSON = "application/json";
23+
private static final String MEDIATYPE_TEXT_HTML = "text/html";
2224
private final Schema schema;
2325
private final OpenAPI api;
2426
private String modelName;
@@ -39,6 +41,10 @@ public String getResponseMediaType() {
3941
return MEDIATYPE_JSON;
4042
}
4143

44+
public String[] getResponseMediaTypes() {
45+
return new String[] {MEDIATYPE_JSON, MEDIATYPE_TEXT_HTML};
46+
}
47+
4248
@Override
4349
public Schema getByIdSchema() {
4450
return schema;
@@ -155,7 +161,8 @@ public Optional<Entry<String, PathItem>> createPath() {
155161
.findAny();
156162
})
157163
.or(() ->
158-
api.getPaths().getPathItems().entrySet().stream().filter(this::matchPostResponse).findAny()
164+
165+
api.getPaths().getPathItems().entrySet().stream().filter(e -> matchPostResponse(e, i -> i.getValue().getPOST())).findAny()
159166

160167
);
161168
}
@@ -195,12 +202,17 @@ private boolean matchPatchResponse(Entry<String, PathItem> e) {
195202
.getContent().getMediaType(getResponseMediaType()).getSchema().getRef());
196203
}
197204

198-
private boolean matchPostResponse(Entry<String, PathItem> e) {
199-
return e.getValue().getPOST() != null && e.getValue().getPOST().getResponses().getAPIResponse("201") != null
200-
&& e.getValue().getPOST().getResponses().getAPIResponse("201").getContent()
205+
private boolean matchPostResponse(Entry<String, PathItem> e, Function<Entry<String, PathItem>, Operation> f) {
206+
return matchPostResponse(e, f, "201") || matchPostResponse(e, f, "200");
207+
}
208+
209+
private boolean matchPostResponse(Entry<String, PathItem> e, Function<Entry<String, PathItem>, Operation> f, String response) {
210+
Operation op = f.apply(e);
211+
return schema.getRef() != null
212+
&& op != null && op.getResponses().getAPIResponse(response) != null
213+
&& op.getResponses().getAPIResponse(response).getContent()
201214
.getMediaType(getResponseMediaType()) != null
202-
&& schema.getRef() != null
203-
&& Objects.equals(schema.getRef(), e.getValue().getPOST().getResponses().getAPIResponse("201")
215+
&& Objects.equals(schema.getRef(), op.getResponses().getAPIResponse(response)
204216
.getContent().getMediaType(getResponseMediaType()).getSchema().getRef());
205217
}
206218

operator-gen-maven-plugin/src/test/java/org/acme/read/crud/ResponseTypeMapperTest.java

Lines changed: 126 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,9 @@
1818
import org.eclipse.microprofile.openapi.models.PathItem;
1919
import org.eclipse.microprofile.openapi.models.media.Schema;
2020
import org.junit.jupiter.api.BeforeAll;
21-
import org.junit.jupiter.api.Test;
2221
import org.junit.jupiter.params.ParameterizedTest;
2322
import org.junit.jupiter.params.provider.MethodSource;
2423

25-
import com.fasterxml.jackson.databind.JsonNode;
26-
import com.fasterxml.jackson.databind.ObjectMapper;
27-
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
28-
29-
import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition;
30-
import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionBuilder;
31-
import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionVersionBuilder;
32-
import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsBuilder;
33-
import io.fabric8.kubernetes.client.utils.Serialization;
3424
import io.quarkus.smallrye.openapi.runtime.OpenApiConstants;
3525
import io.smallrye.openapi.api.OpenApiConfig;
3626
import io.smallrye.openapi.api.OpenApiConfigImpl;
@@ -64,55 +54,6 @@ public static void setUp() {
6454
throw new RuntimeException("Could not find [" + OpenApiConstants.BASE_NAME + Format.JSON + "]");
6555
}
6656
}
67-
68-
@Test
69-
public void test() {
70-
System.out.println(model.getComponents().getResponses().get("Organization"));
71-
ObjectMapper objectMapper = new ObjectMapper();
72-
JsonNode jsonNodeTree;
73-
try {
74-
jsonNodeTree = objectMapper.readTree(ResponseTypeMapperTest.class.getClassLoader().getResourceAsStream("gitea.json"));
75-
String jsonAsYaml = new YAMLMapper().writeValueAsString(jsonNodeTree.get("components").get("schemas").get("Organization"));
76-
//System.out.println(new KubernetesSerialization().unmarshal(schemas).getClass());
77-
System.err.println(jsonAsYaml);
78-
JsonNode schema = jsonNodeTree.get("components").get("schemas").get("Organization");
79-
80-
81-
82-
JSONSchemaPropsBuilder specBuilder = new JSONSchemaPropsBuilder().withType("object");
83-
schema.get("properties").fields().forEachRemaining(
84-
f -> specBuilder.addToProperties(f.getKey(), new JSONSchemaPropsBuilder().withType(f.getValue().get("type").asText()).build())
85-
);
86-
87-
CustomResourceDefinitionBuilder defBuilder = new CustomResourceDefinitionBuilder();
88-
CustomResourceDefinition customResourceDefinition = defBuilder.editOrNewMetadata()
89-
.withName("Organization")
90-
.endMetadata()
91-
.editOrNewSpec()
92-
.withGroup("opgen.io")
93-
.withNewNames().withKind("Organization").endNames()
94-
.withScope("Namespaced")
95-
.withVersions(new CustomResourceDefinitionVersionBuilder()
96-
.withName("v1alpha1")
97-
.withServed(true)
98-
.withStorage(true)
99-
.withNewSchema()
100-
.editOrNewOpenAPIV3Schema()
101-
.addToProperties("status", new JSONSchemaPropsBuilder().withType("object").build())
102-
.addToProperties("spec", specBuilder.build())
103-
.endOpenAPIV3Schema()
104-
.and()
105-
.build())
106-
.endSpec()
107-
.build();
108-
109-
String myPodAsYaml = Serialization.asYaml(customResourceDefinition);
110-
System.out.println(myPodAsYaml);
111-
} catch (IOException e1) {
112-
// TODO Auto-generated catch block
113-
e1.printStackTrace();
114-
}
115-
}
11657

11758
@ParameterizedTest
11859
@MethodSource("responseTypesWithGetById")
@@ -130,7 +71,7 @@ void getByIdPath(String modelName) {
13071
}
13172

13273
@ParameterizedTest
133-
@MethodSource("modelsToTest")
74+
@MethodSource("responseTypesWithDelete")
13475
void deletePath(String modelName) {
13576
ResponseTypeMapper analyzer = new ResponseTypeMapper(model, modelName);
13677
if (!analyzer.isArrayType()) {
@@ -156,7 +97,7 @@ void patchPath(String modelName) {
15697
}
15798

15899
@ParameterizedTest
159-
@MethodSource("modelsToTest")
100+
@MethodSource("responseTypesWithCreate")
160101
void createPath(String modelName) {
161102
ResponseTypeMapper analyzer = new ResponseTypeMapper(model, modelName);
162103
if (!analyzer.isArrayType()) {
@@ -186,27 +127,133 @@ private static Stream<String> responseTypesWithGetById() {
186127
}
187128

188129
private static Stream<String> responseTypesWithPatch() {
189-
Set<String> noFindById = new HashSet<String>(EXCLUDED_RESPONSES);
190-
noFindById.add("AccessToken" /* AccessToken cannot be patched */);
191-
noFindById.add("ActivityPub" /* The post method has additional /index which is not expected */);
192-
noFindById.add("AnnotatedTag" /* AnnotatedTag cannot be patched */);
193-
noFindById.add("Branch" /* Branch cannot be patched */);
194-
noFindById.add("CombinedStatus" /* CombinedStatus is read-only */);
195-
noFindById.add("Commit" /* Commit is read-only */);
196-
noFindById.add("CommitStatus" /* CommitStatus cannot be patched */);
197-
noFindById.add("ContentsResponse" /* ContentsResponse cannot be patched */);
198-
noFindById.add("DeployKey" /* DeployKey cannot be patched */);
199-
noFindById.add("GPGKey" /* GPGKey cannot be patched */);
200-
201-
//TODO Why is patch path for EmailList is /user/settings
202-
//TODO Why is patch path for GPGKeyList is /user/settings
203-
204-
return reader.getResponseTypeNames(e -> !noFindById.contains(e.getKey()));
130+
Set<String> noPatch = new HashSet<String>(EXCLUDED_RESPONSES);
131+
noPatch.add("AccessToken" /* AccessToken cannot be patched */);
132+
noPatch.add("ActivityPub" /* ActivityPub cannot be patched */);
133+
noPatch.add("AnnotatedTag" /* AnnotatedTag is read-onlyd */);
134+
noPatch.add("Branch" /* Branch cannot be patched */);
135+
noPatch.add("CombinedStatus" /* CombinedStatus is read-only */);
136+
noPatch.add("Commit" /* Commit is read-only */);
137+
noPatch.add("CommitStatus" /* CommitStatus cannot be patched */);
138+
noPatch.add("ContentsResponse" /* ContentsResponse cannot be patched */);
139+
noPatch.add("DeployKey" /* DeployKey cannot be patched */);
140+
noPatch.add("GPGKey" /* GPGKey cannot be patched */);
141+
noPatch.add("GeneralAPISettings" /* GeneralAPISettings is read-only */);
142+
noPatch.add("GeneralAttachmentSettings" /* GeneralAttachmentSettings is read-only */);
143+
noPatch.add("GeneralRepoSettings" /* GeneralRepoSettings is read-only */);
144+
noPatch.add("GeneralUISettings" /* GeneralUISettings is read-only */);
145+
noPatch.add("GitBlobResponse" /* GitBlobResponse is read-only*/);
146+
noPatch.add("GitTreeResponse" /* GitTreeResponse is read-only */);
147+
noPatch.add("GitignoreTemplateInfo" /* GitignoreTemplateInfo is read-only */);
148+
noPatch.add("IssueDeadline" /* Issue deadline can only be created */);
149+
noPatch.add("LanguageStatistics" /* LanguageStatistics is read-only */);
150+
noPatch.add("LicenseTemplateInfo" /* LicenseTemplateInfo can only be get by id */);
151+
noPatch.add("MarkdownRender" /* MarkdownRender can only be created */);
152+
noPatch.add("MarkupRender" /* MarkupRender can only be created */);
153+
noPatch.add("NodeInfo" /* NodeInfo is read-only */);
154+
noPatch.add("Note" /* Note is read-only */);
155+
noPatch.add("NotificationCount" /* Note is read-only */);
156+
noPatch.add("OrganizationPermissions" /* OrganizationPermissions is read-only */);
157+
noPatch.add("Package" /* Package cannot be patched */);
158+
noPatch.add("PublicKey" /* PublicKey cannot be patched */);
159+
noPatch.add("PullReview" /* PullReview cannot be patched */);
160+
noPatch.add("PullReviewComment" /* PullReviewComment is read-only */);
161+
noPatch.add("PushMirror" /* PushMirror cannot be patched */);
162+
noPatch.add("Reaction" /* Reaction cannot be patched */);
163+
noPatch.add("Reference" /* Reaction cannot be patched */);
164+
noPatch.add("RepoCollaboratorPermission" /* RepoCollaboratorPermission is read-only */);
165+
noPatch.add("RepoIssueConfig" /* RepoIssueConfig is read-only */);
166+
noPatch.add("RepoIssueConfigValidation" /* RepoIssueConfigValidation is read-only */);
167+
noPatch.add("RepoNewIssuePinsAllowed" /* RepoNewIssuePinsAllowed is read-only */);
168+
noPatch.add("SearchResults" /* SearchResults is read-only */);
169+
noPatch.add("Secret" /* Secret can only be created */);
170+
noPatch.add("ServerVersion" /* ServerVersion is read-only */);
171+
noPatch.add("StopWatch" /* StopWatch is read-only */);
172+
noPatch.add("Tag" /* Tag cannot be patched */);
173+
noPatch.add("TopicNames" /* TODO TopicNames cannot be patched but updated!! */);
174+
noPatch.add("TrackedTime" /* TrackedTime is read-only */);
175+
noPatch.add("WatchInfo" /* WatchInfo is read-only */);
176+
noPatch.add("WikiCommitList" /* WikiCommitList is read-only */);
177+
return reader.getResponseTypeNames(e -> !noPatch.contains(e.getKey()));
205178
}
206179

207-
private static Stream<String> modelsToTest() {
208-
return reader.getResponseTypeNames(e -> !EXCLUDED_RESPONSES.contains(e.getKey()));
180+
private static Stream<String> responseTypesWithCreate() {
181+
Set<String> noCreate = new HashSet<String>(EXCLUDED_RESPONSES);
182+
noCreate.add("ActivityPub" /* The post method has additional /inbox which is not expected */);
183+
noCreate.add("AnnotatedTag" /* AnnotatedTag is read-only */);
184+
noCreate.add("CombinedStatus" /* CombinedStatus is read-only */);
185+
noCreate.add("Commit" /* Commit is read-only */);
186+
noCreate.add("GeneralAPISettings" /* GeneralAPISettings is read-only */);
187+
noCreate.add("GeneralAttachmentSettings" /* GeneralAttachmentSettings is read-only */);
188+
noCreate.add("GeneralRepoSettings" /* GeneralRepoSettings is read-only */);
189+
noCreate.add("GeneralUISettings" /* GeneralUISettings is read-only */);
190+
noCreate.add("GitBlobResponse" /* GitBlobResponse is read-only*/);
191+
noCreate.add("GitHook" /*TODO The get method is in /git and the post one level above */);
192+
noCreate.add("GitTreeResponse" /* GitTreeResponse is read-only */);
193+
noCreate.add("GitignoreTemplateInfo" /* GitignoreTemplateInfo is read-only */);
194+
noCreate.add("LanguageStatistics" /* LanguageStatistics is read-only */);
195+
noCreate.add("LicenseTemplateInfo" /* LicenseTemplateInfo can only be get by id */);
196+
noCreate.add("MarkdownRender" /* TODO MarkdownRender schema is text/html, we don't support this currently*/);
197+
noCreate.add("MarkupRender" /* MarkupRender schema is text/html, we don't support this currently */);
198+
noCreate.add("NodeInfo" /* NodeInfo is read-only */);
199+
noCreate.add("Note" /* Note is read-only */);
200+
noCreate.add("NotificationCount" /* Note is read-only */);
201+
noCreate.add("NotificationThread" /* NotificationThread cannot be created */);
202+
noCreate.add("OrganizationPermissions" /* OrganizationPermissions is read-only */);
203+
noCreate.add("Package" /* Package cannot be created */);
204+
noCreate.add("PullReviewComment" /* PullReviewComment is read-only */);
205+
noCreate.add("Reference" /* Reference cannot be created is only part of ReferenceList */);
206+
noCreate.add("RepoCollaboratorPermission" /* RepoCollaboratorPermission is read-only */);
207+
noCreate.add("RepoIssueConfig" /* RepoIssueConfig is read-only */);
208+
noCreate.add("RepoIssueConfigValidation" /* RepoIssueConfigValidation is read-only */);
209+
noCreate.add("RepoNewIssuePinsAllowed" /* RepoNewIssuePinsAllowed is read-only */);
210+
noCreate.add("SearchResults" /* SearchResults is read-only */);
211+
noCreate.add("Secret" /* Secret can only be created */);
212+
noCreate.add("ServerVersion" /* ServerVersion is read-only */);
213+
noCreate.add("StopWatch" /* StopWatch is read-only */);
214+
noCreate.add("TopicNames" /* TopicNames cannot be created but updated */);
215+
noCreate.add("WatchInfo" /* WatchInfo is read-only */);
216+
noCreate.add("WikiCommitList" /* WikiCommitList is read-only */);
217+
return reader.getResponseTypeNames(e -> !noCreate.contains(e.getKey()));
209218
}
210219

220+
private static Stream<String> responseTypesWithDelete() {
221+
Set<String> noDelete = new HashSet<String>(EXCLUDED_RESPONSES);
222+
noDelete.add("ActivityPub" /* ActivityPub cannot be deleted */);
223+
noDelete.add("AnnotatedTag" /* AnnotatedTag is read-only */);
224+
noDelete.add("CombinedStatus" /* CombinedStatus is read-only */);
225+
noDelete.add("Commit" /* Commit is read-only */);
226+
noDelete.add("CommitStatus" /* CommitStatus cannot be deleted */);
227+
noDelete.add("GeneralAPISettings" /* GeneralAPISettings is read-only */);
228+
noDelete.add("GeneralAttachmentSettings" /* GeneralAttachmentSettings is read-only */);
229+
noDelete.add("GeneralRepoSettings" /* GeneralRepoSettings is read-only */);
230+
noDelete.add("GeneralUISettings" /* GeneralUISettings is read-only */);
231+
noDelete.add("GitBlobResponse" /* GitBlobResponse is read-only*/);
232+
noDelete.add("GitTreeResponse" /* GitTreeResponse is read-only */);
233+
noDelete.add("GitignoreTemplateInfo" /* GitignoreTemplateInfo is read-only */);
234+
noDelete.add("IssueDeadline" /* Issue deadline can only be created */);
235+
noDelete.add("LanguageStatistics" /* LanguageStatistics is read-only */);
236+
noDelete.add("LicenseTemplateInfo" /* LicenseTemplateInfo can only be get by id */);
237+
noDelete.add("MarkdownRender" /* TODO MarkdownRender schema is text/html, we don't support this currently*/);
238+
noDelete.add("MarkupRender" /* MarkupRender schema is text/html, we don't support this currently */);
239+
noDelete.add("NodeInfo" /* NodeInfo is read-only */);
240+
noDelete.add("Note" /* Note is read-only */);
241+
noDelete.add("NotificationCount" /* Note is read-only */);
242+
noDelete.add("NotificationThread" /* NotificationThread cannot be created */);
243+
noDelete.add("OrganizationPermissions" /* OrganizationPermissions is read-only */);
244+
noDelete.add("PullReviewComment" /* PullReviewComment is read-only */);
245+
noDelete.add("Reference" /* Reference cannot be deleted */);
246+
noDelete.add("RepoCollaboratorPermission" /* RepoCollaboratorPermission is read-only */);
247+
noDelete.add("RepoIssueConfig" /* RepoIssueConfig is read-only */);
248+
noDelete.add("RepoIssueConfigValidation" /* RepoIssueConfigValidation is read-only */);
249+
noDelete.add("RepoNewIssuePinsAllowed" /* RepoNewIssuePinsAllowed is read-only */);
250+
noDelete.add("SearchResults" /* SearchResults is read-only */);
251+
noDelete.add("Secret" /* Secret can only be created */);
252+
noDelete.add("ServerVersion" /* ServerVersion is read-only */);
253+
noDelete.add("StopWatch" /* StopWatch is read-only */);
254+
noDelete.add("TopicNames" /* TODOTopicNames have no findById but only list. Hence it cannot be found. */);
255+
noDelete.add("WikiCommitList" /* WikiCommitList is read-only */);
256+
return reader.getResponseTypeNames(e -> !noDelete.contains(e.getKey()));
257+
}
211258

212259
}

0 commit comments

Comments
 (0)