0921962e6fa468c081c980d6bdca9301ba40dd2f
[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.exposure.spi.impl;
22
23 import static org.oran.smo.teiv.utils.TiesConstants.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.TiesConstants.TIES_CONSUMER_DATA;
28 import static org.oran.smo.teiv.utils.TiesConstants.TIES_CONSUMER_DATA_SCHEMA;
29 import static org.oran.smo.teiv.utils.TiesConstants.TIES_DATA_SCHEMA;
30 import static org.oran.smo.teiv.utils.TiesConstants.TIES_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.SchemaRegistryException;
50 import org.springframework.boot.jdbc.DataSourceBuilder;
51 import org.springframework.context.annotation.Configuration;
52
53 import org.oran.smo.teiv.db.TestPostgresqlContainer;
54 import org.oran.smo.teiv.exposure.spi.ModelRepository;
55 import org.oran.smo.teiv.exposure.spi.Module;
56 import org.oran.smo.teiv.exposure.spi.ModuleStatus;
57 import org.oran.smo.teiv.exposure.tiespath.refiner.BasePathRefinement;
58 import org.oran.smo.teiv.schema.PostgresSchemaLoader;
59 import org.oran.smo.teiv.schema.SchemaLoaderException;
60 import org.oran.smo.teiv.schema.SchemaRegistry;
61
62 @Configuration
63 class DataRepositoryImplGETRequestsContainerizedTest {
64     private static TestPostgresqlContainer postgreSQLContainer = TestPostgresqlContainer.getInstance();
65     private static DataRepositoryImpl underTest;
66     private static ModelRepository modelRepository;
67     private static BasePathRefinement basePathRefinement;
68     private static DSLContext readWriteDataDslContext;
69     private static DSLContext readDataDslContext;
70     private static DSLContext writeDataDslContext;
71
72     @BeforeAll
73     public static void beforeAll() throws UnsupportedOperationException, SchemaLoaderException {
74         String url = postgreSQLContainer.getJdbcUrl();
75         DataSource ds = DataSourceBuilder.create().url(url).username("test").password("test").build();
76         readDataDslContext = DSL.using(ds, SQLDialect.POSTGRES);
77         writeDataDslContext = DSL.using(ds, SQLDialect.POSTGRES);
78         underTest = new DataRepositoryImpl(basePathRefinement, readDataDslContext);
79         modelRepository = new ModelRepositoryImpl(readDataDslContext, readWriteDataDslContext, writeDataDslContext);
80         PostgresSchemaLoader postgresSchemaLoader = new PostgresSchemaLoader(readDataDslContext, new ObjectMapper());
81         postgresSchemaLoader.loadSchemaRegistry();
82     }
83
84     @BeforeEach
85     public void deleteAll() {
86         TestPostgresqlContainer.truncateSchemas(List.of(TIES_DATA_SCHEMA, TIES_CONSUMER_DATA_SCHEMA), writeDataDslContext);
87         TestPostgresqlContainer.loadSampleData();
88     }
89
90     //TODO: Make this test class generic for all repositories
91     @Test
92     void testSchemaCRUD() {
93         final String moduleName = "newSchema";
94         Optional<Module> schemaByName = modelRepository.getConsumerModuleByName(moduleName);
95         Assertions.assertFalse(schemaByName.isPresent());
96
97         Module schema = Module.builder().name(moduleName).namespace("new-namespace").domain("NEW_DOMAIN").content(
98                 "yang content {} \n\n \t\t\t;").ownerAppId("APP").revision("2024-07-15").status(ModuleStatus.IN_USAGE)
99                 .build();
100         modelRepository.createConsumerDataModule(schema, List.of(), Map.of());
101         schemaByName = modelRepository.getConsumerModuleByName(moduleName);
102         Assertions.assertTrue(schemaByName.isPresent());
103
104         modelRepository.doesModuleExists(TIES_MODEL, moduleName);
105         modelRepository.doesModuleExists(TIES_CONSUMER_DATA, moduleName);
106
107         modelRepository.updateModuleStatus(moduleName, ModuleStatus.DELETING);
108
109         final @NotNull Record1<Object> status = readDataDslContext.select(field("status")).from(table(String.format(
110                 TIES_CONSUMER_DATA, MODULE_REFERENCE))).where(field("name").eq(moduleName)).fetchAny();
111
112         Assertions.assertEquals(ModuleStatus.DELETING.name(), status.get("status"));
113
114         modelRepository.deleteModuleByName(moduleName);
115
116         schemaByName = modelRepository.getConsumerModuleByName(moduleName);
117         Assertions.assertFalse(schemaByName.isPresent());
118     }
119
120     @Test
121     void getClassifiersForSchema() {
122         assertThat(List.of("test-app-module:Indoor", "test-app-module:Outdoor", "test-app-module:Rural",
123                 "test-app-module:Weekday", "test-app-module:Weekend")).hasSameElementsAs(underTest.getClassifiersForSchema(
124                         "test-app-module"));
125     }
126
127     @Test
128     void getDecoratorsForSchema() {
129         assertThat(List.of("test-app-module:textdata", "test-app-module:intdata")).hasSameElementsAs(underTest
130                 .getDecoratorsForSchema("test-app-module"));
131     }
132
133     @Test
134     void getRelationshipIdsForDecoratorDeletionTest() throws SchemaRegistryException {
135         Assertions.assertEquals(Collections.singletonList(
136                 "urn:o-ran:smo:teiv:sha512:ANTENNAMODULE_SERVES_ANTENNACAPABILITY=ABD52B030DF1169F9F41C898913EF30F7BB5741F53352F482310B280C90AC569B7D31D52A2BB41F1F0099AE1EDD56CACF0B285D145A5584D376DD45DED1E2D65"),
137                 underTest.getRelationshipIdsForDecoratorDeletion(SchemaRegistry.getRelationTypeByModuleAndName(
138                         "o-ran-smo-teiv-rel-equipment-ran", "ANTENNAMODULE_SERVES_ANTENNACAPABILITY"), Set.of(
139                                 "ocucp-ocuup-model:metadata")));
140
141         Assertions.assertEquals(Collections.singletonList(
142                 "urn:o-ran:smo:teiv:sha512:MANAGEDELEMENT_MANAGES_OCUUPFUNCTION=5255F37093F8EB3763CE5F017DFC1E162B44FC9DF6E13744C04DC1832C5E754AB7BE440DBE1187EE8EEE42FD04E652BB8148655C6F977B1FFDDA54FE87C6411A"),
143                 underTest.getRelationshipIdsForDecoratorDeletion(SchemaRegistry.getRelationTypeByModuleAndName(
144                         "o-ran-smo-teiv-rel-oam-ran", "MANAGEDELEMENT_MANAGES_OCUUPFUNCTION"), Set.of(
145                                 "ocucp-ocuup-model:metadata")));
146     }
147
148     @Test
149     void getRelationshipIdsForClassifierDeletionTest() throws SchemaRegistryException {
150         Assertions.assertEquals(Collections.singletonList(
151                 "urn:o-ran:smo:teiv:sha512:ANTENNAMODULE_SERVES_ANTENNACAPABILITY=ABD52B030DF1169F9F41C898913EF30F7BB5741F53352F482310B280C90AC569B7D31D52A2BB41F1F0099AE1EDD56CACF0B285D145A5584D376DD45DED1E2D65"),
152                 underTest.getRelationshipIdsForClassifierDeletion(SchemaRegistry.getRelationTypeByModuleAndName(
153                         "o-ran-smo-teiv-rel-equipment-ran", "ANTENNAMODULE_SERVES_ANTENNACAPABILITY"), Set.of(
154                                 "ocucp-ocuup-model:Weekend")));
155
156         Assertions.assertEquals(Collections.singletonList(
157                 "urn:o-ran:smo:teiv:sha512:MANAGEDELEMENT_MANAGES_OCUUPFUNCTION=5255F37093F8EB3763CE5F017DFC1E162B44FC9DF6E13744C04DC1832C5E754AB7BE440DBE1187EE8EEE42FD04E652BB8148655C6F977B1FFDDA54FE87C6411A"),
158                 underTest.getRelationshipIdsForClassifierDeletion(SchemaRegistry.getRelationTypeByModuleAndName(
159                         "o-ran-smo-teiv-rel-oam-ran", "MANAGEDELEMENT_MANAGES_OCUUPFUNCTION"), Set.of(
160                                 "ocucp-ocuup-model:Weekend")));
161     }
162
163     @Test
164     void getEntityIdsForDecoratorDeletionTest() throws SchemaRegistryException {
165         Assertions.assertEquals(Collections.singletonList(
166                 "urn:3gpp:dn:SubNetwork=Europe,SubNetwork=Hungary,MeContext=1,ManagedElement=9,ODUFunction=9,NRSectorCarrier=1"),
167                 underTest.getEntityIdsForDecoratorDeletion(SchemaRegistry.getEntityTypeByModuleAndName("o-ran-smo-teiv-ran",
168                         "NRSectorCarrier"), Set.of("ocucp-ocuup-model:metadata")));
169     }
170
171     @Test
172     void getEntityIdsForClassifierDeletionTest() throws SchemaRegistryException {
173         Assertions.assertEquals(Collections.singletonList(
174                 "urn:3gpp:dn:SubNetwork=Europe,SubNetwork=Hungary,MeContext=1,ManagedElement=9,ODUFunction=9,NRSectorCarrier=1"),
175                 underTest.getEntityIdsForClassifierDeletion(SchemaRegistry.getEntityTypeByModuleAndName(
176                         "o-ran-smo-teiv-ran", "NRSectorCarrier"), Set.of("ocucp-ocuup-model:Weekend")));
177     }
178
179 }