b4281540488fbd2e102b84d9865e91223457f9b2
[smo/teiv.git] /
1 /*
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21 package org.oran.smo.teiv.pgsqlgenerator.schema.data;
22
23 import java.util.List;
24 import java.util.Map;
25
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;
31
32 import org.oran.smo.teiv.pgsqlgenerator.Column;
33 import org.oran.smo.teiv.pgsqlgenerator.Table;
34 import org.oran.smo.teiv.pgsqlgenerator.TestHelper;
35
36 @SpringBootTest(classes = { ModelComparator.class, DataSchemaHelper.class })
37 class ModelComparatorTest {
38
39     @Autowired
40     private DataSchemaHelper dataSchemaHelper;
41     @Autowired
42     private ModelComparator modelComparator;
43     //spotless:off
44     private final List<Table> mockModelServiceEntities = List.of(
45         Table.builder().name("Sector").columns(
46             List.of(
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(
53             List.of(
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());
57     //spotless:on
58
59     /*
60      * Find differences between extracted data from module service and baseline
61      * Then check if the schema generated for both are the same
62      */
63
64     // Test will all present except table "Namespace". The algorithm should correctly identify difference as well as generate correct schema for the same.
65     @Test
66     void identifyDifferencesInBaselineAndGeneratedWithTableMissingTest() {
67
68         //spotless:off
69
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());
77
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());
84
85         //spotless:on
86
87         // When
88         Map<String, List<Table>> mappedDifferences = modelComparator.identifyDifferencesInBaselineAndGenerated(
89                 mockModelServiceEntities, baselineEntitiesTableMissing);
90
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());
94
95     }
96
97     // Test will all present except table "Namespace" & column "Sector.azimuth"
98     @Test
99     void identifyDifferencesInBaselineAndGeneratedWithTableAndAttributesMissingTest() {
100
101         //spotless:off
102
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());
109
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")
118                     .build())).build());
119
120         //spotless:on
121
122         // When
123         Map<String, List<Table>> mappedDifferences = modelComparator.identifyDifferencesInBaselineAndGenerated(
124                 mockModelServiceEntities, baselineEntitiesWIthColumnAndTableMissing);
125
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());
129     }
130
131     // Test will all present except table "Namespace", column "Sector.azimuth" & No default value set to "Sector.sectorId"
132     @Test
133     void identifyDifferencesInBaselineAndGeneratedWithTableAttributeAndDefaultMissingTest() {
134
135         //spotless:off
136
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());
143
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")
152                     .build())).build());
153         correctMappedDifferences.get("DEFAULT").add(Table.builder().name("Sector").columns(
154             List.of(Column.builder().name("sectorId").dataType("jsonb").defaultValue("101").build())).build());
155         //spotless:on
156
157         // When
158         Map<String, List<Table>> mappedDifferences = modelComparator.identifyDifferencesInBaselineAndGenerated(
159                 mockModelServiceEntities, baselineEntitiesWIthColumnAndTableAndDefaultValueMissing);
160
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());
164     }
165
166 }