aboutsummaryrefslogtreecommitdiffstats
path: root/cps-ri/src/test/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceTest.java
blob: 050ee265a5e1ff9ab19e91234534f27f892d7f98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
 *  ============LICENSE_START=======================================================
 *  Copyright (C) 2020 Pantheon.tech
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *  ============LICENSE_END=========================================================
 */

package org.onap.cps.spi.impl;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED;
import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED;

import com.google.common.collect.ImmutableMap;
import java.util.Map;
import java.util.Set;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.onap.cps.DatabaseTestContainer;
import org.onap.cps.spi.CpsAdminPersistenceService;
import org.onap.cps.spi.CpsModulePersistenceService;
import org.onap.cps.spi.entities.DataspaceEntity;
import org.onap.cps.spi.entities.SchemaSetEntity;
import org.onap.cps.spi.entities.YangResourceEntity;
import org.onap.cps.spi.exceptions.DataspaceNotFoundException;
import org.onap.cps.spi.exceptions.SchemaSetAlreadyDefinedException;
import org.onap.cps.spi.exceptions.SchemaSetInUseException;
import org.onap.cps.spi.exceptions.SchemaSetNotFoundException;
import org.onap.cps.spi.repository.AnchorRepository;
import org.onap.cps.spi.repository.DataspaceRepository;
import org.onap.cps.spi.repository.FragmentRepository;
import org.onap.cps.spi.repository.SchemaSetRepository;
import org.onap.cps.spi.repository.YangResourceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.SqlGroup;
import org.springframework.test.context.junit4.SpringRunner;


@RunWith(SpringRunner.class)
@SpringBootTest
public class CpsModulePersistenceServiceTest {

    private static final String CLEAR_DATA = "/data/clear-all.sql";
    private static final String SET_DATA = "/data/schemaset.sql";

    private static final String ANCHOR_NAME = "ANCHOR-001";
    private static final String DATASPACE_NAME = "DATASPACE-001";
    private static final String DATASPACE_NAME_INVALID = "DATASPACE-X";
    private static final String SCHEMA_SET_NAME = "SCHEMA-SET-001";
    private static final String SCHEMA_SET_NAME_NEW = "SCHEMA-SET-NEW";
    private static final String SCHEMA_SET_NAME_NO_ANCHORS = "SCHEMA-SET-100";
    private static final String SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA = "SCHEMA-SET-101";

    private static final String EXISTING_RESOURCE_NAME = "module1@2020-02-02.yang";
    private static final String EXISTING_RESOURCE_CONTENT = "CONTENT-001";
    private static final String EXISTING_RESOURCE_CHECKSUM = "877e65a9f36d54e7702c3f073f6bc42b";
    private static final Long EXISTING_RESOURCE_ID = 3001L;

    private static final String NEW_RESOURCE_NAME = "new-module@2020-02-02.yang";
    private static final String NEW_RESOURCE_CONTENT = "CONTENT-NEW";
    private static final String NEW_RESOURCE_CHECKSUM = "c94d40a1350eb1c0b1c1949eac84fc59";
    private static final Long NEW_RESOURCE_ABSTRACT_ID = 0L;

    private static final Long SHARED_RESOURCE_ID1 = 3003L;
    private static final Long SHARED_RESOURCE_ID2 = 3004L;
    private static final Long ORPHAN_RESOURCE_ID = 3100L;
    private static final Integer REMOVED_ANCHOR_ID1 = 6001;
    private static final Integer REMOVED_ANCHOR_ID2 = 6002;
    private static final Long REMOVED_FRAGMENT_ID = 7001L;

    @ClassRule
    public static DatabaseTestContainer testContainer = DatabaseTestContainer.getInstance();

    @Autowired
    private CpsModulePersistenceService cpsModulePersistenceService;

    @Autowired
    private CpsAdminPersistenceService cpsAdminPersistenceService;

    @Autowired
    DataspaceRepository dataspaceRepository;

    @Autowired
    private SchemaSetRepository schemaSetRepository;

    @Autowired
    private YangResourceRepository yangResourceRepository;

    @Autowired
    private AnchorRepository anchorRepository;

    @Autowired
    private FragmentRepository fragmentRepository;

    @Test(expected = DataspaceNotFoundException.class)
    @Sql(CLEAR_DATA)
    public void testStoreSchemaSetToInvalidDataspace() {
        cpsModulePersistenceService.storeSchemaSet(DATASPACE_NAME_INVALID, SCHEMA_SET_NAME_NEW,
            toMap(NEW_RESOURCE_NAME, NEW_RESOURCE_CONTENT));
    }

    @Test(expected = SchemaSetAlreadyDefinedException.class)
    @Sql({CLEAR_DATA, SET_DATA})
    public void testStoreDuplicateSchemaSet() {
        cpsModulePersistenceService.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME,
            toMap(NEW_RESOURCE_NAME, NEW_RESOURCE_CONTENT));
    }

    @Test
    @Sql({CLEAR_DATA, SET_DATA})
    public void testStoreSchemaSetWithNewYangResource() {
        final Map<String, String> yangResourcesNameToContentMap = toMap(NEW_RESOURCE_NAME, NEW_RESOURCE_CONTENT);
        cpsModulePersistenceService.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW,
            yangResourcesNameToContentMap);
        assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW,
            NEW_RESOURCE_ABSTRACT_ID, NEW_RESOURCE_NAME, NEW_RESOURCE_CONTENT, NEW_RESOURCE_CHECKSUM);
        assertEquals(yangResourcesNameToContentMap,
            cpsModulePersistenceService.getYangSchemaResources(DATASPACE_NAME, SCHEMA_SET_NAME_NEW));
    }

    @Test
    @Sql({CLEAR_DATA, SET_DATA})
    public void testGetYangResourcesWithAnchorName() {
        final Map<String, String> yangResourcesNameToContentMap =
            toMap(NEW_RESOURCE_NAME, NEW_RESOURCE_CONTENT);
        cpsModulePersistenceService.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW,
            yangResourcesNameToContentMap);

        cpsAdminPersistenceService.createAnchor(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, ANCHOR_NAME);
        assertEquals(yangResourcesNameToContentMap,
            cpsModulePersistenceService.getYangSchemaSetResources(DATASPACE_NAME, ANCHOR_NAME));
    }

    @Test
    @Sql({CLEAR_DATA, SET_DATA})
    public void testStoreSchemaSetWithExistingYangResourceReuse() {
        cpsModulePersistenceService.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW,
            toMap(NEW_RESOURCE_NAME, EXISTING_RESOURCE_CONTENT));
        assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW,
            EXISTING_RESOURCE_ID, EXISTING_RESOURCE_NAME, EXISTING_RESOURCE_CONTENT, EXISTING_RESOURCE_CHECKSUM);
    }

    private void assertSchemaSetPersisted(final String expectedDataspaceName, final String expectedSchemaSetName,
        final Long expectedYangResourceId, final String expectedYangResourceName,
        final String expectedYangResourceContent,
        final String expectedYangResourceChecksum) {

        // assert the schema set is persisted
        final SchemaSetEntity schemaSetEntity = getSchemaSetFromDatabase(expectedDataspaceName, expectedSchemaSetName);
        assertEquals(expectedDataspaceName, schemaSetEntity.getDataspace().getName());
        assertEquals(expectedSchemaSetName, schemaSetEntity.getName());

        // assert the attached yang resource is persisted
        final Set<YangResourceEntity> yangResourceEntities = schemaSetEntity.getYangResources();
        assertNotNull(yangResourceEntities);
        assertEquals(1, yangResourceEntities.size());

        // assert the attached yang resource content
        final YangResourceEntity yangResourceEntity = yangResourceEntities.iterator().next();
        assertNotNull(yangResourceEntity.getId());
        if (expectedYangResourceId != NEW_RESOURCE_ABSTRACT_ID) {
            // existing resource with known id
            assertEquals(expectedYangResourceId, yangResourceEntity.getId());
        }
        assertEquals(expectedYangResourceName, yangResourceEntity.getName());
        assertEquals(expectedYangResourceContent, yangResourceEntity.getContent());
        assertEquals(expectedYangResourceChecksum, yangResourceEntity.getChecksum());
    }

    @Test
    @Sql({CLEAR_DATA, SET_DATA})
    public void testStrictDeleteSchemaSetNoAnchors() {
        cpsModulePersistenceService.deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NO_ANCHORS,
            CASCADE_DELETE_PROHIBITED);

        // validate schema set removed
        final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(DATASPACE_NAME);
        assertFalse(schemaSetRepository
            .findByDataspaceAndName(dataspaceEntity, SCHEMA_SET_NAME_NO_ANCHORS).isPresent());

        // validate shared resource remain, but orphan one is removed
        assertTrue(yangResourceRepository.findById(SHARED_RESOURCE_ID1).isPresent());
        assertFalse(yangResourceRepository.findById(ORPHAN_RESOURCE_ID).isPresent());
    }


    @Test
    @Sql({CLEAR_DATA, SET_DATA})
    public void testFullDeleteSchemaSetWithAnchorsAndData() {
        cpsModulePersistenceService
            .deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA, CASCADE_DELETE_ALLOWED);

        // validate schema set removed
        final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(DATASPACE_NAME);
        assertFalse(schemaSetRepository
            .findByDataspaceAndName(dataspaceEntity, SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA).isPresent());

        // validate shared resources remain
        assertTrue(yangResourceRepository.findById(SHARED_RESOURCE_ID1).isPresent());
        assertTrue(yangResourceRepository.findById(SHARED_RESOURCE_ID2).isPresent());

        // validate associated anchors and data are removed
        assertFalse(anchorRepository.findById(REMOVED_ANCHOR_ID1).isPresent());
        assertFalse(anchorRepository.findById(REMOVED_ANCHOR_ID2).isPresent());
        assertFalse(fragmentRepository.findById(REMOVED_FRAGMENT_ID).isPresent());
    }

    @Test(expected = DataspaceNotFoundException.class)
    @Sql(CLEAR_DATA)
    public void testDeleteSchemaSetWithinInvalidDataspace() {
        cpsModulePersistenceService.deleteSchemaSet(DATASPACE_NAME_INVALID, SCHEMA_SET_NAME, CASCADE_DELETE_ALLOWED);
    }

    @Test(expected = SchemaSetNotFoundException.class)
    @Sql({CLEAR_DATA, SET_DATA})
    public void testDeleteNonExistingSchemaSet() {
        cpsModulePersistenceService.deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, CASCADE_DELETE_ALLOWED);
    }

    @Test(expected = SchemaSetInUseException.class)
    @Sql({CLEAR_DATA, SET_DATA})
    public void testStrictDeleteSchemaSetInUse() {
        cpsModulePersistenceService
            .deleteSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_WITH_ANCHORS_AND_DATA, CASCADE_DELETE_PROHIBITED);
    }

    private static Map<String, String> toMap(final String key, final String value) {
        return ImmutableMap.<String, String>builder().put(key, value).build();
    }

    private SchemaSetEntity getSchemaSetFromDatabase(final String dataspaceName, final String schemaSetName) {
        final DataspaceEntity dataspaceEntity = dataspaceRepository.findByName(dataspaceName).orElseThrow();
        return schemaSetRepository.findByDataspaceAndName(dataspaceEntity, schemaSetName).orElseThrow();
    }
}