2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2024 Ericsson
4 * Modifications Copyright (C) 2024 OpenInfra Foundation Europe
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
21 package org.oran.smo.teiv.pgsqlgenerator.schema.data;
24 import java.io.IOException;
25 import java.nio.file.Files;
26 import java.nio.file.Paths;
27 import java.util.List;
29 import org.oran.smo.teiv.pgsqlgenerator.TestHelper;
30 import org.oran.smo.teiv.pgsqlgenerator.Table;
31 import org.oran.smo.teiv.pgsqlgenerator.Column;
32 import org.oran.smo.teiv.pgsqlgenerator.PrimaryKeyConstraint;
33 import org.oran.smo.teiv.pgsqlgenerator.Relationship;
34 import org.oran.smo.teiv.pgsqlgenerator.schema.BackwardCompatibilityChecker;
35 import org.junit.jupiter.api.Assertions;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.beans.factory.annotation.Value;
40 import org.springframework.boot.test.context.SpringBootTest;
41 import org.springframework.core.io.ClassPathResource;
43 import org.oran.smo.teiv.pgsqlgenerator.schema.model.HashInfoDataGenerator;
45 @SpringBootTest(classes = { DataSchemaGenerator.class, ModelComparator.class, DataSchemaHelper.class, TableBuilder.class,
46 HashInfoDataGenerator.class, BackwardCompatibilityChecker.class }, properties = { "green-field-installation=true" })
47 public class GreenFieldInstallationTest {
49 static File outputSqlFile;
51 private DataSchemaGenerator dataSchemaGenerator;
53 private BackwardCompatibilityChecker nbcChecker;
54 @Value("${green-field-installation}")
55 private boolean isGreenFieldInstallation;
56 @Value("${schema.data.skeleton}")
57 private String skeletonDataSqlFile;
58 @Value("${schema.data.output}")
59 private String actualResultSqlFile;
60 @Value("${schema.model.temp-baseline}")
61 private String tempBaselineModelSqlFile;
64 void cleanup() throws IOException {
65 Files.deleteIfExists(Paths.get(tempBaselineModelSqlFile));
69 void prepareSchemaTest() throws IOException {
71 dataSchemaGenerator.prepareSchema();
72 File skeletonFile = new ClassPathResource(skeletonDataSqlFile).getFile();
73 outputSqlFile = new File(actualResultSqlFile);
76 Assertions.assertFalse(new File(tempBaselineModelSqlFile).exists());
77 Assertions.assertTrue(outputSqlFile.exists());
78 Assertions.assertTrue(isGreenFieldInstallation);
79 Assertions.assertTrue(TestHelper.filesCompareByLine(skeletonFile.toPath(), outputSqlFile.toPath()));
80 Assertions.assertTrue(outputSqlFile.delete());
84 void verifyNoExceptionsThrownForNBCWhenGreenfieldEnabled() {
86 List<Table> mockModelServiceEntities = List.of(Table.builder().name("Sector").columns(List.of(Column.builder().name(
87 "azimuth").dataType("DECIMAL").build(), Column.builder().name("id").dataType("VARCHAR(511)")
88 .postgresConstraints(List.of(PrimaryKeyConstraint.builder().tableName("Sector").constraintName(
89 "PK_Source_id").columnToAddConstraintTo("id").build())).build(), Column.builder().name(
90 "sectorId").dataType("jsonb").defaultValue("101").build(), Column.builder().name(
91 "geo-location").dataType("geography").build())).build());
94 List<Table> mockBaselineEntitiesTableDeleted = List.of();
96 // Renaming Sector table won't throw exception when green field is enabled
97 Assertions.assertDoesNotThrow(() -> nbcChecker.checkForNBCChangesInData(mockBaselineEntitiesTableDeleted,
98 mockModelServiceEntities));
101 List<Table> mockBaselineEntitiesColumnRenamed = List.of(Table.builder().name("Sector").columns(List.of(Column
102 .builder().name("azimuth").dataType("DECIMAL").build(), Column.builder().name("id").dataType("VARCHAR(511)")
103 .postgresConstraints(List.of(PrimaryKeyConstraint.builder().tableName("Sector").constraintName(
104 "PK_Source_id").columnToAddConstraintTo("id").build())).build(), Column.builder().name(
105 "sectorId123").dataType("jsonb").defaultValue("101").build(), Column.builder().name(
106 "geo-location").dataType("geography").build())).build());
108 // Renaming Sector.sectorId won't throw exception when green field is enabled
109 Assertions.assertDoesNotThrow(() -> nbcChecker.checkForNBCChangesInData(mockBaselineEntitiesColumnRenamed,
110 mockModelServiceEntities));
113 List<Table> mockBaselineEntitiesColumnDeleted = List.of(Table.builder().name("Sector").columns(List.of(Column
114 .builder().name("azimuth").dataType("DECIMAL").build(), Column.builder().name("id").dataType("VARCHAR(511)")
115 .postgresConstraints(List.of(PrimaryKeyConstraint.builder().tableName("Sector").constraintName(
116 "PK_Source_id").columnToAddConstraintTo("id").build())).build(), Column.builder().name(
117 "geo-location").dataType("geography").build())).build());
119 Assertions.assertDoesNotThrow(() -> nbcChecker.checkForNBCChangesInData(mockBaselineEntitiesColumnDeleted,
120 mockModelServiceEntities));
124 List<Relationship> baselineRel = List.of(Relationship.builder().name("ENODEBFUNCTION_PROVIDES_LTESECTORCARRIER")
125 .aSideAssociationName("provided-lteSectorCarrier").aSideMOType("ENodeBFunction").aSideMinCardinality(0)
126 .aSideMaxCardinality(1).bSideAssociationName("provided-by-enodebFunction").bSideMOType("LTESectorCarrier")
127 .bSideMinCardinality(0).bSideMaxCardinality(1).associationKind("BI_DIRECTIONAL").relationshipDataLocation(
128 "B_SIDE").connectSameEntity(false).moduleReferenceName("o-ran-smo-teiv-ran").build());
130 //Deleted relationship ENODEBFUNCTION_PROVIDES_LTESECTORCARRIER
132 List<Relationship> mockModelSvcRel1 = List.of(Relationship.builder().name(
133 "LTESECTORCARRIER_PROVIDES_ENODEBFUNCTION").aSideAssociationName("provided-eNodeBFunction").aSideMOType(
134 "LTESectorCarrier").aSideMinCardinality(0).aSideMaxCardinality(100).bSideAssociationName(
135 "provided-by-lteSectorCarrier").bSideMOType("ENodeBFunction").bSideMinCardinality(0)
136 .bSideMaxCardinality(100).associationKind("BI_DIRECTIONAL").relationshipDataLocation("RELATION")
137 .connectSameEntity(false).moduleReferenceName("o-ran-smo-teiv-ran").build());
138 Assertions.assertDoesNotThrow(() -> nbcChecker.checkForNBCChangesInModel(baselineRel, mockModelSvcRel1));
140 //from ONE_TO_ONE to MANY_TO_MANY
141 List<Relationship> mockModelSvcRel2 = List.of(Relationship.builder().name(
142 "ENODEBFUNCTION_PROVIDES_LTESECTORCARRIER").aSideAssociationName("provided-lteSectorCarrier").aSideMOType(
143 "ENodeBFunction").aSideMinCardinality(1).aSideMaxCardinality(100).bSideAssociationName(
144 "provided-by-enodebFunction").bSideMOType("LTESectorCarrier").bSideMinCardinality(0)
145 .bSideMaxCardinality(100).associationKind("BI_DIRECTIONAL").relationshipDataLocation("A_SIDE")
146 .connectSameEntity(false).moduleReferenceName("o-ran-smo-teiv-ran").build());
147 Assertions.assertDoesNotThrow(() -> nbcChecker.checkForNBCChangesInModel(baselineRel, mockModelSvcRel2));