aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/aai/modelloader/entity/model/ModelSorterTest.java
blob: 86985d8631f5288474fa3835ac2f05bcbf18a24d (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
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017-2018 European Software Marketing Ltd.
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */
package org.onap.aai.modelloader.entity.model;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.isEmptyString;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.Test;
import org.onap.aai.modelloader.entity.Artifact;

public class ModelSorterTest {

    @Test
    public void edgeEquality() throws BabelArtifactParsingException {
        ModelArtifact model = buildTestModel();
        ModelSorter.Node nodeA = new ModelSorter.Node(model);
        ModelSorter.Node nodeB = new ModelSorter.Node(model);

        ModelSorter.Edge edgeA = new ModelSorter.Edge(nodeA, nodeB);

        assertThat(edgeA, is(equalTo(edgeA)));
        assertThat(edgeA, is(not(equalTo(null))));
        assertThat(edgeA, is(not(equalTo(model))));

        ModelSorter.Edge edgeB = new ModelSorter.Edge(nodeA, nodeB);
        ModelSorter.Edge edgeC = new ModelSorter.Edge(nodeB, nodeA);

        ModelSorter.Node nodeC = new ModelSorter.Node(model);
        ModelSorter.Edge edgeD = new ModelSorter.Edge(nodeA, nodeC);
        assertThat(edgeA, is(equalTo(edgeB)));
        assertThat(edgeA, is(not(equalTo(edgeC))));
        assertThat(edgeA, is(not(equalTo(edgeD))));
    }

    @Test
    public void nodeEquality() throws BabelArtifactParsingException {
        ModelArtifact model = buildTestModel();
        ModelSorter.Node nodeA = new ModelSorter.Node(model);

        assertThat(nodeA, is(equalTo(nodeA)));
        assertThat(nodeA, is(not(equalTo(null))));
        assertThat(nodeA, is(not(equalTo(model))));

        ModelSorter.Node nodeB = new ModelSorter.Node(model);
        assertThat(nodeA, is(equalTo(nodeB)));
        assertThat(nodeA.toString(), is(equalTo(nodeB.toString())));
        assertThat(nodeA, is(not(equalTo(new ModelSorter.Node(new ModelArtifact())))));
    }

    @Test
    public void testToString() throws BabelArtifactParsingException {
        ModelArtifact model = buildTestModel();

        ModelSorter.Node nodeA = new ModelSorter.Node(model);
        assertThat(nodeA.toString(), not(isEmptyString()));

        ModelSorter.Node nodeB = new ModelSorter.Node(model);
        nodeA.addEdge(nodeB);
        assertThat(nodeA.toString(), not(isEmptyString()));
        assertThat(nodeB.toString(), not(isEmptyString()));

    }

    @Test
    public void noModels() throws BabelArtifactParsingException {
        assertThat(new ModelSorter().sort(null), is(nullValue()));
        assertThat(new ModelSorter().sort(Collections.emptyList()).size(), is(0));
    }

    @Test
    public void singleModel() throws BabelArtifactParsingException {
        assertThat(new ModelSorter().sort(Arrays.asList(buildTestModel())).size(), is(1));
    }

    @Test
    public void multipleModels() throws BabelArtifactParsingException {
        Artifact artA = buildTestModel("aaaa", "mvaaaa", "cccc|mvcccc");
        Artifact artB = buildTestModel("bbbb", "mvbbbb", "aaaa|mvaaaa");
        Artifact artC = buildTestModel("cccc", "mvcccc");
        List<Artifact> expected = Arrays.asList(artC, artA, artB);
        assertThat(new ModelSorter().sort(Arrays.asList(artA, artB, artC)), is(expected));
    }


    @Test
    public void multipleModelsWithMultipleIncomingEdges() throws BabelArtifactParsingException {
        ModelArtifact artA = buildTestModel("aaaa", "mvaaaa", "cccc|mvcccc");
        Artifact artB = buildTestModel("bbbb", "mvbbbb", "aaaa|mvaaaa");
        Artifact artC = buildTestModel("cccc", "mvcccc");
        Artifact artD = buildTestModel("dddd", "mvdddd", "cccc|mvcccc");
        artA.addDependentModelId("dddd|mvdddd");
        List<Artifact> expected = Arrays.asList(artC, artD, artA, artB);
        assertThat(new ModelSorter().sort(Arrays.asList(artA, artB, artC, artD)), is(expected));
    }

    @Test
    public void multipleModelsAndNamedQueries() throws BabelArtifactParsingException {
        Artifact artifact = buildTestModel("aaaa", "1111", "cccc|2222");
        Artifact nq1 = buildTestNamedQuery("nq1", "aaaa|1111");
        Artifact nq2 = buildTestNamedQuery("nqw", "existing-model");
        List<Artifact> expected = Arrays.asList(artifact, nq2, nq1);
        assertThat(new ModelSorter().sort(Arrays.asList(nq1, nq2, artifact)), is(expected));
    }

    @Test(expected = BabelArtifactParsingException.class)
    public void circularDependency() throws BabelArtifactParsingException {
        List<Artifact> modelList = new ArrayList<Artifact>();
        modelList.add(buildTestModel("aaaa", "1111", "bbbb|1111"));
        modelList.add(buildTestModel("bbbb", "1111", "aaaa|1111"));
        new ModelSorter().sort(modelList);
    }

    private ModelArtifact buildTestModel() {
        return buildTestModel("aaa", "111", "xyz|123");
    }

    private ModelArtifact buildTestModel(String id, String version) {
        return buildTestModel(id, version, null);
    }

    private ModelArtifact buildTestModel(String id, String version, String dependentModel) {
        ModelArtifact modelArtifact = new ModelArtifact();
        modelArtifact.setModelInvariantId(id);
        modelArtifact.setModelVerId(version);
        if (dependentModel != null) {
            modelArtifact.addDependentModelId(dependentModel);
        }
        return modelArtifact;
    }

    private NamedQueryArtifact buildTestNamedQuery(String uuid, String modelId) {
        NamedQueryArtifact nq = new NamedQueryArtifact();
        nq.setNamedQueryUuid(uuid);
        nq.addDependentModelId(modelId);
        return nq;
    }

}