diff options
Diffstat (limited to 'mod2/helm-generator/helmchartgenerator-core/src/test/java/org/onap')
7 files changed, 589 insertions, 0 deletions
diff --git a/mod2/helm-generator/helmchartgenerator-core/src/test/java/org/onap/dcaegen2/platform/helmchartgenerator/ChartBuilderTest.java b/mod2/helm-generator/helmchartgenerator-core/src/test/java/org/onap/dcaegen2/platform/helmchartgenerator/ChartBuilderTest.java new file mode 100644 index 0000000..dd19382 --- /dev/null +++ b/mod2/helm-generator/helmchartgenerator-core/src/test/java/org/onap/dcaegen2/platform/helmchartgenerator/ChartBuilderTest.java @@ -0,0 +1,54 @@ + /** # ============LICENSE_START======================================================= + * # Copyright (c) 2021 AT&T Intellectual Property. All rights reserved. + * # ================================================================================ + * # 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. + * # ============LICENSE_END========================================================= +*/ + + +package org.onap.dcaegen2.platform.helmchartgenerator; + + import org.junit.jupiter.api.Test; + import org.junit.jupiter.api.extension.ExtendWith; + import org.mockito.Mock; + import org.mockito.Mockito; + import org.mockito.junit.jupiter.MockitoExtension; + import org.onap.dcaegen2.platform.helmchartgenerator.chartbuilder.ChartBuilder; + import org.onap.dcaegen2.platform.helmchartgenerator.chartbuilder.ChartGenerator; + import org.onap.dcaegen2.platform.helmchartgenerator.chartbuilder.ComponentSpecParser; + import org.onap.dcaegen2.platform.helmchartgenerator.validation.ChartTemplateStructureValidator; + + import static org.mockito.ArgumentMatchers.any; + + @ExtendWith(MockitoExtension.class) +class ChartBuilderTest { + + @Mock + private ChartGenerator chartGenerator; + + @Mock + private ComponentSpecParser specParser; + + @Mock + private ChartTemplateStructureValidator validator; + + @Test + void testChartBuilderSteps() throws Exception{ + ChartBuilder builder = new ChartBuilder(specParser, chartGenerator, validator); + builder.build("someSpec", "someChartLocation", "someOutputLocation", "someSpecSchemaLocation"); + + Mockito.verify(specParser, Mockito.times(1)).extractChartInfo(any(), any(), any()); + Mockito.verify(chartGenerator, Mockito.times(1)).generate(any(), any(), any()); + Mockito.verify(validator, Mockito.times(1)).validateChartTemplateStructure(any()); + } +} diff --git a/mod2/helm-generator/helmchartgenerator-core/src/test/java/org/onap/dcaegen2/platform/helmchartgenerator/ChartGeneratorTest.java b/mod2/helm-generator/helmchartgenerator-core/src/test/java/org/onap/dcaegen2/platform/helmchartgenerator/ChartGeneratorTest.java new file mode 100644 index 0000000..1c2c9bc --- /dev/null +++ b/mod2/helm-generator/helmchartgenerator-core/src/test/java/org/onap/dcaegen2/platform/helmchartgenerator/ChartGeneratorTest.java @@ -0,0 +1,58 @@ +/* + * # ============LICENSE_START======================================================= + * # Copyright (c) 2021 AT&T Intellectual Property. All rights reserved. + * # ================================================================================ + * # 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. + * # ============LICENSE_END========================================================= + */ + + +package org.onap.dcaegen2.platform.helmchartgenerator; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.onap.dcaegen2.platform.helmchartgenerator.chartbuilder.ChartGenerator; +import org.onap.dcaegen2.platform.helmchartgenerator.chartbuilder.HelmClient; +import org.onap.dcaegen2.platform.helmchartgenerator.chartbuilder.KeyValueMerger; +import org.onap.dcaegen2.platform.helmchartgenerator.models.chartinfo.ChartInfo; + +import static org.mockito.ArgumentMatchers.any; + +@ExtendWith(MockitoExtension.class) +class ChartGeneratorTest { + + @Mock + private KeyValueMerger kvMerger; + + @Mock + private HelmClient helmClient; + + @Mock + private Utils utils; + + @Test + void testChartGenerationSteps() throws Exception{ + ChartGenerator chartGenerator = new ChartGenerator(helmClient, kvMerger, utils); + Mockito.when(utils.cloneFileToTempLocation(any())).thenReturn(any()); + + chartGenerator.generate("src/test/input/blueprint", new ChartInfo(), "src/test/output"); + + Mockito.verify(kvMerger, Mockito.times(1)).mergeValuesToChart(any(), any()); + Mockito.verify(helmClient, Mockito.times(1)).lint(any()); + Mockito.verify(helmClient, Mockito.times(1)).packageChart(any(), any()); + } + +} diff --git a/mod2/helm-generator/helmchartgenerator-core/src/test/java/org/onap/dcaegen2/platform/helmchartgenerator/ChartMuseumDistributorTest.java b/mod2/helm-generator/helmchartgenerator-core/src/test/java/org/onap/dcaegen2/platform/helmchartgenerator/ChartMuseumDistributorTest.java new file mode 100644 index 0000000..c0d0663 --- /dev/null +++ b/mod2/helm-generator/helmchartgenerator-core/src/test/java/org/onap/dcaegen2/platform/helmchartgenerator/ChartMuseumDistributorTest.java @@ -0,0 +1,74 @@ +/* + * # ============LICENSE_START======================================================= + * # Copyright (c) 2021 AT&T Intellectual Property. All rights reserved. + * # ================================================================================ + * # 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. + * # ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.platform.helmchartgenerator; + +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; +import org.apache.commons.io.FileUtils; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.platform.helmchartgenerator.distribution.ChartMuseumDistributor; + +import java.io.File; +import java.io.IOException; + +public class ChartMuseumDistributorTest { + + ChartMuseumDistributor distributor; + + public static MockWebServer mockChartMuseumServer; + + private static File mockChartFile; + + @BeforeAll + static void init() throws IOException { + mockChartMuseumServer = new MockWebServer(); + mockChartMuseumServer.start(); + mockChartFile = File.createTempFile("dcae-ves-collector-1.8.0","tgz"); + } + + @BeforeEach + void setUp(){ + String baseUrl = String.format("http://localhost:%s", + mockChartMuseumServer.getPort()); + distributor = new ChartMuseumDistributor(baseUrl, "mockUsername", "mockPassword"); + } + + @Test + void distribute() throws Exception{ + mockChartMuseumServer.enqueue(getMock201Response()); + distributor.distribute(mockChartFile); + } + + private MockResponse getMock201Response() { + return new MockResponse() + .setResponseCode(201) + .setBody("{\"saved\": true}"); + } + + + @AfterAll + static void tearDown() throws IOException { + mockChartMuseumServer.shutdown(); + FileUtils.deleteQuietly(mockChartFile); + } +} + diff --git a/mod2/helm-generator/helmchartgenerator-core/src/test/java/org/onap/dcaegen2/platform/helmchartgenerator/ChartTemplateStructureValidatorTest.java b/mod2/helm-generator/helmchartgenerator-core/src/test/java/org/onap/dcaegen2/platform/helmchartgenerator/ChartTemplateStructureValidatorTest.java new file mode 100644 index 0000000..3e94b64 --- /dev/null +++ b/mod2/helm-generator/helmchartgenerator-core/src/test/java/org/onap/dcaegen2/platform/helmchartgenerator/ChartTemplateStructureValidatorTest.java @@ -0,0 +1,48 @@ +/* + * # ============LICENSE_START======================================================= + * # Copyright (c) 2021 AT&T Intellectual Property. All rights reserved. + * # ================================================================================ + * # 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. + * # ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.platform.helmchartgenerator; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.onap.dcaegen2.platform.helmchartgenerator.validation.ChartTemplateStructureValidatorImpl; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class ChartTemplateStructureValidatorTest { + + private ChartTemplateStructureValidatorImpl validator; + + @BeforeEach + void setUp() { + validator = new ChartTemplateStructureValidatorImpl(); + } + + @Test + void validateTemplateStructure(){ + String validStructureLocation = "src/test/input/blueprint"; + assertDoesNotThrow(() -> validator.validateChartTemplateStructure(validStructureLocation)); + } + + @Test + void invalidateTemplateStructureShouldThrowRuntimeError() { + String invalidStructureLocation = "test"; + assertThrows(RuntimeException.class, () -> validator.validateChartTemplateStructure(invalidStructureLocation)); + } +} diff --git a/mod2/helm-generator/helmchartgenerator-core/src/test/java/org/onap/dcaegen2/platform/helmchartgenerator/ComponentSpecParserTest.java b/mod2/helm-generator/helmchartgenerator-core/src/test/java/org/onap/dcaegen2/platform/helmchartgenerator/ComponentSpecParserTest.java new file mode 100644 index 0000000..0f157d3 --- /dev/null +++ b/mod2/helm-generator/helmchartgenerator-core/src/test/java/org/onap/dcaegen2/platform/helmchartgenerator/ComponentSpecParserTest.java @@ -0,0 +1,187 @@ +/* + * # ============LICENSE_START======================================================= + * # Copyright (c) 2021 AT&T Intellectual Property. All rights reserved. + * # ================================================================================ + * # 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. + * # ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.platform.helmchartgenerator; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.onap.dcaegen2.platform.helmchartgenerator.chartbuilder.ComponentSpecParser; +import org.onap.dcaegen2.platform.helmchartgenerator.models.chartinfo.ChartInfo; +import org.onap.dcaegen2.platform.helmchartgenerator.validation.ComponentSpecValidator; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +@ExtendWith({MockitoExtension.class}) +class ComponentSpecParserTest { + + ComponentSpecParser parser; + + @Mock + ComponentSpecValidator validator; + + @BeforeEach + void setUp() { + parser = new ComponentSpecParser(validator, new Utils()); + } + + @Test + void extractChartInfo() throws Exception{ + String specLocation = "src/test/input/specs/ves.json"; + String chartTemplateLocation = "src/test/input/blueprint"; + String specSchemaLocation = ""; + ChartInfo chartInfo = parser.extractChartInfo(specLocation, chartTemplateLocation, specSchemaLocation); + + assertMetadata(chartInfo); + assertOuterKeyValues(chartInfo); + assertApplicationConfigSection(chartInfo); + assertReadinessCheck(chartInfo); + assertApplicationEnv(chartInfo); + assertService(chartInfo); + assertPolicyInfo(chartInfo); + assertCertificates(chartInfo); + assertConfigMap(chartInfo); + assertPostgres(chartInfo); + assertSecrets(chartInfo); + } + + private void assertApplicationConfigSection(ChartInfo chartInfo) { + Map<String, Object> applicationConfig = (Map<String, Object>) chartInfo.getValues().get("applicationConfig"); + assertThat(applicationConfig.size()).isEqualTo(20); + } + + private void assertOuterKeyValues(ChartInfo chartInfo) { + Map<String, Object> outerKv = chartInfo.getValues(); + assertThat(outerKv.get("image")).isEqualTo("nexus3.onap.org:10001/onap/org.onap.dcaegen2.collectors.ves.vescollector:latest"); + assertThat(outerKv.get("logDirectory")).isEqualTo("/opt/app/VESCollector/logs/"); + assertThat(outerKv.get("certDirectory")).isEqualTo("/opt/app/dcae-certificate/"); + assertThat(outerKv.get("tlsServer")).isEqualTo(true); + } + + private void assertMetadata(ChartInfo chartInfo) { + assertThat(chartInfo.getMetadata().getName()).isEqualTo("dcae-ves-collector"); + assertThat(chartInfo.getMetadata().getVersion()).isEqualTo("1.8.0"); + assertThat(chartInfo.getMetadata().getDescription()). + isEqualTo("Collector for receiving VES events through restful interface"); + } + + private void assertReadinessCheck(ChartInfo chartInfo) { + Map<String, Object> readiness = (Map<String, Object>) chartInfo.getValues().get("readiness"); + assertThat(readiness.get("scheme")).isEqualTo("http"); + assertThat(readiness.get("path")).isEqualTo("/healthcheck"); + assertThat(readiness.get("periodSeconds")).isEqualTo(15); + assertThat(readiness.get("port")).isEqualTo(8080); + assertThat(readiness.get("initialDelaySeconds")).isEqualTo(5); + assertThat(readiness.get("timeoutSeconds")).isEqualTo(1); + } + + private void assertApplicationEnv(ChartInfo chartInfo) { + ObjectMapper oMapper = new ObjectMapper(); + Map<String, Object> applicationEnv = (Map<String, Object>) chartInfo.getValues().get("applicationEnv"); + Map<String, Object> PMSH_PG_USERNAME = oMapper.convertValue(applicationEnv.get("PMSH_PG_USERNAME"), Map.class); + Map<String, Object> PMSH_PG_PASSWORD = oMapper.convertValue(applicationEnv.get("PMSH_PG_PASSWORD"), Map.class); + + assertThat(applicationEnv.get("PMSH_PG_URL")).isEqualTo("dcae-pmsh-pg-primary"); + assertThat(PMSH_PG_USERNAME.get("secretUid")).isEqualTo("pgUserCredsSecretUid"); + assertThat(PMSH_PG_USERNAME.get("key")).isEqualTo("login"); + assertThat(PMSH_PG_PASSWORD.get("secretUid")).isEqualTo("pgUserCredsSecretUid"); + assertThat(PMSH_PG_PASSWORD.get("key")).isEqualTo("password"); + } + + private void assertService(ChartInfo chartInfo) { + Map<String, Object> service = (Map<String, Object>) chartInfo.getValues().get("service"); + List<Map> ports = new ArrayList<Map>(); + for(Object portsGroup : (ArrayList) service.get("ports")){ + ports.add((Map<String, Object>) portsGroup); + } + assertThat(service.get("type")).isEqualTo("NodePort"); + assertThat(service.get("name")).isEqualTo("dcae-ves-collector"); + assertThat(service.get("has_internal_only_ports")).isEqualTo(true); + assertThat(ports.get(0).get("name")).isEqualTo("http"); + assertThat(ports.get(0).get("port")).isEqualTo(8443); + assertThat(ports.get(0).get("plain_port")).isEqualTo(8080); + assertThat(ports.get(0).get("port_protocol")).isEqualTo("http"); + assertThat(ports.get(0).get("nodePort")).isEqualTo(17); + assertThat(ports.get(0).get("useNodePortExt")).isEqualTo(true); + assertThat(ports.get(1).get("name")).isEqualTo("metrics"); + assertThat(ports.get(1).get("port")).isEqualTo(4444); + assertThat(ports.get(1).get("internal_only")).isEqualTo(true); + } + + private void assertPolicyInfo(ChartInfo chartInfo) { + Map<String, Object> policyInfo = (Map<String, Object>) chartInfo.getValues().get("policies"); + assertThat(policyInfo.get("policyID")).isEqualTo("'[\"tca_policy_id_10\", \"tca_policy_id_11\"]'\n"); + } + + private void assertCertificates(ChartInfo chartInfo) { + List certificates = (List) chartInfo.getValues().get("certificates"); + Map<String, Object> certificate = (Map<String, Object>) certificates.get(0); + assertThat(certificate.get("mountPath")).isEqualTo("/opt/app/dcae-certificate/external"); + assertThat(certificate.get("commonName")).isEqualTo("dcae-ves-collector"); + assertThat(((List) certificate.get("dnsNames")).get(0)).isEqualTo("dcae-ves-collector"); + assertThat(((List) ((Map<String, Map>) certificate.get("keystore")).get("outputType")).get(0)).isEqualTo("jks"); + assertThat((((Map<String, Map>) certificate.get("keystore")).get("passwordSecretRef")).get("name")).isEqualTo("ves-collector-cmpv2-keystore-password"); + assertThat((((Map<String, Map>) certificate.get("keystore")).get("passwordSecretRef")).get("key")).isEqualTo("password"); + assertThat((((Map<String, Map>) certificate.get("keystore")).get("passwordSecretRef")).get("create")).isEqualTo(true); + } + + private void assertConfigMap(ChartInfo chartInfo) { + List externalVolumes = (List) chartInfo.getValues().get("externalVolumes"); + Map<String, Object> volume_one = (Map<String, Object>) externalVolumes.get(0); + Map<String, Object> volume_two = (Map<String, Object>) externalVolumes.get(1); + assertThat(volume_one.get("name")).isEqualTo("dcae-external-repo-configmap-schema-map"); + assertThat(volume_one.get("type")).isEqualTo("configMap"); + assertThat(volume_one.get("mountPath")).isEqualTo("/opt/app/VESCollector/etc/externalRepo/"); + assertThat(volume_one.get("optional")).isEqualTo(true); + } + + private void assertPostgres(ChartInfo chartInfo) { + Map<String, Object> postgres = (Map<String, Object>) chartInfo.getValues().get("postgres"); + assertThat(postgres.get("nameOverride")).isEqualTo("dcae-ves-collector-postgres"); + assertThat(((Map<String, Object>) postgres.get("service")).get("name")).isEqualTo("dcae-ves-collector-postgres"); + assertThat(((Map<String, Object>) postgres.get("service")).get("name2")).isEqualTo("dcae-ves-collector-pg-primary"); + assertThat(((Map<String, Object>) postgres.get("service")).get("name3")).isEqualTo("dcae-ves-collector-pg-replica"); + assertThat(((Map<String, Object>) ((Map<String, Object>) postgres.get("container")).get("name")).get("primary")).isEqualTo("dcae-ves-collector-pg-primary"); + assertThat(((Map<String, Object>) ((Map<String, Object>) postgres.get("container")).get("name")).get("replica")).isEqualTo("dcae-ves-collector-pg-replica"); + assertThat(((Map<String, Object>) postgres.get("persistence")).get("mountSubPath")).isEqualTo("dcae-ves-collector/data"); + assertThat(((Map<String, Object>) postgres.get("persistence")).get("mountInitPath")).isEqualTo("dcae-ves-collector"); + assertThat(((Map<String, Object>) postgres.get("config")).get("pgUserName")).isEqualTo("ves-collector"); + assertThat(((Map<String, Object>) postgres.get("config")).get("pgDatabase")).isEqualTo("ves-collector"); + assertThat(((Map<String, Object>) postgres.get("config")).get("pgUserExternalSecret")).isEqualTo("{{ include \"common.release\" . }}-ves-collector-pg-user-creds"); + } + + private void assertSecrets(ChartInfo chartInfo) { + List<Object> secrets = (List<Object>) chartInfo.getValues().get("secrets"); + Map<String, Object> secret1 = (Map<String, Object>) secrets.get(0); + assertThat(secret1.get("uid")).isEqualTo("pg-user-creds"); + assertThat(secret1.get("name")).isEqualTo("{{ include \"common.release\" . }}-ves-collector-pg-user-creds"); + assertThat(secret1.get("type")).isEqualTo("basicAuth"); + assertThat(secret1.get("externalSecret")).isEqualTo("{{ ternary \"\" (tpl (default \"\" .Values.postgres.config.pgUserExternalSecret) .) (hasSuffix \"ves-collector-pg-user-creds\" .Values.postgres.config.pgUserExternalSecret) }}"); + assertThat(secret1.get("login")).isEqualTo("{{ .Values.postgres.config.pgUserName }}"); + assertThat(secret1.get("password")).isEqualTo("{{ .Values.postgres.config.pgUserPassword }}"); + assertThat(secret1.get("passwordPolicy")).isEqualTo("generate"); + } +} + diff --git a/mod2/helm-generator/helmchartgenerator-core/src/test/java/org/onap/dcaegen2/platform/helmchartgenerator/ComponentSpecValidatorTest.java b/mod2/helm-generator/helmchartgenerator-core/src/test/java/org/onap/dcaegen2/platform/helmchartgenerator/ComponentSpecValidatorTest.java new file mode 100644 index 0000000..a9124b4 --- /dev/null +++ b/mod2/helm-generator/helmchartgenerator-core/src/test/java/org/onap/dcaegen2/platform/helmchartgenerator/ComponentSpecValidatorTest.java @@ -0,0 +1,68 @@ +/* + * # ============LICENSE_START======================================================= + * # Copyright (c) 2021 AT&T Intellectual Property. All rights reserved. + * # ================================================================================ + * # 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. + * # ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.platform.helmchartgenerator; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; +import org.onap.dcaegen2.platform.helmchartgenerator.validation.ComponentSpecValidator; +import org.onap.dcaegen2.platform.helmchartgenerator.validation.ComponentSpecValidatorImpl; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; + +@ExtendWith(MockitoExtension.class) +public class ComponentSpecValidatorTest { + + private static final String SPEC_SCHEMA = "src/test/input/specs/schemas/component-spec-schema.json"; + + ComponentSpecValidator validator; + + @BeforeEach + void setUp() { + validator = new ComponentSpecValidatorImpl(new Utils()); + } + + @Test + void validSpecShouldNotThrowAnyException() { + String specSchema = "src/test/input/specs/schemas/component-spec-schema.json"; + String specFileLocation = "src/test/input/specs/ves.json"; + assertDoesNotThrow(() -> validator.validateSpecFile(specFileLocation, specSchema)); + } + + @Test + void invalidSpecShouldThrowRuntimeException() { + String specFileLocation = "src/test/input/specs/invalidSpecSchema.json"; + assertThrows(RuntimeException.class, () -> validator.validateSpecFile(specFileLocation, SPEC_SCHEMA)); + } + + @Test + void ifNoHelmSectionFoundThrowRuntimeError() { + String specFileLocation = "src/test/input/specs/invalidSpecNoHelm.json"; + assertThrows(RuntimeException.class, () -> validator.validateSpecFile(specFileLocation, SPEC_SCHEMA)); + } + + @Test + void ifNoServiceSectionFoundThrowRuntimeError() { + String specFileLocation = "src/test/input/specs/invalidSpecNoServices.json"; + assertThrows(RuntimeException.class, () -> validator.validateSpecFile(specFileLocation, SPEC_SCHEMA)); + } + +} diff --git a/mod2/helm-generator/helmchartgenerator-core/src/test/java/org/onap/dcaegen2/platform/helmchartgenerator/KeyValueMergerTest.java b/mod2/helm-generator/helmchartgenerator-core/src/test/java/org/onap/dcaegen2/platform/helmchartgenerator/KeyValueMergerTest.java new file mode 100644 index 0000000..4082170 --- /dev/null +++ b/mod2/helm-generator/helmchartgenerator-core/src/test/java/org/onap/dcaegen2/platform/helmchartgenerator/KeyValueMergerTest.java @@ -0,0 +1,100 @@ +/* + * # ============LICENSE_START======================================================= + * # Copyright (c) 2021 AT&T Intellectual Property. All rights reserved. + * # ================================================================================ + * # 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. + * # ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.platform.helmchartgenerator; + +import org.apache.commons.io.FileUtils; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.onap.dcaegen2.platform.helmchartgenerator.chartbuilder.KeyValueMerger; +import org.onap.dcaegen2.platform.helmchartgenerator.models.chartinfo.ChartInfo; +import org.onap.dcaegen2.platform.helmchartgenerator.models.chartinfo.Metadata; +import org.yaml.snakeyaml.Yaml; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.PrintWriter; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.HashMap; +import java.util.Map; + +import static org.mockito.ArgumentMatchers.any; + +@ExtendWith(MockitoExtension.class) +class KeyValueMergerTest { + + private KeyValueMerger merger; + + @Mock + private Yaml yamlHelper; + + private File chartDir; + + @BeforeEach + void setUp() { + merger = new KeyValueMerger(yamlHelper); + } + + @Test + void mergeValuesToChart() throws IOException { + ChartInfo chartInfo = prepareChartInfo(); + chartDir = prepareChartDir(); + + Mockito.when(yamlHelper.load(any(InputStream.class))).thenReturn(new HashMap<String, Object>()); + + merger.mergeValuesToChart(chartInfo, chartDir); + Mockito.verify(yamlHelper, Mockito.times(2)).dump(any(HashMap.class), any(PrintWriter.class)); + } + + @AfterEach + void tearDown(){ + FileUtils.deleteQuietly(chartDir); + } + + private File prepareChartDir() throws IOException { + final Path chartDir = Files.createTempDirectory("chartDir"); + Files.createFile(chartDir.resolve("Chart.yaml")); + Files.createFile(chartDir.resolve("values.yaml")); + return chartDir.toFile(); + } + + private ChartInfo prepareChartInfo() { + ChartInfo chartInfo = new ChartInfo(); + + Metadata metadata = new Metadata(); + metadata.setName("someComponent"); + metadata.setVersion("someVersion"); + metadata.setDescription("someDescription"); + + Map<String, Object> values = new HashMap<>(); + values.put("someKey", "someValue"); + + chartInfo.setMetadata(metadata); + chartInfo.setValues(values); + + return chartInfo; + } +} + |