b9b1054590a02c8190a58b9083e2cf521b22a708
[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_DATA_SCHEMA;
29 import static org.oran.smo.teiv.utils.TiesConstants.TIES_MODEL;
30
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.Optional;
34 import java.util.Set;
35 import javax.sql.DataSource;
36
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;
51
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;
61
62 @Configuration
63 @SpringBootTest
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;
72
73     @MockBean
74     private SchemaHandler schemaHandler;
75
76     @BeforeAll
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();
87     }
88
89     @BeforeEach
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();
94     }
95
96     //TODO: Make this test class generic for all repositories
97     @Test
98     void testSchemaCRUD() {
99         final String moduleName = "newSchema";
100         Optional<Module> schemaByName = modelRepository.getConsumerModuleByName(moduleName);
101         Assertions.assertFalse(schemaByName.isPresent());
102
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).revision("2024-07-15")
105                 .build();
106
107         modelRepository.createModule(schema);
108
109         schemaByName = modelRepository.getConsumerModuleByName(moduleName);
110         Assertions.assertTrue(schemaByName.isPresent());
111
112         modelRepository.doesModuleExists(TIES_MODEL, moduleName);
113         modelRepository.doesModuleExists(TIES_CONSUMER_DATA, moduleName);
114
115         modelRepository.updateModuleStatus(moduleName, ModuleStatus.DELETING);
116
117         final @NotNull Record1<Object> status = readDataDslContext.select(field("status")).from(table(String.format(
118                 TIES_CONSUMER_DATA, MODULE_REFERENCE))).where(field("name").eq(moduleName)).fetchAny();
119
120         Assertions.assertEquals(ModuleStatus.DELETING.name(), status.get("status"));
121
122         modelRepository.deleteModuleByName(moduleName);
123
124         schemaByName = modelRepository.getConsumerModuleByName(moduleName);
125         Assertions.assertFalse(schemaByName.isPresent());
126     }
127
128     @Test
129     void getClassifiersForSchema() {
130         assertThat(List.of("test-app-module:Indoor", "test-app-module:Outdoor", "test-app-module:Rural",
131                 "test-app-module:Weekday", "test-app-module:Weekend")).hasSameElementsAs(underTest.getClassifiersForSchema(
132                         "test-app-module"));
133     }
134
135     @Test
136     void getDecoratorsForSchema() {
137         assertThat(List.of("test-app-module:textdata", "test-app-module:intdata")).hasSameElementsAs(underTest
138                 .getDecoratorsForSchema("test-app-module"));
139     }
140
141     @Test
142     void getRelationshipIdsForDecoratorDeletionTest() {
143         Assertions.assertEquals(Collections.singletonList(
144                 "urn:base64:R05CQ1VVUEZ1bmN0aW9uOkJGRUVBQzJDRTYwMjczQ0IwQTc4MzE5Q0MyMDFBN0ZFOlJFQUxJU0VEX0JZOkNsb3VkTmF0aXZlQXBwbGljYXRpb246QUQ0MkQ5MDQ5N0U5M0QyNzYyMTVERjZEM0I4OTlFMTc="),
145                 underTest.getRelationshipIdsForDecoratorDeletion(SchemaRegistry.getRelationTypeByName(
146                         "GNBCUUPFUNCTION_REALISED_BY_CLOUDNATIVEAPPLICATION"), Set.of("gnbcucp-gnbcuup-model:metadata")));
147
148         Assertions.assertEquals(Collections.singletonList(
149                 "urn:base64:TWFuYWdlZEVsZW1lbnQ6RTY0MzcxQ0Q0RDEyRUQwQ0VEMjAwREQzQTc1OTE3ODQ6TUFOQUdFUzpHTkJDVVVQRnVuY3Rpb246QkZFRUFDMkNFNjAyNzNDQjBBNzgzMTlDQzIwMUE3RkU="),
150                 underTest.getRelationshipIdsForDecoratorDeletion(SchemaRegistry.getRelationTypeByName(
151                         "MANAGEDELEMENT_MANAGES_GNBCUUPFUNCTION"), Set.of("gnbcucp-gnbcuup-model:metadata")));
152     }
153
154     @Test
155     void getRelationshipIdsForClassifierDeletionTest() {
156         Assertions.assertEquals(Collections.singletonList(
157                 "urn:base64:R05CQ1VVUEZ1bmN0aW9uOkJGRUVBQzJDRTYwMjczQ0IwQTc4MzE5Q0MyMDFBN0ZFOlJFQUxJU0VEX0JZOkNsb3VkTmF0aXZlQXBwbGljYXRpb246QUQ0MkQ5MDQ5N0U5M0QyNzYyMTVERjZEM0I4OTlFMTc="),
158                 underTest.getRelationshipIdsForClassifierDeletion(SchemaRegistry.getRelationTypeByName(
159                         "GNBCUUPFUNCTION_REALISED_BY_CLOUDNATIVEAPPLICATION"), Set.of("gnbcucp-gnbcuup-model:Weekend")));
160
161         Assertions.assertEquals(Collections.singletonList(
162                 "urn:base64:TWFuYWdlZEVsZW1lbnQ6RTY0MzcxQ0Q0RDEyRUQwQ0VEMjAwREQzQTc1OTE3ODQ6TUFOQUdFUzpHTkJDVVVQRnVuY3Rpb246QkZFRUFDMkNFNjAyNzNDQjBBNzgzMTlDQzIwMUE3RkU="),
163                 underTest.getRelationshipIdsForClassifierDeletion(SchemaRegistry.getRelationTypeByName(
164                         "MANAGEDELEMENT_MANAGES_GNBCUUPFUNCTION"), Set.of("gnbcucp-gnbcuup-model:Weekend")));
165     }
166
167     @Test
168     void getEntityIdsForDecoratorDeletionTest() {
169         Assertions.assertEquals(Collections.singletonList("E49D942C16E0364E1E0788138916D70C"), underTest
170                 .getEntityIdsForDecoratorDeletion(SchemaRegistry.getEntityTypeByName("NRSectorCarrier"), Set.of(
171                         "gnbcucp-gnbcuup-model:metadata")));
172     }
173
174     @Test
175     void getEntityIdsForClassifierDeletionTest() {
176         Assertions.assertEquals(Collections.singletonList("E49D942C16E0364E1E0788138916D70C"), underTest
177                 .getEntityIdsForClassifierDeletion(SchemaRegistry.getEntityTypeByName("NRSectorCarrier"), Set.of(
178                         "gnbcucp-gnbcuup-model:Weekend")));
179     }
180
181 }