b88ca1ca36783a92de3bfb9081bde4369cf93c10
[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).build();
105
106         modelRepository.createModule(schema);
107
108         schemaByName = modelRepository.getConsumerModuleByName(moduleName);
109         Assertions.assertTrue(schemaByName.isPresent());
110
111         modelRepository.doesModuleExists(TIES_MODEL, moduleName);
112         modelRepository.doesModuleExists(TIES_CONSUMER_DATA, moduleName);
113
114         modelRepository.updateModuleStatus(moduleName, ModuleStatus.DELETING);
115
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();
118
119         Assertions.assertEquals(ModuleStatus.DELETING.name(), status.get("status"));
120
121         modelRepository.deleteModuleByName(moduleName);
122
123         schemaByName = modelRepository.getConsumerModuleByName(moduleName);
124         Assertions.assertFalse(schemaByName.isPresent());
125     }
126
127     @Test
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(
131                         "test-app-module"));
132     }
133
134     @Test
135     void getDecoratorsForSchema() {
136         assertThat(List.of("test-app-module:textdata", "test-app-module:intdata")).hasSameElementsAs(underTest
137                 .getDecoratorsForSchema("test-app-module"));
138     }
139
140     @Test
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")));
146
147         Assertions.assertEquals(Collections.singletonList(
148                 "urn:base64:TWFuYWdlZEVsZW1lbnQ6RTY0MzcxQ0Q0RDEyRUQwQ0VEMjAwREQzQTc1OTE3ODQ6TUFOQUdFUzpHTkJDVVVQRnVuY3Rpb246QkZFRUFDMkNFNjAyNzNDQjBBNzgzMTlDQzIwMUE3RkU="),
149                 underTest.getRelationshipIdsForDecoratorDeletion(SchemaRegistry.getRelationTypeByName(
150                         "MANAGEDELEMENT_MANAGES_GNBCUUPFUNCTION"), Set.of("gnbcucp-gnbcuup-model:metadata")));
151     }
152
153     @Test
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")));
159
160         Assertions.assertEquals(Collections.singletonList(
161                 "urn:base64:TWFuYWdlZEVsZW1lbnQ6RTY0MzcxQ0Q0RDEyRUQwQ0VEMjAwREQzQTc1OTE3ODQ6TUFOQUdFUzpHTkJDVVVQRnVuY3Rpb246QkZFRUFDMkNFNjAyNzNDQjBBNzgzMTlDQzIwMUE3RkU="),
162                 underTest.getRelationshipIdsForClassifierDeletion(SchemaRegistry.getRelationTypeByName(
163                         "MANAGEDELEMENT_MANAGES_GNBCUUPFUNCTION"), Set.of("gnbcucp-gnbcuup-model:Weekend")));
164     }
165
166     @Test
167     void getEntityIdsForDecoratorDeletionTest() {
168         Assertions.assertEquals(Collections.singletonList("E49D942C16E0364E1E0788138916D70C"), underTest
169                 .getEntityIdsForDecoratorDeletion(SchemaRegistry.getEntityTypeByName("NRSectorCarrier"), Set.of(
170                         "gnbcucp-gnbcuup-model:metadata")));
171     }
172
173     @Test
174     void getEntityIdsForClassifierDeletionTest() {
175         Assertions.assertEquals(Collections.singletonList("E49D942C16E0364E1E0788138916D70C"), underTest
176                 .getEntityIdsForClassifierDeletion(SchemaRegistry.getEntityTypeByName("NRSectorCarrier"), Set.of(
177                         "gnbcucp-gnbcuup-model:Weekend")));
178     }
179
180 }