bd65066e0739b5b36cd5a20a29a92485bfe27a08
[smo/teiv.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2024 Ericsson
4  *  Modifications Copyright (C) 2024-2025 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.exposure.spi.impl;
22
23 import static org.oran.smo.teiv.utils.TeivConstants.MODULE_REFERENCE;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.jooq.impl.DSL.field;
26 import static org.jooq.impl.DSL.table;
27 import static org.oran.smo.teiv.utils.TeivConstants.TEIV_CONSUMER_DATA;
28 import static org.oran.smo.teiv.utils.TeivConstants.TEIV_CONSUMER_DATA_SCHEMA;
29 import static org.oran.smo.teiv.utils.TeivConstants.TEIV_DATA_SCHEMA;
30 import static org.oran.smo.teiv.utils.TeivConstants.TEIV_MODEL;
31
32 import java.util.Collections;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Optional;
36 import java.util.Set;
37 import javax.sql.DataSource;
38
39 import com.fasterxml.jackson.databind.ObjectMapper;
40 import org.jetbrains.annotations.NotNull;
41 import org.jooq.DSLContext;
42 import org.jooq.Record1;
43 import org.jooq.SQLDialect;
44 import org.jooq.impl.DSL;
45 import org.junit.jupiter.api.Assertions;
46 import org.junit.jupiter.api.BeforeAll;
47 import org.junit.jupiter.api.BeforeEach;
48 import org.junit.jupiter.api.Test;
49 import org.oran.smo.teiv.schema.EntityType;
50 import org.oran.smo.teiv.schema.RelationType;
51 import org.oran.smo.teiv.schema.SchemaRegistryException;
52 import org.springframework.boot.jdbc.DataSourceBuilder;
53 import org.springframework.context.annotation.Configuration;
54
55 import org.oran.smo.teiv.db.TestPostgresqlContainer;
56 import org.oran.smo.teiv.exposure.spi.ModelRepository;
57 import org.oran.smo.teiv.exposure.spi.Module;
58 import org.oran.smo.teiv.exposure.spi.ModuleStatus;
59 import org.oran.smo.teiv.exposure.teivpath.refiner.BasePathRefinement;
60 import org.oran.smo.teiv.schema.PostgresSchemaLoader;
61 import org.oran.smo.teiv.schema.SchemaLoaderException;
62 import org.oran.smo.teiv.schema.SchemaRegistry;
63
64 @Configuration
65 class DataRepositoryImplGETRequestsContainerizedTest {
66     private static TestPostgresqlContainer postgreSQLContainer = TestPostgresqlContainer.getInstance();
67     private static DataRepositoryImpl underTest;
68     private static ModelRepository modelRepository;
69     private static BasePathRefinement basePathRefinement;
70     private static DSLContext readWriteDataDslContext;
71     private static DSLContext readDataDslContext;
72     private static DSLContext writeDataDslContext;
73
74     @BeforeAll
75     public static void beforeAll() throws UnsupportedOperationException, SchemaLoaderException {
76         String url = postgreSQLContainer.getJdbcUrl();
77         DataSource ds = DataSourceBuilder.create().url(url).username("test").password("test").build();
78         readDataDslContext = DSL.using(ds, SQLDialect.POSTGRES);
79         writeDataDslContext = DSL.using(ds, SQLDialect.POSTGRES);
80         underTest = new DataRepositoryImpl(basePathRefinement, readDataDslContext);
81         modelRepository = new ModelRepositoryImpl(readDataDslContext, readWriteDataDslContext, writeDataDslContext);
82         PostgresSchemaLoader postgresSchemaLoader = new PostgresSchemaLoader(readDataDslContext, new ObjectMapper());
83         postgresSchemaLoader.loadSchemaRegistry();
84     }
85
86     @BeforeEach
87     public void deleteAll() {
88         TestPostgresqlContainer.truncateSchemas(List.of(TEIV_DATA_SCHEMA, TEIV_CONSUMER_DATA_SCHEMA), writeDataDslContext);
89         TestPostgresqlContainer.loadSampleData();
90     }
91
92     //TODO: Make this test class generic for all repositories
93     @Test
94     void testSchemaCRUD() {
95         final String moduleName = "newSchema";
96         Optional<Module> schemaByName = modelRepository.getConsumerModuleByName(moduleName);
97         Assertions.assertFalse(schemaByName.isPresent());
98
99         Module schema = Module.builder().name(moduleName).namespace("new-namespace").domain("NEW_DOMAIN").content(
100                 "yang content {} \n\n \t\t\t;").ownerAppId("APP").revision("2024-07-15").status(ModuleStatus.IN_USAGE)
101                 .revision("2024-10-04").build();
102         modelRepository.createConsumerDataModule(schema, List.of(), Map.of());
103         schemaByName = modelRepository.getConsumerModuleByName(moduleName);
104         Assertions.assertTrue(schemaByName.isPresent());
105
106         modelRepository.doesModuleExists(TEIV_MODEL, moduleName);
107         modelRepository.doesModuleExists(TEIV_CONSUMER_DATA, moduleName);
108
109         modelRepository.updateModuleStatus(moduleName, ModuleStatus.DELETING);
110
111         final @NotNull Record1<Object> status = readDataDslContext.select(field("status")).from(table(String.format(
112                 TEIV_CONSUMER_DATA, MODULE_REFERENCE))).where(field("name").eq(moduleName)).fetchAny();
113
114         Assertions.assertEquals(ModuleStatus.DELETING.name(), status.get("status"));
115
116         modelRepository.deleteModuleByName(moduleName);
117
118         schemaByName = modelRepository.getConsumerModuleByName(moduleName);
119         Assertions.assertFalse(schemaByName.isPresent());
120     }
121
122     @Test
123     void getClassifiersForSchema() {
124         assertThat(List.of("test-app-module:Indoor", "test-app-module:Outdoor", "test-app-module:Rural",
125                 "test-app-module:Weekday", "test-app-module:Weekend")).hasSameElementsAs(underTest.getClassifiersForSchema(
126                         "test-app-module"));
127     }
128
129     @Test
130     void getDecoratorsForSchema() {
131         assertThat(List.of("test-app-module:textdata", "test-app-module:intdata")).hasSameElementsAs(underTest
132                 .getDecoratorsForSchema("test-app-module"));
133     }
134
135     @Test
136     void getRelationshipIdsForDecoratorDeletionTest() throws SchemaRegistryException {
137         Assertions.assertEquals(Collections.singletonList(
138                 "urn:o-ran:smo:teiv:sha512:ANTENNAMODULE_SERVES_ANTENNACAPABILITY=ABD52B030DF1169F9F41C898913EF30F7BB5741F53352F482310B280C90AC569B7D31D52A2BB41F1F0099AE1EDD56CACF0B285D145A5584D376DD45DED1E2D65"),
139                 underTest.getRelationshipIdsForDecoratorDeletion(SchemaRegistry.getRelationTypeByModuleAndName(
140                         "o-ran-smo-teiv-rel-equipment-ran", "ANTENNAMODULE_SERVES_ANTENNACAPABILITY"), Set.of(
141                                 "ocucp-ocuup-model:metadata")));
142
143         Assertions.assertEquals(Collections.singletonList(
144                 "urn:o-ran:smo:teiv:sha512:MANAGEDELEMENT_MANAGES_OCUUPFUNCTION=5255F37093F8EB3763CE5F017DFC1E162B44FC9DF6E13744C04DC1832C5E754AB7BE440DBE1187EE8EEE42FD04E652BB8148655C6F977B1FFDDA54FE87C6411A"),
145                 underTest.getRelationshipIdsForDecoratorDeletion(SchemaRegistry.getRelationTypeByModuleAndName(
146                         "o-ran-smo-teiv-rel-oam-ran", "MANAGEDELEMENT_MANAGES_OCUUPFUNCTION"), Set.of(
147                                 "ocucp-ocuup-model:metadata")));
148     }
149
150     @Test
151     void getRelationshipIdsForClassifierDeletionTest() throws SchemaRegistryException {
152         Assertions.assertEquals(Collections.singletonList(
153                 "urn:o-ran:smo:teiv:sha512:ANTENNAMODULE_SERVES_ANTENNACAPABILITY=ABD52B030DF1169F9F41C898913EF30F7BB5741F53352F482310B280C90AC569B7D31D52A2BB41F1F0099AE1EDD56CACF0B285D145A5584D376DD45DED1E2D65"),
154                 underTest.getRelationshipIdsForClassifierDeletion(SchemaRegistry.getRelationTypeByModuleAndName(
155                         "o-ran-smo-teiv-rel-equipment-ran", "ANTENNAMODULE_SERVES_ANTENNACAPABILITY"), Set.of(
156                                 "ocucp-ocuup-model:Weekend")));
157
158         Assertions.assertEquals(Collections.singletonList(
159                 "urn:o-ran:smo:teiv:sha512:MANAGEDELEMENT_MANAGES_OCUUPFUNCTION=5255F37093F8EB3763CE5F017DFC1E162B44FC9DF6E13744C04DC1832C5E754AB7BE440DBE1187EE8EEE42FD04E652BB8148655C6F977B1FFDDA54FE87C6411A"),
160                 underTest.getRelationshipIdsForClassifierDeletion(SchemaRegistry.getRelationTypeByModuleAndName(
161                         "o-ran-smo-teiv-rel-oam-ran", "MANAGEDELEMENT_MANAGES_OCUUPFUNCTION"), Set.of(
162                                 "ocucp-ocuup-model:Weekend")));
163     }
164
165     @Test
166     void getEntityIdsForDecoratorDeletionTest() throws SchemaRegistryException {
167         Assertions.assertEquals(Collections.singletonList(
168                 "urn:3gpp:dn:SubNetwork=Europe,SubNetwork=Hungary,MeContext=1,ManagedElement=9,ODUFunction=9,NRSectorCarrier=1"),
169                 underTest.getEntityIdsForDecoratorDeletion(SchemaRegistry.getEntityTypeByModuleAndName("o-ran-smo-teiv-ran",
170                         "NRSectorCarrier"), Set.of("ocucp-ocuup-model:metadata")));
171     }
172
173     @Test
174     void getEntityIdsForClassifierDeletionTest() throws SchemaRegistryException {
175         Assertions.assertEquals(Collections.singletonList(
176                 "urn:3gpp:dn:SubNetwork=Europe,SubNetwork=Hungary,MeContext=1,ManagedElement=9,ODUFunction=9,NRSectorCarrier=1"),
177                 underTest.getEntityIdsForClassifierDeletion(SchemaRegistry.getEntityTypeByModuleAndName(
178                         "o-ran-smo-teiv-ran", "NRSectorCarrier"), Set.of("ocucp-ocuup-model:Weekend")));
179     }
180
181     @Test
182     void isTopologyExist_entityTest() throws SchemaRegistryException {
183         EntityType entityType = SchemaRegistry.getEntityTypeByDomainAndName("RAN", "NRCellDU").get(0);
184         String entityId = "urn:3gpp:dn:SubNetwork=Europe,SubNetwork=Hungary,MeContext=1,ManagedElement=19,ODUFunction=19,NRCellDU=91";
185         Assertions.assertTrue(underTest.isTopologyExist(entityType, entityId),
186                 "That's strange, the result should have been true as this id in DB dataset: " + entityId);
187         String nonExistentEntityId = "urn:3gpp:dn:SubNetwork=Europe,SubNetwork=Hungary,MeContext=1,ManagedElement=19,ODUFunction=19,NRCellDU=911";
188         Assertions.assertFalse(underTest.isTopologyExist(entityType, nonExistentEntityId),
189                 "Result should have been false as the entity id is not in DB data set: " + nonExistentEntityId);
190     }
191
192     @Test
193     void isTopologyExist_relationshipTest() throws SchemaRegistryException {
194         RelationType relationType = SchemaRegistry.getRelationTypeByDomainAndName("RAN",
195                 "NRSECTORCARRIER_USES_ANTENNACAPABILITY").get(0);
196         String aSideRelationshipId = "urn:o-ran:smo:teiv:sha512:NRSECTORCARRIER_USES_ANTENNACAPABILITY=11EDFC31E2BE240D3CB15DB1A3FA3B78C828524BC8FCA3365A615129A61A627C21DA8EBF6DD788CDBDEC668344D1F79A371749083D6AE04DDDD57CB4FA8C3ECB";
197         Assertions.assertTrue(underTest.isTopologyExist(relationType, aSideRelationshipId),
198                 "That's strange, the result should have been true as this id in DB dataset: " + aSideRelationshipId);
199
200         relationType = SchemaRegistry.getRelationTypeByDomainAndName("REL_OAM_RAN", "MANAGEDELEMENT_MANAGES_OCUUPFUNCTION")
201                 .get(0);
202         String bSideRelationshipId = "urn:o-ran:smo:teiv:sha512:MANAGEDELEMENT_MANAGES_OCUUPFUNCTION=5255F37093F8EB3763CE5F017DFC1E162B44FC9DF6E13744C04DC1832C5E754AB7BE440DBE1187EE8EEE42FD04E652BB8148655C6F977B1FFDDA54FE87C6411A";
203         Assertions.assertTrue(underTest.isTopologyExist(relationType, bSideRelationshipId),
204                 "That's strange, the result should have been true as this id in DB dataset: " + bSideRelationshipId);
205
206         relationType = SchemaRegistry.getRelationTypeByDomainAndName("REL_EQUIPMENT_RAN",
207                 "ANTENNAMODULE_SERVES_ANTENNACAPABILITY").get(0);
208         String manyToManyRelationshipId = "urn:o-ran:smo:teiv:sha512:ANTENNAMODULE_SERVES_ANTENNACAPABILITY=ABD52B030DF1169F9F41C898913EF30F7BB5741F53352F482310B280C90AC569B7D31D52A2BB41F1F0099AE1EDD56CACF0B285D145A5584D376DD45DED1E2D65";
209         Assertions.assertTrue(underTest.isTopologyExist(relationType, manyToManyRelationshipId),
210                 "That's strange, the result should have been true as this id in DB dataset: " + manyToManyRelationshipId);
211
212         String nonExistentRelationshipId = "urn:o-ran:smo:teiv:sha512:MANAGEDELEMENT_MANAGES_OCUUPFUNCTION=5255F37093F8EB3763CE5F017DFC1E162B44FC9DF6E13744C04DC1832C5E754AB7BE440DBE1187EE8EEE42FD04E652BB8148655C6F977B1FFDDA54FE87C6411A";
213         Assertions.assertFalse(underTest.isTopologyExist(relationType, nonExistentRelationshipId),
214                 "Result should have been false as the relationship id is not in DB data set: " + nonExistentRelationshipId);
215     }
216
217 }