2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation
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 junit.framework.TestCase.assertEquals;
24 import com.google.common.collect.ImmutableSet;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import org.assertj.core.api.Assertions;
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.CpsDataPersistenceService;
33 import org.onap.cps.spi.entities.FragmentEntity;
34 import org.onap.cps.spi.exceptions.AnchorNotFoundException;
35 import org.onap.cps.spi.exceptions.DataspaceNotFoundException;
36 import org.onap.cps.spi.exceptions.NotFoundInDataspaceException;
37 import org.onap.cps.spi.model.DataNode;
38 import org.onap.cps.spi.repository.FragmentRepository;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.boot.test.context.SpringBootTest;
41 import org.springframework.dao.DataIntegrityViolationException;
42 import org.springframework.test.context.jdbc.Sql;
43 import org.springframework.test.context.junit4.SpringRunner;
46 @RunWith(SpringRunner.class)
48 public class CpsDataPersistenceServiceTest {
50 private static final String CLEAR_DATA = "/data/clear-all.sql";
51 private static final String SET_DATA = "/data/fragment.sql";
53 private static final String DATASPACE_NAME = "DATASPACE-001";
54 private static final String ANCHOR_NAME1 = "ANCHOR-001";
56 private static final long PARENT_ID_4001 = 4001;
57 private static final long PARENT_ID_4002 = 4002;
58 private static final long PARENT_ID_4003 = 4003;
59 private static final String PARENT_XPATH1 = "/parent-1";
60 private static final String PARENT_XPATH2 = "/parent-2";
61 private static final String PARENT_XPATH3 = "/parent-3";
63 private static final long CHILD_ID_4004 = 4004;
64 private static final String CHILD_XPATH1 = "/parent-1/child-1";
65 private static final String CHILD_XPATH2 = "/parent-2/child-2";
67 private static final long GRAND_CHILD_ID_4006 = 4006;
68 private static final String GRAND_CHILD_XPATH1 = "/parent-1/child-1/grandchild-1";
72 public static DatabaseTestContainer databaseTestContainer = DatabaseTestContainer.getInstance();
75 private CpsDataPersistenceService cpsDataPersistenceService;
78 private FragmentRepository fragmentRepository;
81 @Sql({CLEAR_DATA, SET_DATA})
82 public void testGetFragmentsWithChildAndGrandChild() {
83 final FragmentEntity parentFragment = fragmentRepository.findById(PARENT_ID_4001).orElseThrow();
84 final FragmentEntity childFragment = fragmentRepository.findById(CHILD_ID_4004).orElseThrow();
85 final FragmentEntity grandChildFragment = fragmentRepository.findById(GRAND_CHILD_ID_4006).orElseThrow();
86 assertFragment(parentFragment, childFragment, grandChildFragment, PARENT_XPATH1, CHILD_XPATH1,
90 @Test(expected = DataspaceNotFoundException.class)
91 @Sql({CLEAR_DATA, SET_DATA})
92 public void testStoreDataNodeAtNonExistingDataspace() {
93 cpsDataPersistenceService
94 .storeDataNode("Non Existing Dataspace Name", ANCHOR_NAME1, new DataNode());
97 @Test(expected = AnchorNotFoundException.class)
98 @Sql({CLEAR_DATA, SET_DATA})
99 public void testStoreDataNodeAtNonExistingAnchor() {
100 cpsDataPersistenceService
101 .storeDataNode(DATASPACE_NAME, "Non Existing Anchor Name", new DataNode());
104 @Test(expected = DataIntegrityViolationException.class)
105 @Sql({CLEAR_DATA, SET_DATA})
106 public void testStoreDataNodeWithIntegrityException() {
107 cpsDataPersistenceService.storeDataNode(DATASPACE_NAME, ANCHOR_NAME1,
108 createDataNodeTree(PARENT_XPATH1));
112 @Sql({CLEAR_DATA, SET_DATA})
113 public void testStoreDataNodeWithChildAndGrandChild() {
114 final String parentXpath = "/parent-new";
115 final String childXpath = "/parent-new/child-new";
116 final String grandChildXpath = "/parent-new/child-new/grandchild-new";
118 cpsDataPersistenceService.storeDataNode(DATASPACE_NAME, ANCHOR_NAME1,
119 createDataNodeTree(parentXpath, childXpath, grandChildXpath));
120 final FragmentEntity parentFragment = getFragmentByXpath(parentXpath);
121 final FragmentEntity childFragment = getFragmentByXpath(childXpath);
122 final FragmentEntity grandChildFragment = getFragmentByXpath(grandChildXpath);
123 assertFragment(parentFragment, childFragment, grandChildFragment, parentXpath, childXpath,
128 @Sql({CLEAR_DATA, SET_DATA})
129 public void testAddChildToFragmentThatHasOneChild() {
130 final String childXpath = "some-xpath";
131 final DataNode childDataNode = createDataNodeTree(childXpath);
132 cpsDataPersistenceService
133 .addChildDataNode(DATASPACE_NAME, ANCHOR_NAME1, PARENT_XPATH2, childDataNode);
134 final FragmentEntity parentFragment = fragmentRepository.findById(PARENT_ID_4002).orElseThrow();
135 Assertions.assertThat(parentFragment.getChildFragments())
137 .extracting(FragmentEntity::getXpath)
138 .containsExactlyInAnyOrder(childXpath, CHILD_XPATH2);
142 @Sql({CLEAR_DATA, SET_DATA})
143 public void testAddChildToFragmentThatHasNoChild() {
144 final String childXpath = "some-xpath";
145 final DataNode childDataNode = createDataNodeTree(childXpath);
146 cpsDataPersistenceService
147 .addChildDataNode(DATASPACE_NAME, ANCHOR_NAME1, PARENT_XPATH3, childDataNode);
148 final FragmentEntity parentFragment = fragmentRepository.findById(PARENT_ID_4003).orElseThrow();
149 Assertions.assertThat(parentFragment.getChildFragments())
151 .extracting(FragmentEntity::getXpath)
152 .containsExactlyInAnyOrder(childXpath);
155 @Test(expected = DataIntegrityViolationException.class)
156 @Sql({CLEAR_DATA, SET_DATA})
157 public void testAddAChildWithTheSameXpathAsExistingChild() {
158 cpsDataPersistenceService
159 .addChildDataNode(DATASPACE_NAME, ANCHOR_NAME1, PARENT_XPATH1, createDataNodeTree(CHILD_XPATH1));
162 @Test(expected = NotFoundInDataspaceException.class)
163 @Sql({CLEAR_DATA, SET_DATA})
164 public void testAddAChildWithToAParentThatDoesNotExist() {
165 cpsDataPersistenceService
166 .addChildDataNode(DATASPACE_NAME, ANCHOR_NAME1, "non-existing-xpath", createDataNodeTree("some-xpath"));
169 private void assertFragment(final FragmentEntity parentFragment, final FragmentEntity childFragment,
170 final FragmentEntity grandChildFragment, final String parentXpath, final String childXpath,
171 final String grandChildXpath) {
172 assertEquals(parentXpath, parentFragment.getXpath());
173 assertEquals(DATASPACE_NAME, parentFragment.getDataspace().getName());
174 assertEquals(ANCHOR_NAME1, parentFragment.getAnchor().getName());
176 assertEquals(childXpath, childFragment.getXpath());
177 assertEquals(DATASPACE_NAME, childFragment.getDataspace().getName());
178 assertEquals(ANCHOR_NAME1, childFragment.getAnchor().getName());
180 assertEquals(grandChildXpath, grandChildFragment.getXpath());
181 assertEquals(DATASPACE_NAME, grandChildFragment.getDataspace().getName());
182 assertEquals(ANCHOR_NAME1, grandChildFragment.getAnchor().getName());
185 private FragmentEntity getFragmentByXpath(final String xpath) {
186 return fragmentRepository.findAll().stream()
187 .filter(fragment -> fragment.getXpath().contains(xpath)).findAny().orElseThrow();
190 private static DataNode createDataNodeTree(final String... xpaths) {
191 final DataNode dataNode = DataNode.builder().xpath(xpaths[0]).childDataNodes(Collections.emptySet()).build();
192 if (xpaths.length > 1) {
193 final String[] xPathsDescendant = Arrays.copyOfRange(xpaths, 1, xpaths.length);
194 final DataNode childDataNode = createDataNodeTree(xPathsDescendant);
195 dataNode.setChildDataNodes(ImmutableSet.of(childDataNode));