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;
23 import java.util.List;
26 import org.oran.smo.teiv.pgsqlgenerator.PrimaryKeyConstraint;
27 import org.junit.jupiter.api.Assertions;
28 import org.junit.jupiter.api.Test;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.boot.test.context.SpringBootTest;
32 import org.oran.smo.teiv.pgsqlgenerator.Column;
33 import org.oran.smo.teiv.pgsqlgenerator.Table;
34 import org.oran.smo.teiv.pgsqlgenerator.TestHelper;
36 @SpringBootTest(classes = { ModelComparator.class, DataSchemaHelper.class })
37 class ModelComparatorTest {
40 private DataSchemaHelper dataSchemaHelper;
42 private ModelComparator modelComparator;
44 private final List<Table> mockModelServiceEntities = List.of(
45 Table.builder().name("Sector").columns(
47 Column.builder().name("azimuth").dataType("DECIMAL").build(),
48 Column.builder().name("id").dataType("VARCHAR(511)").postgresConstraints(List.of(
49 PrimaryKeyConstraint.builder().tableName("Sector").constraintName("PK_Source_id").columnToAddConstraintTo("id").build())).build(),
50 Column.builder().name("sectorId").dataType("jsonb").defaultValue("101").build(),
51 Column.builder().name("geo-location").dataType("geography").build())).build(),
52 Table.builder().name("Namespace").columns(
54 Column.builder().name("id").dataType("VARCHAR(511)").postgresConstraints(List.of(
55 PrimaryKeyConstraint.builder().tableName("Namespace").constraintName("PK_Namespace_id").columnToAddConstraintTo("id").build())).build(),
56 Column.builder().name("name").dataType("TEXT").build())).build());
60 * Find differences between extracted data from module service and baseline
61 * Then check if the schema generated for both are the same
64 // Test will all present except table "Namespace". The algorithm should correctly identify difference as well as generate correct schema for the same.
66 void identifyDifferencesInBaselineAndGeneratedWithTableMissingTest() {
70 // Given baseline mock data
71 List<Table> baselineEntitiesTableMissing = List.of(Table.builder().name("Sector").columns(
72 List.of(Column.builder().name("azimuth").dataType("DECIMAL").build(),
73 Column.builder().name("id").dataType("VARCHAR(511)").postgresConstraints(List.of(
74 PrimaryKeyConstraint.builder().tableName("Sector").constraintName("PK_Source_id").columnToAddConstraintTo("id").build())).build(),
75 Column.builder().name("sectorId").dataType("jsonb").defaultValue("101").build(),
76 Column.builder().name("geo-location").dataType("geography").build())).build());
78 // Correct result of difference below
79 Map<String, List<Table>> correctMappedDifferences = TestHelper.identifiedModelChangeMapping();
80 correctMappedDifferences.get("CREATE").add(Table.builder().name("Namespace").columns(
81 List.of(Column.builder().name("id").dataType("VARCHAR(511)").postgresConstraints(List.of(
82 PrimaryKeyConstraint.builder().tableName("Namespace").constraintName("PK_Namespace_id").columnToAddConstraintTo("id").build())).build(),
83 Column.builder().name("name").dataType("TEXT").build())).build());
88 Map<String, List<Table>> mappedDifferences = modelComparator.identifyDifferencesInBaselineAndGenerated(
89 mockModelServiceEntities, baselineEntitiesTableMissing);
91 // Then - check if the schema for diff identified above match with the correct result
92 Assertions.assertEquals(dataSchemaHelper.generateSchemaFromDifferences(correctMappedDifferences).toString(),
93 dataSchemaHelper.generateSchemaFromDifferences(mappedDifferences).toString());
97 // Test will all present except table "Namespace" & column "Sector.azimuth"
99 void identifyDifferencesInBaselineAndGeneratedWithTableAndAttributesMissingTest() {
103 // Given baseline mock data
104 List<Table> baselineEntitiesWIthColumnAndTableMissing = List.of(Table.builder().name("Sector").columns(
105 List.of(Column.builder().name("id").dataType("VARCHAR(511)").postgresConstraints(List.of(
106 PrimaryKeyConstraint.builder().tableName("Sector").constraintName("PK_Source_id").columnToAddConstraintTo("id").build())).build(),
107 Column.builder().name("sectorId").dataType("jsonb").defaultValue("101").build(),
108 Column.builder().name("geo-location").dataType("geography").build())).build());
110 // Correct result of difference below
111 Map<String, List<Table>> correctMappedDifferences = TestHelper.identifiedModelChangeMapping();
112 correctMappedDifferences.get("CREATE").add(Table.builder().name("Namespace").columns(
113 List.of(Column.builder().name("id").dataType("VARCHAR(511)").postgresConstraints(List.of(
114 PrimaryKeyConstraint.builder().tableName("Namespace").constraintName("PK_Namespace_id").columnToAddConstraintTo("id").build())).build(),
115 Column.builder().name("name").dataType("TEXT").build())).build());
116 correctMappedDifferences.get("ALTER")
117 .add(Table.builder().name("Sector").columns(List.of(Column.builder().name("azimuth").dataType("DECIMAL")
123 Map<String, List<Table>> mappedDifferences = modelComparator.identifyDifferencesInBaselineAndGenerated(
124 mockModelServiceEntities, baselineEntitiesWIthColumnAndTableMissing);
126 // Then - check if the schema for diff identified above match with the correct result
127 Assertions.assertEquals(dataSchemaHelper.generateSchemaFromDifferences(correctMappedDifferences).toString(),
128 dataSchemaHelper.generateSchemaFromDifferences(mappedDifferences).toString());
131 // Test will all present except table "Namespace", column "Sector.azimuth" & No default value set to "Sector.sectorId"
133 void identifyDifferencesInBaselineAndGeneratedWithTableAttributeAndDefaultMissingTest() {
137 // Given baseline mock data
138 List<Table> baselineEntitiesWIthColumnAndTableAndDefaultValueMissing = List.of(Table.builder().name("Sector").columns(
139 List.of(Column.builder().name("id").dataType("VARCHAR(511)").postgresConstraints(List.of(
140 PrimaryKeyConstraint.builder().tableName("Sector").constraintName("PK_Source_id").columnToAddConstraintTo("id").build())).build(),
141 Column.builder().name("sectorId").dataType("jsonb").build(),
142 Column.builder().name("geo-location").dataType("geography").build())).build());
144 // Correct result of difference below
145 Map<String, List<Table>> correctMappedDifferences = TestHelper.identifiedModelChangeMapping();
146 correctMappedDifferences.get("CREATE").add(Table.builder().name("Namespace").columns(
147 List.of(Column.builder().name("id").dataType("VARCHAR(511)").postgresConstraints(List.of(
148 PrimaryKeyConstraint.builder().tableName("Namespace").constraintName("PK_Namespace_id").columnToAddConstraintTo("id").build())).build(),
149 Column.builder().name("name").dataType("TEXT").build())).build());
150 correctMappedDifferences.get("ALTER")
151 .add(Table.builder().name("Sector").columns(List.of(Column.builder().name("azimuth").dataType("DECIMAL")
153 correctMappedDifferences.get("DEFAULT").add(Table.builder().name("Sector").columns(
154 List.of(Column.builder().name("sectorId").dataType("jsonb").defaultValue("101").build())).build());
158 Map<String, List<Table>> mappedDifferences = modelComparator.identifyDifferencesInBaselineAndGenerated(
159 mockModelServiceEntities, baselineEntitiesWIthColumnAndTableAndDefaultValueMissing);
161 // Then - check if the schema for diff identified above match with the correct result
162 Assertions.assertEquals(dataSchemaHelper.generateSchemaFromDifferences(correctMappedDifferences).toString(),
163 dataSchemaHelper.generateSchemaFromDifferences(mappedDifferences).toString());