aboutsummaryrefslogtreecommitdiffstats
path: root/models-dao/src/test/java/org/onap/policy/models/dao/EntityTest.java
blob: 8278cfe2047eefbfe990b744a9af65ac31ad14eb (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019 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.policy.models.dao;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.UUID;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onap.policy.models.base.PfConceptKey;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.base.PfReferenceKey;
import org.onap.policy.models.dao.DaoParameters;
import org.onap.policy.models.dao.PfDao;
import org.onap.policy.models.dao.PfDaoFactory;
import org.onap.policy.models.dao.impl.DefaultPfDao;

/**
 * JUnit test class.
 */
public class EntityTest {
    private Connection connection;
    private PfDao pfDao;

    @Before
    public void setup() throws Exception {
        connection = DriverManager.getConnection("jdbc:h2:mem:test");
    }

    @After
    public void teardown() throws Exception {
        connection.close();
        new File("derby.log").delete();
    }

    @Test
    public void testEntityTestSanity() throws PfModelException {
        final DaoParameters daoParameters = new DaoParameters();

        pfDao = new PfDaoFactory().createPfDao(daoParameters);

        try {
            pfDao.init(null);
            fail("Test should throw an exception here");
        } catch (final Exception e) {
            assertEquals("Policy Framework persistence unit parameter not set", e.getMessage());
        }

        try {
            pfDao.init(daoParameters);
            fail("Test should throw an exception here");
        } catch (final Exception e) {
            assertEquals("Policy Framework persistence unit parameter not set", e.getMessage());
        }

        daoParameters.setPluginClass("somewhere.over.the.rainbow");
        daoParameters.setPersistenceUnit("Dorothy");
        try {
            pfDao.init(daoParameters);
            fail("Test should throw an exception here");
        } catch (final Exception e) {
            assertEquals("Creation of Policy Framework persistence unit \"Dorothy\" failed", e.getMessage());
        }
        try {
            pfDao.create(new PfConceptKey());
            fail("Test should throw an exception here");
        } catch (final Exception e) {
            assertEquals("Policy Framework DAO has not been initialized", e.getMessage());
        }
        pfDao.close();
    }

    @Test
    public void testEntityTestAllOpsJpa() throws PfModelException {
        final DaoParameters daoParameters = new DaoParameters();
        daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
        daoParameters.setPersistenceUnit("DaoTest");

        pfDao = new PfDaoFactory().createPfDao(daoParameters);
        pfDao.init(daoParameters);

        testAllOps();
        pfDao.close();
    }

    @Test
    public void testEntityTestBadVals() throws PfModelException {
        final DaoParameters daoParameters = new DaoParameters();
        daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
        daoParameters.setPersistenceUnit("DaoTest");

        pfDao = new PfDaoFactory().createPfDao(daoParameters);
        pfDao.init(daoParameters);

        final PfConceptKey nullKey = null;
        final PfReferenceKey nullRefKey = null;
        final List<PfConceptKey> nullKeyList = null;
        final List<PfConceptKey> emptyKeyList = new ArrayList<>();
        final List<PfReferenceKey> nullRKeyList = null;
        final List<PfReferenceKey> emptyRKeyList = new ArrayList<>();

        pfDao.create(nullKey);
        pfDao.createCollection(nullKeyList);
        pfDao.createCollection(emptyKeyList);

        pfDao.delete(nullKey);
        pfDao.deleteCollection(nullKeyList);
        pfDao.deleteCollection(emptyKeyList);
        pfDao.delete(PfConceptKey.class, nullKey);
        pfDao.delete(PfReferenceKey.class, nullRefKey);
        pfDao.deleteByConceptKey(PfConceptKey.class, nullKeyList);
        pfDao.deleteByConceptKey(PfConceptKey.class, emptyKeyList);
        pfDao.deleteByReferenceKey(PfReferenceKey.class, nullRKeyList);
        pfDao.deleteByReferenceKey(PfReferenceKey.class, emptyRKeyList);

        pfDao.get(null, nullKey);
        pfDao.get(null, nullRefKey);
        pfDao.getAll(null);
        pfDao.getAll(null, nullKey);
        pfDao.getConcept(null, nullKey);
        pfDao.getConcept(PfConceptKey.class, nullKey);
        pfDao.getConcept(null, nullRefKey);
        pfDao.getConcept(PfReferenceKey.class, nullRefKey);
        pfDao.size(null);

        pfDao.close();
    }

    private void testAllOps() {
        final PfConceptKey aKey0 = new PfConceptKey("A-KEY0", "0.0.1");
        final PfConceptKey aKey1 = new PfConceptKey("A-KEY1", "0.0.1");
        final PfConceptKey aKey2 = new PfConceptKey("A-KEY2", "0.0.1");
        final DummyConceptEntity keyInfo0 = new DummyConceptEntity(aKey0,
                UUID.fromString("00000000-0000-0000-0000-000000000000"), "key description 0");
        final DummyConceptEntity keyInfo1 = new DummyConceptEntity(aKey1,
                UUID.fromString("00000000-0000-0000-0000-000000000001"), "key description 1");
        final DummyConceptEntity keyInfo2 = new DummyConceptEntity(aKey2,
                UUID.fromString("00000000-0000-0000-0000-000000000002"), "key description 2");

        pfDao.create(keyInfo0);

        final DummyConceptEntity keyInfoBack0 = pfDao.get(DummyConceptEntity.class, aKey0);
        assertTrue(keyInfo0.equals(keyInfoBack0));

        final DummyConceptEntity keyInfoBackNull = pfDao.get(DummyConceptEntity.class, PfConceptKey.getNullKey());
        assertNull(keyInfoBackNull);

        final DummyConceptEntity keyInfoBack1 = pfDao.getConcept(DummyConceptEntity.class, aKey0);
        assertTrue(keyInfoBack0.equals(keyInfoBack1));

        final DummyConceptEntity keyInfoBack2 =
                pfDao.getConcept(DummyConceptEntity.class, new PfConceptKey("A-KEY3", "0.0.1"));
        assertNull(keyInfoBack2);

        final Set<DummyConceptEntity> keyInfoSetIn = new TreeSet<DummyConceptEntity>();
        keyInfoSetIn.add(keyInfo1);
        keyInfoSetIn.add(keyInfo2);

        pfDao.createCollection(keyInfoSetIn);

        Set<DummyConceptEntity> keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));

        keyInfoSetIn.add(keyInfo0);
        assertTrue(keyInfoSetIn.equals(keyInfoSetOut));

        pfDao.delete(keyInfo1);
        keyInfoSetIn.remove(keyInfo1);
        keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
        assertTrue(keyInfoSetIn.equals(keyInfoSetOut));

        pfDao.deleteCollection(keyInfoSetIn);
        keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
        assertEquals(0, keyInfoSetOut.size());

        keyInfoSetIn.add(keyInfo0);
        keyInfoSetIn.add(keyInfo1);
        keyInfoSetIn.add(keyInfo0);
        pfDao.createCollection(keyInfoSetIn);
        keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
        assertTrue(keyInfoSetIn.equals(keyInfoSetOut));

        pfDao.delete(DummyConceptEntity.class, aKey0);
        keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
        assertEquals(2, keyInfoSetOut.size());
        assertEquals(2, pfDao.size(DummyConceptEntity.class));

        final Set<PfConceptKey> keySetIn = new TreeSet<PfConceptKey>();
        keySetIn.add(aKey1);
        keySetIn.add(aKey2);

        final int deletedCount = pfDao.deleteByConceptKey(DummyConceptEntity.class, keySetIn);
        assertEquals(2, deletedCount);

        keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
        assertEquals(0, keyInfoSetOut.size());

        keyInfoSetIn.add(keyInfo0);
        keyInfoSetIn.add(keyInfo1);
        keyInfoSetIn.add(keyInfo0);
        pfDao.createCollection(keyInfoSetIn);
        keyInfoSetOut = new TreeSet<DummyConceptEntity>(pfDao.getAll(DummyConceptEntity.class));
        assertTrue(keyInfoSetIn.equals(keyInfoSetOut));

        pfDao.deleteAll(DummyConceptEntity.class);
        assertEquals(0, pfDao.size(DummyConceptEntity.class));

        final PfConceptKey owner0Key = new PfConceptKey("Owner0", "0.0.1");
        final PfConceptKey owner1Key = new PfConceptKey("Owner1", "0.0.1");
        final PfConceptKey owner2Key = new PfConceptKey("Owner2", "0.0.1");
        final PfConceptKey owner3Key = new PfConceptKey("Owner3", "0.0.1");
        final PfConceptKey owner4Key = new PfConceptKey("Owner4", "0.0.1");
        final PfConceptKey owner5Key = new PfConceptKey("Owner5", "0.0.1");

        pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity0"), 100.0));
        pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity1"), 101.0));
        pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity2"), 102.0));
        pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity3"), 103.0));
        pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner0Key, "Entity4"), 104.0));
        pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner1Key, "Entity5"), 105.0));
        pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner1Key, "Entity6"), 106.0));
        pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner1Key, "Entity7"), 107.0));
        pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner2Key, "Entity8"), 108.0));
        pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner2Key, "Entity9"), 109.0));
        pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner3Key, "EntityA"), 110.0));
        pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner4Key, "EntityB"), 111.0));
        pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityC"), 112.0));
        pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityD"), 113.0));
        pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityE"), 114.0));
        pfDao.create(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityF"), 115.0));

        TreeSet<DummyReferenceEntity> testEntitySetOut =
                new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class));
        assertEquals(16, testEntitySetOut.size());

        testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner0Key));
        assertEquals(5, testEntitySetOut.size());

        testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner1Key));
        assertEquals(3, testEntitySetOut.size());

        testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner2Key));
        assertEquals(2, testEntitySetOut.size());

        testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner3Key));
        assertEquals(1, testEntitySetOut.size());

        testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner4Key));
        assertEquals(1, testEntitySetOut.size());

        testEntitySetOut = new TreeSet<DummyReferenceEntity>(pfDao.getAll(DummyReferenceEntity.class, owner5Key));
        assertEquals(4, testEntitySetOut.size());

        assertNotNull(pfDao.get(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity0")));
        assertNotNull(pfDao.getConcept(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity0")));
        assertNull(pfDao.get(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity1000")));
        assertNull(pfDao.getConcept(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity1000")));
        pfDao.delete(DummyReferenceEntity.class, new PfReferenceKey(owner0Key, "Entity0"));

        final Set<PfReferenceKey> rKeySetIn = new TreeSet<PfReferenceKey>();
        rKeySetIn.add(new PfReferenceKey(owner4Key, "EntityB"));
        rKeySetIn.add(new PfReferenceKey(owner5Key, "EntityD"));

        final int deletedRCount = pfDao.deleteByReferenceKey(DummyReferenceEntity.class, rKeySetIn);
        assertEquals(2, deletedRCount);

        pfDao.update(new DummyReferenceEntity(new PfReferenceKey(owner5Key, "EntityF"), 120.0));
    }
}