2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.versioning.impl;
24 import org.mockito.ArgumentCaptor;
25 import org.mockito.InjectMocks;
26 import org.mockito.Mock;
27 import org.mockito.MockitoAnnotations;
28 import org.openecomp.sdc.common.errors.CoreException;
29 import org.openecomp.sdc.versioning.ItemManager;
30 import org.openecomp.sdc.versioning.VersionCalculator;
31 import org.openecomp.sdc.versioning.dao.VersionDao;
32 import org.openecomp.sdc.versioning.dao.types.Revision;
33 import org.openecomp.sdc.versioning.dao.types.SynchronizationState;
34 import org.openecomp.sdc.versioning.dao.types.Version;
35 import org.openecomp.sdc.versioning.dao.types.VersionState;
36 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
37 import org.openecomp.sdc.versioning.types.VersionCreationMethod;
38 import org.testng.Assert;
39 import org.testng.annotations.BeforeMethod;
40 import org.testng.annotations.Test;
42 import java.util.ArrayList;
43 import java.util.List;
44 import java.util.Optional;
45 import java.util.stream.Collectors;
46 import java.util.stream.Stream;
48 import static org.mockito.Matchers.any;
49 import static org.mockito.Matchers.eq;
50 import static org.mockito.Mockito.doReturn;
51 import static org.mockito.Mockito.doThrow;
52 import static org.mockito.Mockito.never;
53 import static org.mockito.Mockito.times;
54 import static org.mockito.Mockito.verify;
55 import static org.openecomp.sdc.versioning.dao.types.SynchronizationState.OutOfSync;
56 import static org.openecomp.sdc.versioning.dao.types.SynchronizationState.UpToDate;
57 import static org.openecomp.sdc.versioning.dao.types.VersionStatus.Certified;
58 import static org.openecomp.sdc.versioning.dao.types.VersionStatus.Draft;
60 public class VersioningManagerImplTest {
61 private static final String ITEM_ID = "itemId";
62 private static final String VERSION_ID = "versionId";
65 private VersionDao versionDaoMock;
67 private VersionCalculator versionCalculatorMock;
69 private ItemManager itemManagerMock;
71 private VersioningManagerImpl versioningManager;
74 public void setUp() throws Exception {
75 MockitoAnnotations.initMocks(this);
79 public void testListWhenNone() throws Exception {
80 doReturn(new ArrayList<>()).when(versionDaoMock).list(ITEM_ID);
82 List<Version> versions = versioningManager.list(ITEM_ID);
84 Assert.assertTrue(versions.isEmpty());
88 public void testList() throws Exception {
89 List<Version> returnedVersions = Stream.of(createVersion("1", null, null, false),
90 createVersion("2", null, null, false),
91 createVersion("3", null, null, false)).collect(Collectors.toList());
92 doReturn(returnedVersions).when(versionDaoMock).list(ITEM_ID);
94 List<Version> versions = versioningManager.list(ITEM_ID);
95 Assert.assertEquals(versions, returnedVersions);
98 @Test(expectedExceptions = Exception.class)
99 public void testGetNonExisting() throws Exception {
100 Version version = new Version(VERSION_ID);
102 doReturn(Optional.empty()).when(versionDaoMock).get(ITEM_ID, version);
103 doThrow(new Exception()).when(versionDaoMock).sync(ITEM_ID, version);
105 versioningManager.get(ITEM_ID, version);
109 public void testGetNonExistingForUser() throws Exception {
110 Version requestedVersion = new Version(VERSION_ID);
112 Version returnedVersion = createVersion(VERSION_ID, Draft, UpToDate, false);
113 doReturn(Optional.empty()).doReturn(Optional.of(returnedVersion))
114 .when(versionDaoMock).get(ITEM_ID, requestedVersion);
116 Version version = versioningManager.get(ITEM_ID, requestedVersion);
117 Assert.assertEquals(version, returnedVersion);
119 verify(versionDaoMock, times(2)).get(ITEM_ID, requestedVersion);
120 verify(versionDaoMock).sync(ITEM_ID, requestedVersion);
124 public void testGetOutOfSyncCertified() throws Exception {
125 Version requestedVersion = new Version(VERSION_ID);
127 Version returnedVersion = createVersion(VERSION_ID, Certified, UpToDate, false);
128 doReturn(Optional.of(createVersion(VERSION_ID, Certified, OutOfSync, false)))
129 .doReturn(Optional.of(returnedVersion))
130 .when(versionDaoMock).get(ITEM_ID, requestedVersion);
132 Version version = versioningManager.get(ITEM_ID, requestedVersion);
133 Assert.assertEquals(version, returnedVersion);
135 verify(versionDaoMock, times(2)).get(ITEM_ID, requestedVersion);
136 verify(versionDaoMock).forceSync(ITEM_ID, requestedVersion);
140 public void testGet() throws Exception {
141 Version requestedVersion = new Version(VERSION_ID);
143 Version returnedVersion = createVersion(VERSION_ID, Draft, OutOfSync, true);
144 doReturn(Optional.of(returnedVersion)).when(versionDaoMock).get(ITEM_ID, requestedVersion);
146 Version version = versioningManager.get(ITEM_ID, requestedVersion);
147 Assert.assertEquals(version, returnedVersion);
149 verify(versionDaoMock).get(ITEM_ID, requestedVersion);
150 verify(versionDaoMock, never()).sync(any(), any());
151 verify(versionDaoMock, never()).forceSync(any(), any());
155 public void testCreate() throws Exception {
156 Version requestedVersion = new Version();
158 String versionName = "versionName";
159 doReturn(versionName).when(versionCalculatorMock).calculate(null, VersionCreationMethod.major);
161 doReturn(Stream.of(createVersion("1", null, null, false),
162 createVersion("2", null, null, false),
163 createVersion("3", null, null, false)).collect(Collectors.toList()))
164 .when(versionDaoMock).list(ITEM_ID);
167 versioningManager.create(ITEM_ID, requestedVersion, VersionCreationMethod.major);
168 Assert.assertNotNull(version);
169 Assert.assertEquals(version.getName(), versionName);
171 verify(versionDaoMock).create(ITEM_ID, requestedVersion);
172 verify(itemManagerMock).updateVersionStatus(ITEM_ID, Draft, null);
173 verify(versionDaoMock)
174 .publish(eq(ITEM_ID), eq(requestedVersion), eq("Create version: versionName"));
178 public void testCreateBasedOn() throws Exception {
179 Version requestedVersion = new Version();
180 requestedVersion.setBaseId("baseVersionId");
182 Version baseVersion = createVersion(requestedVersion.getBaseId(), Certified, UpToDate, false);
183 // TODO: 12/13/2017 fix to eq(new Version("baseVersionId")) when version.equals will be fixed
184 doReturn(Optional.of(baseVersion)).when(versionDaoMock).get(eq(ITEM_ID), any(Version.class));
186 String versionName = "4.0";
187 doReturn(versionName)
188 .when(versionCalculatorMock).calculate(baseVersion.getName(), VersionCreationMethod.major);
190 doReturn(Stream.of(createVersion("1", null, null, false),
191 createVersion("2", null, null, false),
192 createVersion("3", null, null, false)).collect(Collectors.toList()))
193 .when(versionDaoMock).list(ITEM_ID);
196 versioningManager.create(ITEM_ID, requestedVersion, VersionCreationMethod.major);
197 Assert.assertNotNull(version);
198 Assert.assertEquals(version.getName(), versionName);
200 verify(versionDaoMock).create(ITEM_ID, requestedVersion);
201 verify(itemManagerMock).updateVersionStatus(ITEM_ID, Draft, null);
202 verify(versionDaoMock).publish(eq(ITEM_ID), eq(requestedVersion), eq("Create version: 4.0"));
205 @Test(expectedExceptions = CoreException.class, expectedExceptionsMessageRegExp =
206 "Item itemId: create version failed, a version with the name 2.0 already exist")
207 public void testCreateWithExistingName() throws Exception {
208 Version version = new Version();
209 version.setBaseId("baseVersionId");
211 Version baseVersion = createVersion(version.getBaseId(), Certified, UpToDate, false);
212 // TODO: 12/13/2017 fix to eq(new Version("baseVersionId")) when version.equals will be fixed
213 doReturn(Optional.of(baseVersion)).when(versionDaoMock).get(eq(ITEM_ID), any(Version.class));
215 String versionName = "2.0";
216 doReturn(versionName)
217 .when(versionCalculatorMock).calculate(baseVersion.getName(), VersionCreationMethod.major);
219 doReturn(Stream.of(createVersion("1", null, null, false),
220 createVersion("2", null, null, false),
221 createVersion("3", null, null, false)).collect(Collectors.toList()))
222 .when(versionDaoMock).list(ITEM_ID);
224 versioningManager.create(ITEM_ID, version, VersionCreationMethod.major);
227 @Test(expectedExceptions = CoreException.class, expectedExceptionsMessageRegExp =
228 "Item itemId: submit version failed, version versionId is already Certified")
229 public void testSubmitCertified() throws Exception {
230 Version version = new Version(VERSION_ID);
232 Version returnedVersion = createVersion(VERSION_ID, Certified, UpToDate, false);
233 doReturn(Optional.of(returnedVersion)).when(versionDaoMock).get(ITEM_ID, version);
235 versioningManager.submit(ITEM_ID, version, "Submit message");
239 public void testSubmit() throws Exception {
240 Version version = new Version(VERSION_ID);
242 ArgumentCaptor<Version> versionArgumentCaptor = ArgumentCaptor.forClass(Version.class);
244 Version returnedVersion = createVersion(VERSION_ID, Draft, UpToDate, false);
245 doReturn(Optional.of(returnedVersion)).when(versionDaoMock).get(ITEM_ID, version);
247 String submitDescription = "Submit message";
248 versioningManager.submit(ITEM_ID, version, submitDescription);
250 verify(versionDaoMock).update(eq(ITEM_ID), versionArgumentCaptor.capture());
251 Assert.assertEquals(Certified, versionArgumentCaptor.getValue().getStatus());
252 verify(versionDaoMock).publish(ITEM_ID, version, submitDescription);
253 verify(itemManagerMock).updateVersionStatus(ITEM_ID, Certified, Draft);
257 public void testPublish() throws Exception {
258 Version version = new Version(VERSION_ID);
259 String publishDescription = "Publish message";
261 versioningManager.publish(ITEM_ID, version, publishDescription);
263 verify(versionDaoMock).publish(ITEM_ID, version, publishDescription);
267 public void testSync() throws Exception {
268 Version version = new Version(VERSION_ID);
270 versioningManager.sync(ITEM_ID, version);
272 verify(versionDaoMock).sync(ITEM_ID, version);
276 public void testForceSync() throws Exception {
277 Version version = new Version(VERSION_ID);
279 versioningManager.forceSync(ITEM_ID, version);
281 verify(versionDaoMock).forceSync(ITEM_ID, version);
285 public void testRevert() throws Exception {
286 Version version = new Version(VERSION_ID);
287 String revisionId = "revisionId";
289 versioningManager.revert(ITEM_ID, version, revisionId);
291 verify(versionDaoMock).revert(ITEM_ID, version, revisionId);
295 public void testListRevisions() throws Exception {
296 Version version = new Version(VERSION_ID);
298 List<Revision> returnedRevisions =
299 Stream.of(new Revision(), new Revision()).collect(Collectors.toList());
300 doReturn(returnedRevisions)
301 .when(versionDaoMock).listRevisions(ITEM_ID, version);
303 List<Revision> revisions = versioningManager.listRevisions(ITEM_ID, version);
304 Assert.assertEquals(revisions, returnedRevisions);
307 private Version createVersion(String id, VersionStatus status,
308 SynchronizationState syncState, boolean dirty) {
309 Version version = new Version(id);
310 version.setName(id + ".0");
311 version.setDescription(id + " desc");
312 version.setStatus(status);
314 VersionState state = new VersionState();
315 state.setSynchronizationState(syncState);
316 state.setDirty(dirty);
317 version.setState(state);
322 private static final String USR1 = "usr1";
323 private static final String USR2 = "usr2";
324 private static final String TYPE1 = "Type1";
326 /* private static final String TYPE2 = "Type2";*//*
328 private static final String ID1 = "Id1";
330 /* private static final String ID2 = "Id2";
331 private static final String ID3 = "Id3";
332 private static final String TYPE1_TABLE_NAME = "vendor_license_model";
333 private static final String TYPE1_ID_NAME = "vlm_id";
334 private static final String TYPE1_VERSION_NAME = "version";
335 private static final String TYPE2_TABLE_NAME = "feature_group";
336 private static final String TYPE2_ID_NAME = "vlm_id";
337 private static final String TYPE2_VERSION_NAME = "version";*//*
339 private static final Version VERSION0 = new Version(0, 0);
340 private static final Version VERSION01 = new Version(0, 1);
341 private static final Version VERSION02 = new Version(0, 2);
342 private static final Version VERSION10 = new Version(1, 0);
343 private static final Version VERSION11 = new Version(1, 1);
346 /* private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
348 private static UDTMapper<Version> versionMapper =
349 noSqlDb.getMappingManager().udtMapper(Version.class);*//*
352 private VersionInfoDao versionInfoDaoMock;
354 private VersionInfoDeletedDao versionInfoDeletedDaoMock;
356 private VersioningManagerImpl versioningManager;
359 private ArgumentCaptor<VersionInfoEntity> versionInfoEntityArg;
362 public void setUp() throws Exception {
363 MockitoAnnotations.initMocks(this);
368 private void init() {
369 versionInfoDaoMock.delete(new VersionInfoEntity(TYPE1, ID1));
370 versionInfoDaoMock.delete(new VersionInfoEntity(TYPE1, ID2));
371 versionInfoDaoMock.delete(new VersionInfoEntity(TYPE2, ID3));
372 String deleteFromType1 = String
373 .format("delete from %s where %s=? and %s=?", TYPE1_TABLE_NAME, TYPE1_ID_NAME,
375 noSqlDb.execute(deleteFromType1, ID1, versionMapper.toUDT(VERSION01));
376 noSqlDb.execute(deleteFromType1, ID1, versionMapper.toUDT(VERSION02));
377 noSqlDb.execute(deleteFromType1, ID1, versionMapper.toUDT(VERSION11));
379 versioningManager.register(TYPE1,
380 new VersionableEntityMetadata(TYPE1_TABLE_NAME, TYPE1_ID_NAME, TYPE1_VERSION_NAME));
381 versioningManager.register(TYPE2,
382 new VersionableEntityMetadata(TYPE2_TABLE_NAME, TYPE2_ID_NAME, TYPE2_VERSION_NAME));
388 public void testRegister() throws Exception {
389 VersionableEntityMetadata entityMetadata =
390 new VersionableEntityMetadata(TYPE1_TABLE_NAME, TYPE1_ID_NAME, TYPE1_VERSION_NAME);
391 versioningManager.register(TYPE1, entityMetadata);
393 Map<String, Set<VersionableEntityMetadata>> versionableEntities =
394 versionableEntitiesCapture.capture();
395 Set<VersionableEntityMetadata> type1Entities = versionableEntities.get(TYPE1);
396 Assert.assertNotNull(type1Entities);
397 Assert.assertTrue(type1Entities.contains(entityMetadata));
401 @Test(expectedExceptions = CoreException.class)
402 public void testCreateAlreadyExisting() {
403 doReturn(new VersionInfoEntity()).when(versionInfoDaoMock).get(anyObject());
404 versioningManager.create(TYPE1, ID1, USR1);
408 public void testCreate() {
409 Version version = versioningManager.create(TYPE1, ID1, USR1);
410 Assert.assertEquals(version, VERSION01);
413 /* createVersionableEntityRecord(TYPE1_TABLE_NAME, TYPE1_ID_NAME, TYPE1_VERSION_NAME, ID1,
416 verify(versionInfoDaoMock).create(versionInfoEntityArg.capture());
417 VersionInfoEntity versionInfoEntity = versionInfoEntityArg.getValue();
418 assretVersionInfoEntity(versionInfoEntity, TYPE1, ID1, new Version(0, 0), VERSION01, USR1,
419 VersionStatus.Locked, new HashSet<>(), null);
422 @Test(expectedExceptions = CoreException.class)
423 public void testDeleteNonExisting() {
424 versioningManager.delete(TYPE1, ID1, USR1);
427 @Test(expectedExceptions = CoreException.class)
428 public void testDeleteLocked() {
429 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION0,
430 new UserCandidateVersion(USR1, VERSION01), Collections.emptySet(), null);
431 versioningManager.delete(TYPE1, ID1, USR1);
435 public void testDelete() {
436 VersionInfoEntity versionInfoEntity = new VersionInfoEntity();
437 versionInfoEntity.setStatus(VersionStatus.Draft);
438 doReturn(versionInfoEntity).when(versionInfoDaoMock).get(anyObject());
440 versioningManager.delete(TYPE1, ID1, USR1);
442 verify(versionInfoDaoMock).delete(versionInfoEntity);
443 ArgumentCaptor<VersionInfoDeletedEntity> versionInfoDeletedEntityArg =
444 ArgumentCaptor.forClass(VersionInfoDeletedEntity.class);
445 verify(versionInfoDeletedDaoMock).create(versionInfoDeletedEntityArg.capture());
448 @Test(expectedExceptions = CoreException.class)
449 public void testUndoDeleteNonExisting() {
450 versioningManager.undoDelete(TYPE1, ID1, USR1);
454 public void testUndoDelete() {
455 VersionInfoDeletedEntity versionInfoDeletedEntity = new VersionInfoDeletedEntity();
456 versionInfoDeletedEntity.setStatus(VersionStatus.Draft);
457 doReturn(versionInfoDeletedEntity).when(versionInfoDeletedDaoMock).get(anyObject());
459 versioningManager.undoDelete(TYPE1, ID1, USR1);
461 verify(versionInfoDeletedDaoMock).delete(versionInfoDeletedEntity);
462 verify(versionInfoDaoMock).create(versionInfoEntityArg.capture());
465 VersionInfoDeletedEntity versionInfoDeletedEntity =
466 versionInfoDeletedDaoMock.get(new VersionInfoDeletedEntity(TYPE1, ID1));
467 Assert.assertNotNull(versionInfoDeletedEntity);
469 Map<String, VersionInfo> entitiesInfoMap =
470 versioningManager.listDeletedEntitiesVersionInfo(TYPE1, USR2, null);
471 Assert.assertEquals(entitiesInfoMap.size(), 1);
472 VersionInfoEntity versionInfoEntity = versionInfoDaoMock.get(new VersionInfoEntity(TYPE1, ID1));
473 Assert.assertNull(versionInfoEntity);
474 versioningManager.undoDelete(TYPE1, ID1, USR1);
475 versionInfoEntity = versionInfoDaoMock.get(new VersionInfoEntity(TYPE1, ID1));
476 Assert.assertNotNull(versionInfoEntity);*//*
480 @Test(expectedExceptions = CoreException.class)
481 public void testCheckoutNonExisting() {
482 versioningManager.checkout(TYPE1, ID1, USR2);
485 @Test(expectedExceptions = CoreException.class)
486 public void testCheckoutOnLockedSameUser() {
487 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION0,
488 new UserCandidateVersion(USR1, VERSION01), Collections.emptySet(), null);
489 versioningManager.checkout(TYPE1, ID1, USR1);
492 @Test(expectedExceptions = CoreException.class)
493 public void testCheckoutOnLockedOtherUser() {
494 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION0,
495 new UserCandidateVersion(USR2, VERSION01), Collections.emptySet(), null);
496 versioningManager.checkout(TYPE1, ID1, USR1);
500 public void testCheckoutOnFinalized() {
501 Set<Version> viewableVersions = new HashSet<>();
502 viewableVersions.add(VERSION10);
503 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Certified, VERSION10, null, viewableVersions,
506 Version version = versioningManager.checkout(TYPE1, ID1, USR1);
507 Assert.assertEquals(version, VERSION11);
509 VersionInfoEntity versionInfoEntity = versionInfoDaoMock.get(new VersionInfoEntity(TYPE1, ID1));
510 assretVersionInfoEntity(versionInfoEntity, TYPE1, ID1, VERSION10, VERSION11, USR1,
511 VersionStatus.Locked, viewableVersions, VERSION10);
515 loadVersionableEntityRecord(TYPE1_TABLE_NAME, TYPE1_ID_NAME, TYPE1_VERSION_NAME, ID1,
517 Assert.assertTrue(results.iterator().hasNext());*//*
522 public void testCheckout() {
523 Set<Version> viewableVersions = new HashSet<>();
524 viewableVersions.add(VERSION01);
525 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Draft, VERSION01, null, viewableVersions,
528 Version version = versioningManager.checkout(TYPE1, ID1, USR1);
529 Assert.assertEquals(version, VERSION02);
531 verify(versionInfoDaoMock).update(versionInfoEntityArg.capture());
532 VersionInfoEntity versionInfoEntity = versionInfoEntityArg.getValue();
534 assretVersionInfoEntity(versionInfoEntity, TYPE1, ID1, VERSION01, VERSION02, USR1,
535 VersionStatus.Locked, viewableVersions, null);
538 /* ResultSet results =
539 loadVersionableEntityRecord(TYPE1_TABLE_NAME, TYPE1_ID_NAME, TYPE1_VERSION_NAME, ID1,
541 Assert.assertTrue(results.iterator().hasNext());*//*
545 @Test(expectedExceptions = CoreException.class)
546 public void testUndoCheckoutNonExisting() {
547 versioningManager.undoCheckout(TYPE1, ID1, USR1);
550 @Test(expectedExceptions = CoreException.class)
551 public void testUndoCheckoutOnAvailable() {
552 Set<Version> viewableVersions = new HashSet<>();
553 viewableVersions.add(VERSION01);
554 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Draft, VERSION01, null, viewableVersions,
557 versioningManager.undoCheckout(TYPE1, ID1, USR1);
560 @Test(expectedExceptions = CoreException.class)
561 public void testUndoCheckouOnFinalized() {
562 Set<Version> viewableVersions = new HashSet<>();
563 viewableVersions.add(VERSION10);
564 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Certified, VERSION10, null, viewableVersions,
566 versioningManager.undoCheckout(TYPE1, ID1, USR2);
569 @Test(expectedExceptions = CoreException.class)
570 public void testUndoCheckoutOnLockedOtherUser() {
571 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION0,
572 new UserCandidateVersion(USR2, VERSION01), Collections.emptySet(), null);
574 versioningManager.undoCheckout(TYPE1, ID1, USR1);
578 public void testUndoCheckout() {
579 HashSet<Version> viewableVersions = new HashSet<>();
580 viewableVersions.add(VERSION01);
581 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION01,
582 new UserCandidateVersion(USR1, VERSION02), viewableVersions, null);
584 Version version = versioningManager.undoCheckout(TYPE1, ID1, USR1);
585 Assert.assertEquals(version, VERSION01);
587 VersionInfoEntity versionInfoEntity = versionInfoDaoMock.get(new VersionInfoEntity(TYPE1, ID1));
588 assretVersionInfoEntity(versionInfoEntity, TYPE1, ID1, VERSION01, null, null,
589 VersionStatus.Draft, viewableVersions, null);
592 /* ResultSet results =
593 loadVersionableEntityRecord(TYPE1_TABLE_NAME, TYPE1_ID_NAME, TYPE1_VERSION_NAME, ID1,
595 Assert.assertFalse(results.iterator().hasNext());*//*
599 @Test(expectedExceptions = CoreException.class)
600 public void testCheckinNonExisting() {
601 versioningManager.checkin(TYPE1, ID1, USR1, "");
604 @Test(expectedExceptions = CoreException.class)
605 public void testCheckinOnAvailable() {
606 Set<Version> viewableVersions = new HashSet<>();
607 viewableVersions.add(VERSION01);
608 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Draft, VERSION01, null, viewableVersions,
611 versioningManager.checkin(TYPE1, ID1, USR1, "fail checkin");
615 @Test(expectedExceptions = CoreException.class)
616 public void testCheckinOnFinalized() {
617 Set<Version> viewableVersions = new HashSet<>();
618 viewableVersions.add(VERSION10);
619 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Certified, VERSION10, null, viewableVersions,
622 versioningManager.checkin(TYPE1, ID1, USR1, "failed checkin");
625 @Test(expectedExceptions = CoreException.class)
626 public void testCheckinOnLockedOtherUser() {
627 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION0,
628 new UserCandidateVersion(USR2, VERSION01), Collections.emptySet(), null);
630 versioningManager.checkin(TYPE1, ID1, USR1, "");
634 public void testCheckin() {
635 HashSet<Version> viewableVersions = new HashSet<>();
636 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION0,
637 new UserCandidateVersion(USR1, VERSION01), viewableVersions, null);
639 Version version = versioningManager.checkin(TYPE1, ID1, USR1, "checkin 0.1");
640 Assert.assertEquals(version, VERSION01);
642 verify(versionInfoDaoMock).update(versionInfoEntityArg.capture());
643 VersionInfoEntity versionInfoEntity = versionInfoEntityArg.getValue();
645 viewableVersions.add(VERSION01);
646 assretVersionInfoEntity(versionInfoEntity, TYPE1, ID1, VERSION01, null, null,
647 VersionStatus.Draft, viewableVersions, null);
650 @Test(expectedExceptions = CoreException.class)
651 public void testSubmitNonExisting() {
652 versioningManager.submit(TYPE1, ID1, USR2, "failed submit");
655 @Test(expectedExceptions = CoreException.class)
656 public void testSubmitOnLocked() {
657 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION0,
658 new UserCandidateVersion(USR1, VERSION01), Collections.emptySet(), null);
659 versioningManager.submit(TYPE1, ID1, USR2, "failed submit");
663 @Test(expectedExceptions = CoreException.class)
664 public void testSubmitOnFinalized() {
665 Set<Version> viewableVersions = new HashSet<>();
666 viewableVersions.add(VERSION10);
667 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Certified, VERSION10, null, viewableVersions,
669 versioningManager.submit(TYPE1, ID1, USR2, "failed submit");
673 public void testSubmit() {
674 Version version32 = new Version(3, 2);
675 Version version40 = new Version(4, 0);
677 Set<Version> viewableVersions = new HashSet<>();
678 viewableVersions.add(VERSION10);
679 viewableVersions.add(new Version(2, 0));
680 viewableVersions.add(new Version(3, 0));
681 viewableVersions.add(new Version(3, 1));
682 viewableVersions.add(version32);
683 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Draft, version32, null, viewableVersions,
686 Version version = versioningManager.submit(TYPE1, ID1, USR1, "submit msg");
687 Assert.assertEquals(version, version40);
688 viewableVersions.remove(new Version(3, 1));
689 viewableVersions.remove(version32);
690 viewableVersions.add(version40);
692 verify(versionInfoDaoMock).update(versionInfoEntityArg.capture());
693 VersionInfoEntity versionInfoEntity = versionInfoEntityArg.getValue();
695 assretVersionInfoEntity(versionInfoEntity, TYPE1, ID1, version40, null, null,
696 VersionStatus.Certified, viewableVersions, version40);
699 /* ResultSet results =
700 loadVersionableEntityRecord(TYPE1_TABLE_NAME, TYPE1_ID_NAME, TYPE1_VERSION_NAME, ID1,
702 Assert.assertTrue(results.iterator().hasNext());*//*
706 @Test(expectedExceptions = CoreException.class)
707 public void testGetVersionInfoOnNonExistingEntity() {
708 versioningManager.getEntityVersionInfo(TYPE1, ID1, USR1, VersionableEntityAction.Read);
712 public void testGetVersionInfoForReadOnAvailable() {
713 Set<Version> viewableVersions = new HashSet<>();
714 viewableVersions.add(VERSION01);
715 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Draft, VERSION01, null, viewableVersions,
718 VersionInfo versionInfo =
719 versioningManager.getEntityVersionInfo(TYPE1, ID1, USR1, VersionableEntityAction.Read);
720 assertVersionInfo(versionInfo, VERSION01, VersionStatus.Draft, null,
721 viewableVersions, null);
724 @Test(expectedExceptions = CoreException.class)
725 public void testGetVersionInfoForWriteOnAvailable() {
726 Set<Version> viewableVersions = new HashSet<>();
727 viewableVersions.add(VERSION01);
728 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Draft, VERSION01, null, viewableVersions,
731 versioningManager.getEntityVersionInfo(TYPE1, ID1, USR1, VersionableEntityAction.Write);
735 public void testGetVersionInfoForReadOnLockedSameUser() {
736 Set<Version> viewableVersions = new HashSet<>();
737 viewableVersions.add(VERSION01);
738 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION01,
739 new UserCandidateVersion(USR1, VERSION02), viewableVersions, null);
741 VersionInfo versionInfo =
742 versioningManager.getEntityVersionInfo(TYPE1, ID1, USR1, VersionableEntityAction.Read);
743 viewableVersions.add(VERSION02);
744 assertVersionInfo(versionInfo, VERSION02, VersionStatus.Locked, USR1, viewableVersions, null);
748 public void testGetVersionInfoForReadOnLockedOtherUser() {
749 Set<Version> viewableVersions = new HashSet<>();
750 viewableVersions.add(VERSION01);
751 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION01,
752 new UserCandidateVersion(USR2, VERSION02), viewableVersions, null);
754 VersionInfo versionInfo =
755 versioningManager.getEntityVersionInfo(TYPE1, ID1, USR1, VersionableEntityAction.Read);
756 Assert.assertEquals(versionInfo.getActiveVersion(), VERSION01);
757 assertVersionInfo(versionInfo, VERSION01, VersionStatus.Locked, USR2, viewableVersions, null);
760 @Test(expectedExceptions = CoreException.class)
761 public void testGetVersionInfoForWriteOnLockedOtherUser() {
762 Set<Version> viewableVersions = new HashSet<>();
763 viewableVersions.add(VERSION01);
764 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION01,
765 new UserCandidateVersion(USR2, VERSION02), viewableVersions, null);
767 versioningManager.getEntityVersionInfo(TYPE1, ID1, USR1, VersionableEntityAction.Write);
771 public void testGetVersionInfoForWriteOnLockedSameUser() {
772 Set<Version> viewableVersions = new HashSet<>();
773 viewableVersions.add(VERSION01);
774 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION01,
775 new UserCandidateVersion(USR1, VERSION02), viewableVersions, null);
777 VersionInfo versionInfo =
778 versioningManager.getEntityVersionInfo(TYPE1, ID1, USR1, VersionableEntityAction.Write);
779 viewableVersions.add(VERSION02);
780 assertVersionInfo(versionInfo, VERSION02, VersionStatus.Locked, USR1, viewableVersions, null);
784 /* private void createVersionableEntityRecord(String tableName, String idName, String versionName,
785 String id, Version version) {
787 String.format("insert into %s (%s,%s) values (?,?)", tableName, idName, versionName), id,
788 versionMapper.toUDT(version));
791 private ResultSet loadVersionableEntityRecord(String tableName, String idName, String versionName,
792 String id, Version version) {
793 return noSqlDb.execute(
794 String.format("select * from %s where %s=? and %s=?", tableName, idName, versionName), id,
795 versionMapper.toUDT(version));
800 private static void assretVersionInfoEntity(VersionInfoEntity actual, String entityType,
801 String entityId, Version activeVersion,
802 Version candidateVersion, String candidateUser,
803 VersionStatus status, Set<Version> viewbleVersions,
804 Version latestFinalVersion) {
805 Assert.assertNotNull(actual);
806 Assert.assertEquals(actual.getEntityType(), entityType);
807 Assert.assertEquals(actual.getEntityId(), entityId);
808 Assert.assertEquals(actual.getActiveVersion(), activeVersion);
809 if (candidateVersion != null && candidateUser != null) {
810 Assert.assertEquals(actual.getCandidate().getVersion(), candidateVersion);
811 Assert.assertEquals(actual.getCandidate().getUser(), candidateUser);
813 Assert.assertNull(actual.getCandidate());
815 Assert.assertEquals(actual.getStatus(), status);
816 Assert.assertEquals(actual.getViewableVersions().size(), viewbleVersions.size());
817 Assert.assertEquals(actual.getViewableVersions(), viewbleVersions);
818 Assert.assertEquals(actual.getLatestFinalVersion(), latestFinalVersion);
821 private static void assertVersionInfo(VersionInfo actual, Version activeVersion,
822 VersionStatus status, String lockingUser,
823 Set<Version> viewableVersions, Version latestFinalVersion) {
824 Assert.assertNotNull(actual);
825 Assert.assertEquals(actual.getActiveVersion(), activeVersion);
826 Assert.assertEquals(actual.getStatus(), status);
827 Assert.assertEquals(actual.getLockingUser(), lockingUser);
828 Assert.assertEquals(actual.getViewableVersions().size(), viewableVersions.size());
829 Assert.assertEquals(actual.getViewableVersions(), viewableVersions);
830 Assert.assertEquals(actual.getLatestFinalVersion(), latestFinalVersion);
833 private VersionInfoEntity mockVersionInfoEntity(String entityType, String entityId,
834 VersionStatus status, Version activeVersion,
835 UserCandidateVersion candidate,
836 Set<Version> viewableVersions,
837 Version latestFinalVersion) {
838 VersionInfoEntity mock = new VersionInfoEntity();
839 mock.setEntityType(entityType);
840 mock.setEntityId(entityId);
841 mock.setStatus(status);
842 mock.setActiveVersion(activeVersion);
843 mock.setCandidate(candidate);
844 mock.setViewableVersions(viewableVersions);
845 mock.setLatestFinalVersion(latestFinalVersion);
847 doReturn(mock).when(versionInfoDaoMock).get(anyObject());