2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Pantheon.tech
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.cps.spi.impl;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
25 import com.google.common.collect.ImmutableSet;
27 import org.junit.ClassRule;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.onap.cps.DatabaseTestContainer;
31 import org.onap.cps.spi.CpsModulePersistenceService;
32 import org.onap.cps.spi.entities.Dataspace;
33 import org.onap.cps.spi.entities.SchemaSet;
34 import org.onap.cps.spi.entities.YangResource;
35 import org.onap.cps.spi.exceptions.DataspaceNotFoundException;
36 import org.onap.cps.spi.exceptions.SchemaSetAlreadyDefinedException;
37 import org.onap.cps.spi.repository.DataspaceRepository;
38 import org.onap.cps.spi.repository.SchemaSetRepository;
39 import org.onap.cps.spi.repository.YangResourceRepository;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.boot.test.context.SpringBootTest;
42 import org.springframework.test.context.jdbc.Sql;
43 import org.springframework.test.context.jdbc.SqlGroup;
44 import org.springframework.test.context.junit4.SpringRunner;
47 @RunWith(SpringRunner.class)
49 public class CpsModulePersistenceServiceTest {
51 private static final String CLEAR_DATA = "/data/clear-all.sql";
52 private static final String SET_DATA = "/data/schemaset.sql";
54 private static final String DATASPACE_NAME = "DATASPACE-001";
55 private static final String DATASPACE_NAME_INVALID = "DATASPACE-X";
56 private static final String SCHEMA_SET_NAME = "SCHEMA-SET-001";
57 private static final String SCHEMA_SET_NAME_NEW = "SCHEMA-SET-NEW";
58 private static final String OLD_RESOURCE_CONTENT = "CONTENT-001";
59 private static final String OLD_RESOURCE_CHECKSUM = "877e65a9f36d54e7702c3f073f6bc42b";
60 private static final String NEW_RESOURCE_CONTENT = "CONTENT-NEW";
61 private static final String NEW_RESOURCE_CHECKSUM = "c94d40a1350eb1c0b1c1949eac84fc59";
62 private static final Long NEW_RESOURCE_ABSTRACT_ID = 0L;
65 public static DatabaseTestContainer testContainer = DatabaseTestContainer.getInstance();
68 private CpsModulePersistenceService cpsModulePersistenceService;
71 private DataspaceRepository dataspaceRepository;
74 private YangResourceRepository yangResourceRepository;
77 private SchemaSetRepository schemaSetRepository;
80 @Test(expected = DataspaceNotFoundException.class)
82 public void testStoreSchemaSetToInvalidDataspace() {
83 cpsModulePersistenceService
84 .storeSchemaSet(DATASPACE_NAME_INVALID, SCHEMA_SET_NAME_NEW, toSet(NEW_RESOURCE_CONTENT));
87 @Test(expected = SchemaSetAlreadyDefinedException.class)
88 @SqlGroup({@Sql(CLEAR_DATA), @Sql(SET_DATA)})
89 public void testStoreDuplicateSchemaSet() {
90 cpsModulePersistenceService.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME, toSet(NEW_RESOURCE_CONTENT));
94 @SqlGroup({@Sql(CLEAR_DATA), @Sql(SET_DATA)})
95 public void testStoreSchemaSetWithNewYangResource() {
96 cpsModulePersistenceService.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, toSet(NEW_RESOURCE_CONTENT));
97 assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW,
98 NEW_RESOURCE_ABSTRACT_ID, NEW_RESOURCE_CONTENT, NEW_RESOURCE_CHECKSUM);
102 @SqlGroup({@Sql(CLEAR_DATA), @Sql(SET_DATA)})
103 public void testStoreSchemaSetWithExistingYangResourceReuse() {
104 cpsModulePersistenceService.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, toSet(OLD_RESOURCE_CONTENT));
105 assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW,
106 3001L, OLD_RESOURCE_CONTENT, OLD_RESOURCE_CHECKSUM);
109 private void assertSchemaSetPersisted(final String expectedDataspaceName, final String expectedSchemaSetName,
110 final Long expectedYangResourceId, final String expectedYangResourceContent,
111 final String expectedYangResourceChecksum) {
113 // assert the schema set is persisted
114 final SchemaSet schemaSet = getSchemaSetFromDatabase(expectedDataspaceName, expectedSchemaSetName);
115 assertEquals(expectedDataspaceName, schemaSet.getDataspace().getName());
116 assertEquals(expectedSchemaSetName, schemaSet.getName());
118 // assert the attached yang resource is persisted
119 final Set<YangResource> yangResources = schemaSet.getYangResources();
120 assertNotNull(yangResources);
121 assertEquals(1, yangResources.size());
123 // assert the attached yang resource content
124 final YangResource yangResource = yangResources.iterator().next();
125 assertNotNull(yangResource.getId());
126 if (expectedYangResourceId != NEW_RESOURCE_ABSTRACT_ID) {
127 // existing resource with known id
128 assertEquals(expectedYangResourceId, yangResource.getId());
130 assertEquals(expectedYangResourceContent, yangResource.getContent());
131 assertEquals(expectedYangResourceChecksum, yangResource.getChecksum());
134 private static Set<String> toSet(final String... elements) {
135 return ImmutableSet.<String>builder().add(elements).build();
138 private SchemaSet getSchemaSetFromDatabase(final String dataspaceName, final String schemaSetName) {
139 final Dataspace dataspace = dataspaceRepository.findByName(dataspaceName).orElseThrow();
140 return schemaSetRepository.findByDataspaceAndName(dataspace, schemaSetName).orElseThrow();