summaryrefslogtreecommitdiffstats
path: root/cps-ri/src/test/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceTest.java
blob: de0942c3c3a10f4ed8ffff6a95015633b8fab1ca (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
/*
 *  ============LICENSE_START=======================================================
 *  Copyright (C) 2021 Nordix Foundation
 *  ================================================================================
 *  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 junit.framework.TestCase.assertEquals;

import com.google.common.collect.ImmutableSet;
import java.util.Arrays;
import java.util.Collections;
import org.assertj.core.api.Assertions;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.onap.cps.DatabaseTestContainer;
import org.onap.cps.spi.CpsDataPersistenceService;
import org.onap.cps.spi.entities.FragmentEntity;
import org.onap.cps.spi.exceptions.AnchorNotFoundException;
import org.onap.cps.spi.exceptions.DataspaceNotFoundException;
import org.onap.cps.spi.exceptions.NotFoundInDataspaceException;
import org.onap.cps.spi.model.DataNode;
import org.onap.cps.spi.repository.FragmentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.junit4.SpringRunner;


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

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

    private static final String DATASPACE_NAME = "DATASPACE-001";
    private static final String ANCHOR_NAME1 = "ANCHOR-001";

    private static final long PARENT_ID_4001 = 4001;
    private static final long PARENT_ID_4002 = 4002;
    private static final long PARENT_ID_4003 = 4003;
    private static final String PARENT_XPATH1 = "/parent-1";
    private static final String PARENT_XPATH2 = "/parent-2";
    private static final String PARENT_XPATH3 = "/parent-3";

    private static final long CHILD_ID_4004 = 4004;
    private static final String CHILD_XPATH1 = "/parent-1/child-1";
    private static final String CHILD_XPATH2 = "/parent-2/child-2";

    private static final long GRAND_CHILD_ID_4006 = 4006;
    private static final String GRAND_CHILD_XPATH1 = "/parent-1/child-1/grandchild-1";


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

    @Autowired
    private CpsDataPersistenceService cpsDataPersistenceService;

    @Autowired
    private FragmentRepository fragmentRepository;

    @Test
    @Sql({CLEAR_DATA, SET_DATA})
    public void testGetFragmentsWithChildAndGrandChild() {
        final FragmentEntity parentFragment = fragmentRepository.findById(PARENT_ID_4001).orElseThrow();
        final FragmentEntity childFragment = fragmentRepository.findById(CHILD_ID_4004).orElseThrow();
        final FragmentEntity grandChildFragment = fragmentRepository.findById(GRAND_CHILD_ID_4006).orElseThrow();
        assertFragment(parentFragment, childFragment, grandChildFragment, PARENT_XPATH1, CHILD_XPATH1,
            GRAND_CHILD_XPATH1);
    }

    @Test(expected = DataspaceNotFoundException.class)
    @Sql({CLEAR_DATA, SET_DATA})
    public void testStoreDataNodeAtNonExistingDataspace() {
        cpsDataPersistenceService
            .storeDataNode("Non Existing Dataspace Name", ANCHOR_NAME1, new DataNode());
    }

    @Test(expected = AnchorNotFoundException.class)
    @Sql({CLEAR_DATA, SET_DATA})
    public void testStoreDataNodeAtNonExistingAnchor() {
        cpsDataPersistenceService
            .storeDataNode(DATASPACE_NAME, "Non Existing Anchor Name", new DataNode());
    }

    @Test(expected = DataIntegrityViolationException.class)
    @Sql({CLEAR_DATA, SET_DATA})
    public void testStoreDataNodeWithIntegrityException() {
        cpsDataPersistenceService.storeDataNode(DATASPACE_NAME, ANCHOR_NAME1,
            createDataNodeTree(PARENT_XPATH1));
    }

    @Test
    @Sql({CLEAR_DATA, SET_DATA})
    public void testStoreDataNodeWithChildAndGrandChild() {
        final String parentXpath = "/parent-new";
        final String childXpath = "/parent-new/child-new";
        final String grandChildXpath = "/parent-new/child-new/grandchild-new";

        cpsDataPersistenceService.storeDataNode(DATASPACE_NAME, ANCHOR_NAME1,
            createDataNodeTree(parentXpath, childXpath, grandChildXpath));
        final FragmentEntity parentFragment = getFragmentByXpath(parentXpath);
        final FragmentEntity childFragment = getFragmentByXpath(childXpath);
        final FragmentEntity grandChildFragment = getFragmentByXpath(grandChildXpath);
        assertFragment(parentFragment, childFragment, grandChildFragment, parentXpath, childXpath,
            grandChildXpath);
    }

    @Test
    @Sql({CLEAR_DATA, SET_DATA})
    public void testAddChildToFragmentThatHasOneChild() {
        final String childXpath = "some-xpath";
        final DataNode childDataNode = createDataNodeTree(childXpath);
        cpsDataPersistenceService
            .addChildDataNode(DATASPACE_NAME, ANCHOR_NAME1, PARENT_XPATH2, childDataNode);
        final FragmentEntity parentFragment = fragmentRepository.findById(PARENT_ID_4002).orElseThrow();
        Assertions.assertThat(parentFragment.getChildFragments())
            .hasSize(2)
            .extracting(FragmentEntity::getXpath)
            .containsExactlyInAnyOrder(childXpath, CHILD_XPATH2);
    }

    @Test
    @Sql({CLEAR_DATA, SET_DATA})
    public void testAddChildToFragmentThatHasNoChild() {
        final String childXpath = "some-xpath";
        final DataNode childDataNode = createDataNodeTree(childXpath);
        cpsDataPersistenceService
            .addChildDataNode(DATASPACE_NAME, ANCHOR_NAME1, PARENT_XPATH3, childDataNode);
        final FragmentEntity parentFragment = fragmentRepository.findById(PARENT_ID_4003).orElseThrow();
        Assertions.assertThat(parentFragment.getChildFragments())
            .hasSize(1)
            .extracting(FragmentEntity::getXpath)
            .containsExactlyInAnyOrder(childXpath);
    }

    @Test(expected = DataIntegrityViolationException.class)
    @Sql({CLEAR_DATA, SET_DATA})
    public void testAddAChildWithTheSameXpathAsExistingChild() {
        cpsDataPersistenceService
            .addChildDataNode(DATASPACE_NAME, ANCHOR_NAME1, PARENT_XPATH1, createDataNodeTree(CHILD_XPATH1));
    }

    @Test(expected = NotFoundInDataspaceException.class)
    @Sql({CLEAR_DATA, SET_DATA})
    public void testAddAChildWithToAParentThatDoesNotExist() {
        cpsDataPersistenceService
            .addChildDataNode(DATASPACE_NAME, ANCHOR_NAME1, "non-existing-xpath", createDataNodeTree("some-xpath"));
    }

    private void assertFragment(final FragmentEntity parentFragment, final FragmentEntity childFragment,
        final FragmentEntity grandChildFragment, final String parentXpath, final String childXpath,
        final String grandChildXpath) {
        assertEquals(parentXpath, parentFragment.getXpath());
        assertEquals(DATASPACE_NAME, parentFragment.getDataspace().getName());
        assertEquals(ANCHOR_NAME1, parentFragment.getAnchor().getName());

        assertEquals(childXpath, childFragment.getXpath());
        assertEquals(DATASPACE_NAME, childFragment.getDataspace().getName());
        assertEquals(ANCHOR_NAME1, childFragment.getAnchor().getName());

        assertEquals(grandChildXpath, grandChildFragment.getXpath());
        assertEquals(DATASPACE_NAME, grandChildFragment.getDataspace().getName());
        assertEquals(ANCHOR_NAME1, grandChildFragment.getAnchor().getName());
    }

    private FragmentEntity getFragmentByXpath(final String xpath) {
        return fragmentRepository.findAll().stream()
            .filter(fragment -> fragment.getXpath().contains(xpath)).findAny().orElseThrow();
    }

    private static DataNode createDataNodeTree(final String... xpaths) {
        final DataNode dataNode = DataNode.builder().xpath(xpaths[0]).childDataNodes(Collections.emptySet()).build();
        if (xpaths.length > 1) {
            final String[] xPathsDescendant = Arrays.copyOfRange(xpaths, 1, xpaths.length);
            final DataNode childDataNode = createDataNodeTree(xPathsDescendant);
            dataNode.setChildDataNodes(ImmutableSet.of(childDataNode));
        }
        return dataNode;
    }
}