Skip to content

Commit bd90d48

Browse files
feat: [CDS-72112]: Unit test to validate on-prem pipeline.json compared with harness-schema json (#49289)
* feat: [CDS-0]: test taking tmie to execute * feat: [CDS-0]: testing * feat: [CDS-0]: verified the api * feat: [CDS-0]: update resource * feat: [CDS-0]: comments * feat: [CDS-0]: buildifier * feat: [CDS-0]: checkstyle
1 parent 328f987 commit bd90d48

File tree

6 files changed

+123462
-2
lines changed

6 files changed

+123462
-2
lines changed

pipeline-service/service/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ java_library(
130130
name = "tests",
131131
testonly = True,
132132
srcs = glob(["src/test/**/*.java"]),
133-
resources = ["//pipeline-service/service/src/test/resources:resource"],
133+
resources = [
134+
"//pipeline-service/service/src/main/resources:static_resource",
135+
"//pipeline-service/service/src/test/resources:resource",
136+
],
134137
visibility = ["//visibility:public"],
135138
deps = [
136139
":module",

pipeline-service/service/src/main/java/io/harness/pms/schema/PmsYamlSchemaResourceImpl.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,15 @@
2525
import io.harness.pms.pipeline.service.PMSYamlSchemaService;
2626
import io.harness.pms.yaml.SchemaErrorResponse;
2727
import io.harness.pms.yaml.YamlSchemaResponse;
28+
import io.harness.serializer.JsonUtils;
2829
import io.harness.yaml.schema.YamlSchemaResource;
2930

3031
import com.fasterxml.jackson.databind.JsonNode;
32+
import com.google.common.io.Resources;
3133
import com.google.inject.Inject;
34+
import java.io.IOException;
35+
import java.nio.charset.StandardCharsets;
36+
import java.util.Objects;
3237
import javax.validation.constraints.NotNull;
3338
import javax.ws.rs.NotSupportedException;
3439
import lombok.AllArgsConstructor;
@@ -43,6 +48,8 @@ public class PmsYamlSchemaResourceImpl implements YamlSchemaResource, PmsYamlSch
4348
private final NGTriggerYamlSchemaService ngTriggerYamlSchemaService;
4449

4550
private final String deployMode = System.getenv().get("DEPLOY_MODE");
51+
private final String PIPELINE_JSON_PATH = "static-schema/pipeline.json";
52+
private final String TEMPLATE_JSON_PATH = "static-schema/template.json";
4653

4754
public ResponseDTO<JsonNode> getYamlSchema(@NotNull EntityType entityType, String projectIdentifier,
4855
String orgIdentifier, Scope scope, String identifier, @NotNull String accountIdentifier) {
@@ -66,7 +73,8 @@ public ResponseDTO<JsonNode> getStaticYamlSchema(String accountIdentifier, Strin
6673
Currently static schema is not supported for community and onPrem env.
6774
*/
6875
if (!validateIfStaticSchemaRequired(entityType, env)) {
69-
return getYamlSchema(entityType, projectIdentifier, orgIdentifier, scope, identifier, accountIdentifier);
76+
return getStaticYamlSchemaFromResource(
77+
accountIdentifier, projectIdentifier, orgIdentifier, identifier, entityType, scope);
7078
}
7179

7280
JsonNode staticJson = pmsYamlSchemaService.getStaticSchema(
@@ -78,6 +86,34 @@ public ResponseDTO<JsonNode> getStaticYamlSchema(String accountIdentifier, Strin
7886
: getYamlSchema(entityType, projectIdentifier, orgIdentifier, scope, identifier, accountIdentifier);
7987
}
8088

89+
private ResponseDTO<JsonNode> getStaticYamlSchemaFromResource(String accountIdentifier, String projectIdentifier,
90+
String orgIdentifier, String identifier, EntityType entityType, Scope scope) {
91+
String filePath;
92+
switch (entityType) {
93+
case PIPELINES:
94+
filePath = PIPELINE_JSON_PATH;
95+
break;
96+
case TEMPLATE:
97+
filePath = TEMPLATE_JSON_PATH;
98+
break;
99+
default:
100+
return getYamlSchema(entityType, projectIdentifier, orgIdentifier, scope, identifier, accountIdentifier);
101+
}
102+
103+
try {
104+
return ResponseDTO.newResponse(fetchFile(filePath));
105+
} catch (IOException ex) {
106+
log.error("Not able to read json from {} path", filePath);
107+
}
108+
return getYamlSchema(entityType, projectIdentifier, orgIdentifier, scope, identifier, accountIdentifier);
109+
}
110+
public JsonNode fetchFile(String filePath) throws IOException {
111+
ClassLoader classLoader = this.getClass().getClassLoader();
112+
String staticJson =
113+
Resources.toString(Objects.requireNonNull(classLoader.getResource(filePath)), StandardCharsets.UTF_8);
114+
return JsonUtils.asObject(staticJson, JsonNode.class);
115+
}
116+
81117
private boolean validateIfStaticSchemaRequired(EntityType entityType, String env) {
82118
// static schema is not supported for empty env or on-prem env. In entity type currently its supported only for
83119
// Pipelines or Template

pipeline-service/service/src/main/resources/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@ filegroup(
1313
name = "resource",
1414
srcs = glob(["**/*"]),
1515
)
16+
17+
filegroup(
18+
name = "static_resource",
19+
srcs = glob(["static-schema/*"]),
20+
)

0 commit comments

Comments
 (0)