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.exposure.spi.impl;
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_DATA_SCHEMA;
29 import static org.oran.smo.teiv.utils.TiesConstants.TIES_MODEL;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.Optional;
35 import javax.sql.DataSource;
37 import com.fasterxml.jackson.databind.ObjectMapper;
38 import org.jetbrains.annotations.NotNull;
39 import org.jooq.DSLContext;
40 import org.jooq.Record1;
41 import org.jooq.SQLDialect;
42 import org.jooq.impl.DSL;
43 import org.junit.jupiter.api.Assertions;
44 import org.junit.jupiter.api.BeforeAll;
45 import org.junit.jupiter.api.BeforeEach;
46 import org.junit.jupiter.api.Test;
47 import org.springframework.boot.jdbc.DataSourceBuilder;
48 import org.springframework.boot.test.context.SpringBootTest;
49 import org.springframework.boot.test.mock.mockito.MockBean;
50 import org.springframework.context.annotation.Configuration;
52 import org.oran.smo.teiv.db.TestPostgresqlContainer;
53 import org.oran.smo.teiv.exposure.spi.ModelRepository;
54 import org.oran.smo.teiv.exposure.spi.Module;
55 import org.oran.smo.teiv.exposure.spi.ModuleStatus;
56 import org.oran.smo.teiv.exposure.tiespath.refiner.BasePathRefinement;
57 import org.oran.smo.teiv.schema.PostgresSchemaLoader;
58 import org.oran.smo.teiv.schema.SchemaLoaderException;
59 import org.oran.smo.teiv.schema.SchemaRegistry;
60 import org.oran.smo.teiv.startup.SchemaHandler;
64 class DataRepositoryImplGETRequestsContainerizedTest {
65 public static TestPostgresqlContainer postgreSQLContainer = TestPostgresqlContainer.getInstance();
66 private static DataRepositoryImpl underTest;
67 private static ModelRepository modelRepository;
68 private static BasePathRefinement basePathRefinement;
69 private static DSLContext readWriteDataDslContext;
70 private static DSLContext readDataDslContext;
71 private static DSLContext writeDataDslContext;
74 private SchemaHandler schemaHandler;
77 public static void beforeAll() throws UnsupportedOperationException, SchemaLoaderException {
78 String url = postgreSQLContainer.getJdbcUrl();
79 DataSource ds = DataSourceBuilder.create().url(url).username("test").password("test").build();
80 readDataDslContext = DSL.using(ds, SQLDialect.POSTGRES);
81 writeDataDslContext = DSL.using(ds, SQLDialect.POSTGRES);
82 underTest = new DataRepositoryImpl(basePathRefinement, readDataDslContext);
83 modelRepository = new ModelRepositoryImpl(readDataDslContext, readWriteDataDslContext, writeDataDslContext);
84 PostgresSchemaLoader postgresSchemaLoader = new PostgresSchemaLoader(readDataDslContext, new ObjectMapper());
85 postgresSchemaLoader.loadSchemaRegistry();
86 TestPostgresqlContainer.loadSampleData();
90 public void deleteAll() {
91 writeDataDslContext.meta().filterSchemas(s -> s.getName().equals(TIES_DATA_SCHEMA)).getTables().forEach(
92 t -> writeDataDslContext.truncate(t).cascade().execute());
93 TestPostgresqlContainer.loadSampleData();
96 //TODO: Make this test class generic for all repositories
98 void testSchemaCRUD() {
99 final String moduleName = "newSchema";
100 Optional<Module> schemaByName = modelRepository.getConsumerModuleByName(moduleName);
101 Assertions.assertFalse(schemaByName.isPresent());
103 Module schema = Module.builder().name(moduleName).namespace("new-namespace").domain("NEW_DOMAIN").content(
104 "yang content {} \n\n \t\t\t;").ownerAppId("APP").status(ModuleStatus.IN_USAGE).build();
106 modelRepository.createModule(schema);
108 schemaByName = modelRepository.getConsumerModuleByName(moduleName);
109 Assertions.assertTrue(schemaByName.isPresent());
111 modelRepository.doesModuleExists(TIES_MODEL, moduleName);
112 modelRepository.doesModuleExists(TIES_CONSUMER_DATA, moduleName);
114 modelRepository.updateModuleStatus(moduleName, ModuleStatus.DELETING);
116 final @NotNull Record1<Object> status = readDataDslContext.select(field("status")).from(table(String.format(
117 TIES_CONSUMER_DATA, MODULE_REFERENCE))).where(field("name").eq(moduleName)).fetchAny();
119 Assertions.assertEquals(ModuleStatus.DELETING.name(), status.get("status"));
121 modelRepository.deleteModuleByName(moduleName);
123 schemaByName = modelRepository.getConsumerModuleByName(moduleName);
124 Assertions.assertFalse(schemaByName.isPresent());
128 void getClassifiersForSchema() {
129 assertThat(List.of("test-app-module:Indoor", "test-app-module:Outdoor", "test-app-module:Rural",
130 "test-app-module:Weekday", "test-app-module:Weekend")).hasSameElementsAs(underTest.getClassifiersForSchema(
135 void getDecoratorsForSchema() {
136 assertThat(List.of("test-app-module:textdata", "test-app-module:intdata")).hasSameElementsAs(underTest
137 .getDecoratorsForSchema("test-app-module"));
141 void getRelationshipIdsForDecoratorDeletionTest() {
142 Assertions.assertEquals(Collections.singletonList(
143 "urn:base64:R05CQ1VVUEZ1bmN0aW9uOkJGRUVBQzJDRTYwMjczQ0IwQTc4MzE5Q0MyMDFBN0ZFOlJFQUxJU0VEX0JZOkNsb3VkTmF0aXZlQXBwbGljYXRpb246QUQ0MkQ5MDQ5N0U5M0QyNzYyMTVERjZEM0I4OTlFMTc="),
144 underTest.getRelationshipIdsForDecoratorDeletion(SchemaRegistry.getRelationTypeByName(
145 "GNBCUUPFUNCTION_REALISED_BY_CLOUDNATIVEAPPLICATION"), Set.of("gnbcucp-gnbcuup-model:metadata")));
147 Assertions.assertEquals(Collections.singletonList(
148 "urn:base64:TWFuYWdlZEVsZW1lbnQ6RTY0MzcxQ0Q0RDEyRUQwQ0VEMjAwREQzQTc1OTE3ODQ6TUFOQUdFUzpHTkJDVVVQRnVuY3Rpb246QkZFRUFDMkNFNjAyNzNDQjBBNzgzMTlDQzIwMUE3RkU="),
149 underTest.getRelationshipIdsForDecoratorDeletion(SchemaRegistry.getRelationTypeByName(
150 "MANAGEDELEMENT_MANAGES_GNBCUUPFUNCTION"), Set.of("gnbcucp-gnbcuup-model:metadata")));
154 void getRelationshipIdsForClassifierDeletionTest() {
155 Assertions.assertEquals(Collections.singletonList(
156 "urn:base64:R05CQ1VVUEZ1bmN0aW9uOkJGRUVBQzJDRTYwMjczQ0IwQTc4MzE5Q0MyMDFBN0ZFOlJFQUxJU0VEX0JZOkNsb3VkTmF0aXZlQXBwbGljYXRpb246QUQ0MkQ5MDQ5N0U5M0QyNzYyMTVERjZEM0I4OTlFMTc="),
157 underTest.getRelationshipIdsForClassifierDeletion(SchemaRegistry.getRelationTypeByName(
158 "GNBCUUPFUNCTION_REALISED_BY_CLOUDNATIVEAPPLICATION"), Set.of("gnbcucp-gnbcuup-model:Weekend")));
160 Assertions.assertEquals(Collections.singletonList(
161 "urn:base64:TWFuYWdlZEVsZW1lbnQ6RTY0MzcxQ0Q0RDEyRUQwQ0VEMjAwREQzQTc1OTE3ODQ6TUFOQUdFUzpHTkJDVVVQRnVuY3Rpb246QkZFRUFDMkNFNjAyNzNDQjBBNzgzMTlDQzIwMUE3RkU="),
162 underTest.getRelationshipIdsForClassifierDeletion(SchemaRegistry.getRelationTypeByName(
163 "MANAGEDELEMENT_MANAGES_GNBCUUPFUNCTION"), Set.of("gnbcucp-gnbcuup-model:Weekend")));
167 void getEntityIdsForDecoratorDeletionTest() {
168 Assertions.assertEquals(Collections.singletonList("E49D942C16E0364E1E0788138916D70C"), underTest
169 .getEntityIdsForDecoratorDeletion(SchemaRegistry.getEntityTypeByName("NRSectorCarrier"), Set.of(
170 "gnbcucp-gnbcuup-model:metadata")));
174 void getEntityIdsForClassifierDeletionTest() {
175 Assertions.assertEquals(Collections.singletonList("E49D942C16E0364E1E0788138916D70C"), underTest
176 .getEntityIdsForClassifierDeletion(SchemaRegistry.getEntityTypeByName("NRSectorCarrier"), Set.of(
177 "gnbcucp-gnbcuup-model:Weekend")));