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.ImmutableMap;
28 import org.junit.ClassRule;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.onap.cps.DatabaseTestContainer;
32 import org.onap.cps.spi.CpsModulePersistenceService;
33 import org.onap.cps.spi.entities.Dataspace;
34 import org.onap.cps.spi.entities.SchemaSet;
35 import org.onap.cps.spi.entities.YangResource;
36 import org.onap.cps.spi.exceptions.DataspaceNotFoundException;
37 import org.onap.cps.spi.exceptions.SchemaSetAlreadyDefinedException;
38 import org.onap.cps.spi.repository.DataspaceRepository;
39 import org.onap.cps.spi.repository.SchemaSetRepository;
40 import org.onap.cps.spi.repository.YangResourceRepository;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.boot.test.context.SpringBootTest;
43 import org.springframework.test.context.jdbc.Sql;
44 import org.springframework.test.context.jdbc.SqlGroup;
45 import org.springframework.test.context.junit4.SpringRunner;
48 @RunWith(SpringRunner.class)
50 public class CpsModulePersistenceServiceTest {
52 private static final String CLEAR_DATA = "/data/clear-all.sql";
53 private static final String SET_DATA = "/data/schemaset.sql";
55 private static final String DATASPACE_NAME = "DATASPACE-001";
56 private static final String DATASPACE_NAME_INVALID = "DATASPACE-X";
57 private static final String SCHEMA_SET_NAME = "SCHEMA-SET-001";
58 private static final String SCHEMA_SET_NAME_NEW = "SCHEMA-SET-NEW";
60 private static final String EXISTING_RESOURCE_NAME = "module1@2020-02-02.yang";
61 private static final String EXISTING_RESOURCE_CONTENT = "CONTENT-001";
62 private static final String EXISTING_RESOURCE_CHECKSUM = "877e65a9f36d54e7702c3f073f6bc42b";
63 private static final Long EXISTING_RESOURCE_ID = 3001L;
65 private static final String NEW_RESOURCE_NAME = "new-module@2020-02-02.yang";
66 private static final String NEW_RESOURCE_CONTENT = "CONTENT-NEW";
67 private static final String NEW_RESOURCE_CHECKSUM = "c94d40a1350eb1c0b1c1949eac84fc59";
68 private static final Long NEW_RESOURCE_ABSTRACT_ID = 0L;
71 public static DatabaseTestContainer testContainer = DatabaseTestContainer.getInstance();
74 private CpsModulePersistenceService cpsModulePersistenceService;
77 private DataspaceRepository dataspaceRepository;
80 private YangResourceRepository yangResourceRepository;
83 private SchemaSetRepository schemaSetRepository;
86 @Test(expected = DataspaceNotFoundException.class)
88 public void testStoreSchemaSetToInvalidDataspace() {
89 cpsModulePersistenceService.storeSchemaSet(DATASPACE_NAME_INVALID, SCHEMA_SET_NAME_NEW,
90 toMap(NEW_RESOURCE_NAME, NEW_RESOURCE_CONTENT));
93 @Test(expected = SchemaSetAlreadyDefinedException.class)
94 @SqlGroup({@Sql(CLEAR_DATA), @Sql(SET_DATA)})
95 public void testStoreDuplicateSchemaSet() {
96 cpsModulePersistenceService.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME,
97 toMap(NEW_RESOURCE_NAME, NEW_RESOURCE_CONTENT));
101 @SqlGroup({@Sql(CLEAR_DATA), @Sql(SET_DATA)})
102 public void testStoreSchemaSetWithNewYangResource() {
103 final Map<String, String> yangResourcesNameToContentMap = toMap(NEW_RESOURCE_NAME, NEW_RESOURCE_CONTENT);
104 cpsModulePersistenceService.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW,
105 yangResourcesNameToContentMap);
106 assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW,
107 NEW_RESOURCE_ABSTRACT_ID, NEW_RESOURCE_NAME, NEW_RESOURCE_CONTENT, NEW_RESOURCE_CHECKSUM);
108 assertEquals(yangResourcesNameToContentMap,
109 cpsModulePersistenceService.getYangSchemaResources(DATASPACE_NAME, SCHEMA_SET_NAME_NEW));
113 @SqlGroup({@Sql(CLEAR_DATA), @Sql(SET_DATA)})
114 public void testStoreSchemaSetWithExistingYangResourceReuse() {
115 cpsModulePersistenceService.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW,
116 toMap(NEW_RESOURCE_NAME, EXISTING_RESOURCE_CONTENT));
117 assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW,
118 EXISTING_RESOURCE_ID, EXISTING_RESOURCE_NAME, EXISTING_RESOURCE_CONTENT, EXISTING_RESOURCE_CHECKSUM);
121 private void assertSchemaSetPersisted(final String expectedDataspaceName, final String expectedSchemaSetName,
122 final Long expectedYangResourceId, final String expectedYangResourceName,
123 final String expectedYangResourceContent,
124 final String expectedYangResourceChecksum) {
126 // assert the schema set is persisted
127 final SchemaSet schemaSet = getSchemaSetFromDatabase(expectedDataspaceName, expectedSchemaSetName);
128 assertEquals(expectedDataspaceName, schemaSet.getDataspace().getName());
129 assertEquals(expectedSchemaSetName, schemaSet.getName());
131 // assert the attached yang resource is persisted
132 final Set<YangResource> yangResources = schemaSet.getYangResources();
133 assertNotNull(yangResources);
134 assertEquals(1, yangResources.size());
136 // assert the attached yang resource content
137 final YangResource yangResource = yangResources.iterator().next();
138 assertNotNull(yangResource.getId());
139 if (expectedYangResourceId != NEW_RESOURCE_ABSTRACT_ID) {
140 // existing resource with known id
141 assertEquals(expectedYangResourceId, yangResource.getId());
143 assertEquals(expectedYangResourceName, yangResource.getName());
144 assertEquals(expectedYangResourceContent, yangResource.getContent());
145 assertEquals(expectedYangResourceChecksum, yangResource.getChecksum());
148 private static Map<String, String> toMap(final String key, final String value) {
149 return ImmutableMap.<String, String>builder().put(key, value).build();
152 private SchemaSet getSchemaSetFromDatabase(final String dataspaceName, final String schemaSetName) {
153 final Dataspace dataspace = dataspaceRepository.findByName(dataspaceName).orElseThrow();
154 return schemaSetRepository.findByDataspaceAndName(dataspace, schemaSetName).orElseThrow();