a93d277ea1047c3a622bd612320e979c61025cf9
[cps.git] / cps-ri / src / test / java / org / onap / cps / spi / impl / CpsModulePersistenceServiceTest.java
1 /*
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
8  *
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.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.cps.spi.impl;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24
25 import com.google.common.collect.ImmutableSet;
26 import java.util.Set;
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;
45
46
47 @RunWith(SpringRunner.class)
48 @SpringBootTest
49 public class CpsModulePersistenceServiceTest {
50
51     private static final String CLEAR_DATA = "/data/clear-all.sql";
52     private static final String SET_DATA = "/data/schemaset.sql";
53
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;
63
64     @ClassRule
65     public static DatabaseTestContainer testContainer = DatabaseTestContainer.getInstance();
66
67     @Autowired
68     private CpsModulePersistenceService cpsModulePersistenceService;
69
70     @Autowired
71     private DataspaceRepository dataspaceRepository;
72
73     @Autowired
74     private YangResourceRepository yangResourceRepository;
75
76     @Autowired
77     private SchemaSetRepository schemaSetRepository;
78
79
80     @Test(expected = DataspaceNotFoundException.class)
81     @Sql(CLEAR_DATA)
82     public void testStoreSchemaSetToInvalidDataspace() {
83         cpsModulePersistenceService
84             .storeSchemaSet(DATASPACE_NAME_INVALID, SCHEMA_SET_NAME_NEW, toSet(NEW_RESOURCE_CONTENT));
85     }
86
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));
91     }
92
93     @Test
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);
99     }
100
101     @Test
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);
107     }
108
109     private void assertSchemaSetPersisted(final String expectedDataspaceName, final String expectedSchemaSetName,
110                                           final Long expectedYangResourceId, final String expectedYangResourceContent,
111                                           final String expectedYangResourceChecksum) {
112
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());
117
118         // assert the attached yang resource is persisted
119         final Set<YangResource> yangResources = schemaSet.getYangResources();
120         assertNotNull(yangResources);
121         assertEquals(1, yangResources.size());
122
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());
129         }
130         assertEquals(expectedYangResourceContent, yangResource.getContent());
131         assertEquals(expectedYangResourceChecksum, yangResource.getChecksum());
132     }
133
134     private static Set<String> toSet(final String... elements) {
135         return ImmutableSet.<String>builder().add(elements).build();
136     }
137
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();
141     }
142 }