From 9c9630b442abd3d060a5f496ec8e2b765b8e97e8 Mon Sep 17 00:00:00 2001 From: Nanda Kishore Date: Mon, 3 Jun 2024 15:03:09 +0530 Subject: [PATCH] fix(web): Add tests --- .../V2PipelineTemplateController.java | 1 - .../V2PipelineTemplateControllerSpec.groovy | 72 ++++++++++++++++++- 2 files changed, 70 insertions(+), 3 deletions(-) diff --git a/front50-web/src/main/java/com/netflix/spinnaker/front50/controllers/V2PipelineTemplateController.java b/front50-web/src/main/java/com/netflix/spinnaker/front50/controllers/V2PipelineTemplateController.java index 44dacb3c4..26429a273 100644 --- a/front50-web/src/main/java/com/netflix/spinnaker/front50/controllers/V2PipelineTemplateController.java +++ b/front50-web/src/main/java/com/netflix/spinnaker/front50/controllers/V2PipelineTemplateController.java @@ -198,7 +198,6 @@ List getDependentConfigs(String templateId) { objectMapper.convertValue(templatedPipeline, V2TemplateConfiguration.class); source = config.getTemplate().getReference(); } catch (Exception e) { - e.printStackTrace(); return; } diff --git a/front50-web/src/test/groovy/com/netflix/spinnaker/front50/controllers/V2PipelineTemplateControllerSpec.groovy b/front50-web/src/test/groovy/com/netflix/spinnaker/front50/controllers/V2PipelineTemplateControllerSpec.groovy index 52c8116d7..15b434b30 100644 --- a/front50-web/src/test/groovy/com/netflix/spinnaker/front50/controllers/V2PipelineTemplateControllerSpec.groovy +++ b/front50-web/src/test/groovy/com/netflix/spinnaker/front50/controllers/V2PipelineTemplateControllerSpec.groovy @@ -17,8 +17,13 @@ package com.netflix.spinnaker.front50.controllers +import com.fasterxml.jackson.databind.DeserializationFeature import com.fasterxml.jackson.databind.ObjectMapper import com.netflix.spinnaker.front50.exceptions.InvalidEntityException +import static com.netflix.spinnaker.front50.api.model.pipeline.Pipeline.TYPE_TEMPLATED; +import static com.netflix.spinnaker.front50.model.pipeline.TemplateConfiguration.TemplateSource.SPINNAKER_PREFIX; +import com.netflix.spinnaker.front50.api.model.pipeline.Pipeline; +import com.netflix.spinnaker.front50.model.pipeline.V2TemplateConfiguration; import com.netflix.spinnaker.front50.model.pipeline.PipelineDAO import com.netflix.spinnaker.front50.model.pipeline.PipelineTemplate import com.netflix.spinnaker.front50.model.pipeline.PipelineTemplateDAO @@ -34,7 +39,9 @@ class V2PipelineTemplateControllerSpec extends Specification { def controller = new V2PipelineTemplateController( pipelineDAO: pipelineDAO, pipelineTemplateDAO: pipelineTemplateDAO, - objectMapper: new ObjectMapper(), + // V2TemplateConfiguration doesn't expect `id` attribute as part of pipeline config. + // Hence has to ignore unknown properties when converting the value. + objectMapper: new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) ) def template1 = [ @@ -156,6 +163,68 @@ class V2PipelineTemplateControllerSpec extends Specification { hash1 == hash2 } + def "getDependentConfigs returns empty list when there are no templated pipelines"() { + given: + pipelineDAO.all() >> [] + + when: + List dependentConfigIds = controller.getDependentConfigs("myPipelineTemplateId") + + then: + dependentConfigIds.isEmpty() + } + + def "getDependentConfigs returns empty list when templated pipelines have no dependencies"() { + given: + Pipeline normalPipeline = new Pipeline() + Pipeline templatedPipeline = new Pipeline(id: "id-of-my-templated-pipeline", type: TYPE_TEMPLATED) + pipelineDAO.all() >> [normalPipeline, templatedPipeline] + + when: + List dependentConfigIds = controller.getDependentConfigs("a-different-pipeline-template-id") + + then: + dependentConfigIds.isEmpty() + } + + def "getDependentConfigs returns list of dependent pipeline IDs"() { + given: + + def normalPipeline = new Pipeline( + id: "normalPipeline", + name: "normalPipeline", + application: "application", + ) + def templatedPipeline = new Pipeline( + id: "id-of-my-templated-pipeline", + name: "name-of-my-templated-pipeline", + application: "application", + type: TYPE_TEMPLATED, + schema: "v2", + template: [ + reference: SPINNAKER_PREFIX + "myPipelineTemplateId" + ] + ) + def oneOtherTemplatedPipeline = new Pipeline( + id: "id-of-one-other-templated-pipeline", + name: "name-of-one-other-templated-pipeline", + application: "application", + type: TYPE_TEMPLATED, + schema: "v2", + template: [ + reference: SPINNAKER_PREFIX + "oneOtherPipelineTemplateId" + ] + ) + + pipelineDAO.all() >> [normalPipeline, templatedPipeline, oneOtherTemplatedPipeline] + + when: + List dependentConfigIds = controller.getDependentConfigs("myPipelineTemplateId") + + then: + dependentConfigIds == ["id-of-my-templated-pipeline"] + } + @Unroll def 'should fail with InvalidEntityException if Id(#id) is not provided or empty'() { when: @@ -168,4 +237,3 @@ class V2PipelineTemplateControllerSpec extends Specification { id << [null, "", " "] } } -