Persisting data nodes (fragments tree structure)
[cps.git] / cps-ri / src / test / java / org / onap / cps / spi / impl / CpsDataPersistenceServiceTest.java
1 /*
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
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 junit.framework.TestCase.assertEquals;
23
24 import com.google.common.collect.ImmutableSet;
25 import java.util.Collections;
26 import org.junit.ClassRule;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.onap.cps.DatabaseTestContainer;
30 import org.onap.cps.spi.CpsDataPersistenceService;
31 import org.onap.cps.spi.entities.FragmentEntity;
32 import org.onap.cps.spi.exceptions.AnchorNotFoundException;
33 import org.onap.cps.spi.exceptions.DataspaceNotFoundException;
34 import org.onap.cps.spi.model.DataNode;
35 import org.onap.cps.spi.repository.FragmentRepository;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.boot.test.context.SpringBootTest;
38 import org.springframework.dao.DataIntegrityViolationException;
39 import org.springframework.test.context.jdbc.Sql;
40 import org.springframework.test.context.junit4.SpringRunner;
41
42
43 @RunWith(SpringRunner.class)
44 @SpringBootTest
45 public class CpsDataPersistenceServiceTest {
46
47     private static final String CLEAR_DATA = "/data/clear-all.sql";
48     private static final String SET_DATA = "/data/fragment.sql";
49
50     private static final String NON_EXISTING_DATASPACE_NAME = "NON EXISTING DATASPACE";
51     private static final String DATASPACE_NAME = "DATASPACE-001";
52     private static final String ANCHOR_NAME1 = "ANCHOR-001";
53     private static final String NON_EXISTING_ANCHOR_NAME = "NON EXISTING ANCHOR";
54     private static final String PARENT_XPATH = "/parent";
55     private static final String CHILD_XPATH = "/parent/child";
56     private static final String GRAND_CHILD_XPATH = "/parent/child/grandchild";
57     private static final String PARENT_XPATH_NEW = "/parent-new";
58     private static final String CHILD_XPATH_NEW = "/parent/child-new";
59     private static final String GRAND_CHILD_XPATH_NEW = "/parent/child/grandchild-new";
60     private static final long PARENT_ID = 3001;
61     private static final long CHILD_ID = 3002;
62     private static final long GRAND_CHILD_ID = 3003;
63     private static final long PARENT_ID_NEW = 2;
64     private static final long CHILD_ID_NEW = 3;
65     private static final long GRAND_CHILD_ID_NEW = 4;
66
67     @ClassRule
68     public static DatabaseTestContainer databaseTestContainer = DatabaseTestContainer.getInstance();
69
70     @Autowired
71     private CpsDataPersistenceService cpsDataPersistenceService;
72
73     @Autowired
74     private FragmentRepository fragmentRepository;
75
76     @Test
77     @Sql({CLEAR_DATA, SET_DATA})
78     public void testGetFragmentsWithChildAndGrandChild() {
79         final FragmentEntity parentFragment = fragmentRepository.findById(PARENT_ID).orElseThrow();
80         final FragmentEntity childFragment = fragmentRepository.findById(CHILD_ID).orElseThrow();
81         final FragmentEntity grandChildFragment = fragmentRepository.findById(GRAND_CHILD_ID).orElseThrow();
82
83         assertFragment(parentFragment, childFragment, grandChildFragment, PARENT_XPATH, CHILD_XPATH, GRAND_CHILD_XPATH);
84     }
85
86     @Test(expected = DataspaceNotFoundException.class)
87     @Sql({CLEAR_DATA, SET_DATA})
88     public void testStoreDataNodeAtNonExistingDataspace() {
89         cpsDataPersistenceService
90             .storeDataNode(NON_EXISTING_DATASPACE_NAME, ANCHOR_NAME1,
91                 createDataNodeWithChildAndGrandChild(PARENT_XPATH_NEW, CHILD_XPATH_NEW, GRAND_CHILD_XPATH_NEW));
92     }
93
94     @Test(expected = AnchorNotFoundException.class)
95     @Sql({CLEAR_DATA, SET_DATA})
96     public void testStoreDataNodeAtNonExistingAnchor() {
97         cpsDataPersistenceService
98             .storeDataNode(DATASPACE_NAME, NON_EXISTING_ANCHOR_NAME,
99                 createDataNodeWithChildAndGrandChild(PARENT_XPATH_NEW, CHILD_XPATH_NEW, GRAND_CHILD_XPATH_NEW));
100     }
101
102     @Test(expected = DataIntegrityViolationException.class)
103     @Sql({CLEAR_DATA, SET_DATA})
104     public void testStoreDataNodeWithIntegrityException() {
105         cpsDataPersistenceService.storeDataNode(DATASPACE_NAME, ANCHOR_NAME1,
106             createDataNodeWithChildAndGrandChild(PARENT_XPATH, CHILD_XPATH, GRAND_CHILD_XPATH));
107     }
108
109     @Test
110     @Sql({CLEAR_DATA, SET_DATA})
111     public void testStoreDataNodeWithChildrenAndGrandChildren() {
112         cpsDataPersistenceService.storeDataNode(DATASPACE_NAME, ANCHOR_NAME1,
113             createDataNodeWithChildAndGrandChild(PARENT_XPATH_NEW, CHILD_XPATH_NEW, GRAND_CHILD_XPATH_NEW));
114
115         final FragmentEntity parentFragment = fragmentRepository.findById(PARENT_ID_NEW).orElseThrow();
116         final FragmentEntity childFragment = fragmentRepository.findById(CHILD_ID_NEW).orElseThrow();
117         final FragmentEntity grandChildFragment = fragmentRepository.findById(GRAND_CHILD_ID_NEW).orElseThrow();
118
119         assertFragment(parentFragment, childFragment, grandChildFragment, PARENT_XPATH_NEW, CHILD_XPATH_NEW,
120             GRAND_CHILD_XPATH_NEW);
121     }
122
123     private void assertFragment(final FragmentEntity parentFragment, final FragmentEntity childFragment,
124         final FragmentEntity grandChildFragment, final String parentXpath, final String childXpath,
125         final String grandChildXpath) {
126         assertEquals(parentXpath, parentFragment.getXpath());
127         assertEquals(DATASPACE_NAME, parentFragment.getDataspace().getName());
128         assertEquals(ANCHOR_NAME1, parentFragment.getAnchor().getName());
129
130         assertEquals(childXpath, childFragment.getXpath());
131         assertEquals(DATASPACE_NAME, childFragment.getDataspace().getName());
132         assertEquals(ANCHOR_NAME1, childFragment.getAnchor().getName());
133
134         assertEquals(grandChildXpath, grandChildFragment.getXpath());
135         assertEquals(DATASPACE_NAME, grandChildFragment.getDataspace().getName());
136         assertEquals(ANCHOR_NAME1, grandChildFragment.getAnchor().getName());
137     }
138
139     private DataNode createDataNodeWithChildAndGrandChild(final String parentXpath, final String childXpath,
140         final String grandChildXpath) {
141         final DataNode parentDataNode = DataNode.builder()
142             .xpath(parentXpath)
143             .build();
144
145         final DataNode childDataNode = DataNode.builder()
146             .xpath(childXpath)
147             .childDataNodes(Collections.emptySet())
148             .build();
149
150         final DataNode grandChildDataNode = DataNode.builder()
151             .xpath(grandChildXpath)
152             .childDataNodes(Collections.emptySet())
153             .build();
154
155         parentDataNode.setChildDataNodes(ImmutableSet.of(childDataNode));
156         childDataNode.setChildDataNodes(ImmutableSet.of(grandChildDataNode));
157         return parentDataNode;
158     }
159 }