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;
23 import org.mockito.ArgumentCaptor;
24 import org.mockito.InjectMocks;
25 import org.mockito.Mock;
26 import org.mockito.MockitoAnnotations;
27 import org.openecomp.sdc.common.errors.CoreException;
28 import org.openecomp.sdc.versioning.ItemManager;
29 import org.openecomp.sdc.versioning.VersionCalculator;
30 import org.openecomp.sdc.versioning.dao.VersionDao;
31 import org.openecomp.sdc.versioning.dao.types.*;
32 import org.openecomp.sdc.versioning.types.VersionCreationMethod;
33 import org.testng.Assert;
34 import org.testng.annotations.BeforeMethod;
35 import org.testng.annotations.Test;
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.Optional;
40 import java.util.stream.Collectors;
41 import java.util.stream.Stream;
43 import static org.mockito.Matchers.any;
44 import static org.mockito.Matchers.eq;
45 import static org.mockito.Mockito.*;
46 import static org.openecomp.sdc.versioning.dao.types.SynchronizationState.OutOfSync;
47 import static org.openecomp.sdc.versioning.dao.types.SynchronizationState.UpToDate;
48 import static org.openecomp.sdc.versioning.dao.types.VersionStatus.Certified;
49 import static org.openecomp.sdc.versioning.dao.types.VersionStatus.Draft;
51 public class VersioningManagerImplTest {
52 private static final String ITEM_ID = "itemId";
53 private static final String VERSION_ID = "versionId";
56 private VersionDao versionDaoMock;
58 private VersionCalculator versionCalculatorMock;
60 private ItemManager asdcItemManager;
62 private VersioningManagerImpl versioningManager;
65 public void setUp() throws Exception {
66 MockitoAnnotations.initMocks(this);
70 public void testListWhenNone() throws Exception {
71 doReturn(new ArrayList<>()).when(versionDaoMock).list(ITEM_ID);
73 List<Version> versions = versioningManager.list(ITEM_ID);
75 Assert.assertTrue(versions.isEmpty());
79 public void testList() throws Exception {
80 List<Version> returnedVersions = Stream.of(createVersion("1", null, null, false),
81 createVersion("2", null, null, false),
82 createVersion("3", null, null, false)).collect(Collectors.toList());
83 doReturn(returnedVersions).when(versionDaoMock).list(ITEM_ID);
85 List<Version> versions = versioningManager.list(ITEM_ID);
86 Assert.assertEquals(versions, returnedVersions);
89 @Test(expectedExceptions = Exception.class)
90 public void testGetNonExisting() throws Exception {
91 Version version = new Version(VERSION_ID);
93 doReturn(Optional.empty()).when(versionDaoMock).get(ITEM_ID, version);
94 doThrow(new Exception()).when(versionDaoMock).sync(ITEM_ID, version);
96 versioningManager.get(ITEM_ID, version);
100 public void testGetNonExistingForUser() throws Exception {
101 Version requestedVersion = new Version(VERSION_ID);
103 Version returnedVersion = createVersion(VERSION_ID, Draft, UpToDate, false);
104 doReturn(Optional.empty()).doReturn(Optional.of(returnedVersion))
105 .when(versionDaoMock).get(ITEM_ID, requestedVersion);
107 Version version = versioningManager.get(ITEM_ID, requestedVersion);
108 Assert.assertEquals(version, returnedVersion);
110 verify(versionDaoMock, times(2)).get(ITEM_ID, requestedVersion);
111 verify(versionDaoMock).sync(ITEM_ID, requestedVersion);
115 public void testGetOutOfSyncCertified() throws Exception {
116 Version requestedVersion = new Version(VERSION_ID);
118 Version returnedVersion = createVersion(VERSION_ID, Certified, UpToDate, false);
119 doReturn(Optional.of(createVersion(VERSION_ID, Certified, OutOfSync, false)))
120 .doReturn(Optional.of(returnedVersion))
121 .when(versionDaoMock).get(ITEM_ID, requestedVersion);
123 Version version = versioningManager.get(ITEM_ID, requestedVersion);
124 Assert.assertEquals(version, returnedVersion);
126 verify(versionDaoMock, times(2)).get(ITEM_ID, requestedVersion);
127 verify(versionDaoMock).forceSync(ITEM_ID, requestedVersion);
131 public void testGet() throws Exception {
132 Version requestedVersion = new Version(VERSION_ID);
134 Version returnedVersion = createVersion(VERSION_ID, Draft, OutOfSync, true);
135 doReturn(Optional.of(returnedVersion)).when(versionDaoMock).get(ITEM_ID, requestedVersion);
137 Version version = versioningManager.get(ITEM_ID, requestedVersion);
138 Assert.assertEquals(version, returnedVersion);
140 verify(versionDaoMock).get(ITEM_ID, requestedVersion);
141 verify(versionDaoMock, never()).sync(any(), any());
142 verify(versionDaoMock, never()).forceSync(any(), any());
146 public void testCreate() throws Exception {
147 Version requestedVersion = new Version();
149 String versionName = "versionName";
150 doReturn(versionName).when(versionCalculatorMock).calculate(null, VersionCreationMethod.major);
152 doReturn(Stream.of(createVersion("1", null, null, false),
153 createVersion("2", null, null, false),
154 createVersion("3", null, null, false)).collect(Collectors.toList()))
155 .when(versionDaoMock).list(ITEM_ID);
158 versioningManager.create(ITEM_ID, requestedVersion, VersionCreationMethod.major);
159 Assert.assertNotNull(version);
160 Assert.assertEquals(version.getName(), versionName);
162 verify(versionDaoMock).create(ITEM_ID, requestedVersion);
163 verify(asdcItemManager).updateVersionStatus(ITEM_ID, Draft, null);
164 verify(versionDaoMock)
165 .publish(eq(ITEM_ID), eq(requestedVersion), eq("Create version: versionName"));
169 public void testCreateBasedOn() throws Exception {
170 Version requestedVersion = new Version();
171 requestedVersion.setBaseId("baseVersionId");
173 Version baseVersion = createVersion(requestedVersion.getBaseId(), Certified, UpToDate, false);
174 // TODO: 12/13/2017 fix to eq(new Version("baseVersionId")) when version.equals will be fixed
175 doReturn(Optional.of(baseVersion)).when(versionDaoMock).get(eq(ITEM_ID), any(Version.class));
177 String versionName = "4.0";
178 doReturn(versionName)
179 .when(versionCalculatorMock).calculate(baseVersion.getName(), VersionCreationMethod.major);
181 doReturn(Stream.of(createVersion("1", null, null, false),
182 createVersion("2", null, null, false),
183 createVersion("3", null, null, false)).collect(Collectors.toList()))
184 .when(versionDaoMock).list(ITEM_ID);
187 versioningManager.create(ITEM_ID, requestedVersion, VersionCreationMethod.major);
188 Assert.assertNotNull(version);
189 Assert.assertEquals(version.getName(), versionName);
191 verify(versionDaoMock).create(ITEM_ID, requestedVersion);
192 verify(asdcItemManager).updateVersionStatus(ITEM_ID, Draft, null);
193 verify(versionDaoMock).publish(eq(ITEM_ID), eq(requestedVersion), eq("Create version: 4.0"));
196 @Test(expectedExceptions = CoreException.class, expectedExceptionsMessageRegExp =
197 "Item itemId: create version failed, a version with the name 2.0 already exist")
198 public void testCreateWithExistingName() throws Exception {
199 Version version = new Version();
200 version.setBaseId("baseVersionId");
202 Version baseVersion = createVersion(version.getBaseId(), Certified, UpToDate, false);
203 // TODO: 12/13/2017 fix to eq(new Version("baseVersionId")) when version.equals will be fixed
204 doReturn(Optional.of(baseVersion)).when(versionDaoMock).get(eq(ITEM_ID), any(Version.class));
206 String versionName = "2.0";
207 doReturn(versionName)
208 .when(versionCalculatorMock).calculate(baseVersion.getName(), VersionCreationMethod.major);
210 doReturn(Stream.of(createVersion("1", null, null, false),
211 createVersion("2", null, null, false),
212 createVersion("3", null, null, false)).collect(Collectors.toList()))
213 .when(versionDaoMock).list(ITEM_ID);
215 versioningManager.create(ITEM_ID, version, VersionCreationMethod.major);
218 @Test(expectedExceptions = CoreException.class, expectedExceptionsMessageRegExp =
219 "Item itemId: submit version failed, version versionId is already Certified")
220 public void testSubmitCertified() throws Exception {
221 Version version = new Version(VERSION_ID);
223 Version returnedVersion = createVersion(VERSION_ID, Certified, UpToDate, false);
224 doReturn(Optional.of(returnedVersion)).when(versionDaoMock).get(ITEM_ID, version);
226 versioningManager.submit(ITEM_ID, version, "Submit message");
230 public void testSubmit() throws Exception {
231 Version version = new Version(VERSION_ID);
233 ArgumentCaptor<Version> versionArgumentCaptor = ArgumentCaptor.forClass(Version.class);
235 Version returnedVersion = createVersion(VERSION_ID, Draft, UpToDate, false);
236 doReturn(Optional.of(returnedVersion)).when(versionDaoMock).get(ITEM_ID, version);
238 String submitDescription = "Submit message";
239 versioningManager.submit(ITEM_ID, version, submitDescription);
241 verify(versionDaoMock).update(eq(ITEM_ID), versionArgumentCaptor.capture());
242 Assert.assertEquals(Certified, versionArgumentCaptor.getValue().getStatus());
243 verify(versionDaoMock).publish(ITEM_ID, version, submitDescription);
244 verify(asdcItemManager).updateVersionStatus(ITEM_ID, Certified, Draft);
248 public void testPublish() throws Exception {
249 Version version = new Version(VERSION_ID);
250 String publishDescription = "Publish message";
252 versioningManager.publish(ITEM_ID, version, publishDescription);
254 verify(versionDaoMock).publish(ITEM_ID, version, publishDescription);
258 public void testSync() throws Exception {
259 Version version = new Version(VERSION_ID);
261 versioningManager.sync(ITEM_ID, version);
263 verify(versionDaoMock).sync(ITEM_ID, version);
267 public void testForceSync() throws Exception {
268 Version version = new Version(VERSION_ID);
270 versioningManager.forceSync(ITEM_ID, version);
272 verify(versionDaoMock).forceSync(ITEM_ID, version);
276 public void testRevert() throws Exception {
277 Version version = new Version(VERSION_ID);
278 String revisionId = "revisionId";
280 versioningManager.revert(ITEM_ID, version, revisionId);
282 verify(versionDaoMock).revert(ITEM_ID, version, revisionId);
286 public void testListRevisions() throws Exception {
287 Version version = new Version(VERSION_ID);
289 List<Revision> returnedRevisions =
290 Stream.of(new Revision(), new Revision()).collect(Collectors.toList());
291 doReturn(returnedRevisions)
292 .when(versionDaoMock).listRevisions(ITEM_ID, version);
294 List<Revision> revisions = versioningManager.listRevisions(ITEM_ID, version);
295 Assert.assertEquals(revisions, returnedRevisions);
298 private Version createVersion(String id, VersionStatus status,
299 SynchronizationState syncState, boolean dirty) {
300 Version version = new Version(id);
301 version.setName(id + ".0");
302 version.setDescription(id + " desc");
303 version.setStatus(status);
305 VersionState state = new VersionState();
306 state.setSynchronizationState(syncState);
307 state.setDirty(dirty);
308 version.setState(state);
313 private static final String USR1 = "usr1";
314 private static final String USR2 = "usr2";
315 private static final String TYPE1 = "Type1";
317 /* private static final String TYPE2 = "Type2";*//*
319 private static final String ID1 = "Id1";
321 /* private static final String ID2 = "Id2";
322 private static final String ID3 = "Id3";
323 private static final String TYPE1_TABLE_NAME = "vendor_license_model";
324 private static final String TYPE1_ID_NAME = "vlm_id";
325 private static final String TYPE1_VERSION_NAME = "version";
326 private static final String TYPE2_TABLE_NAME = "feature_group";
327 private static final String TYPE2_ID_NAME = "vlm_id";
328 private static final String TYPE2_VERSION_NAME = "version";*//*
330 private static final Version VERSION0 = new Version(0, 0);
331 private static final Version VERSION01 = new Version(0, 1);
332 private static final Version VERSION02 = new Version(0, 2);
333 private static final Version VERSION10 = new Version(1, 0);
334 private static final Version VERSION11 = new Version(1, 1);
337 /* private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
339 private static UDTMapper<Version> versionMapper =
340 noSqlDb.getMappingManager().udtMapper(Version.class);*//*
343 private VersionInfoDao versionInfoDaoMock;
345 private VersionInfoDeletedDao versionInfoDeletedDaoMock;
347 private VersioningManagerImpl versioningManager;
350 private ArgumentCaptor<VersionInfoEntity> versionInfoEntityArg;
353 public void setUp() throws Exception {
354 MockitoAnnotations.initMocks(this);
359 private void init() {
360 versionInfoDaoMock.delete(new VersionInfoEntity(TYPE1, ID1));
361 versionInfoDaoMock.delete(new VersionInfoEntity(TYPE1, ID2));
362 versionInfoDaoMock.delete(new VersionInfoEntity(TYPE2, ID3));
363 String deleteFromType1 = String
364 .format("delete from %s where %s=? and %s=?", TYPE1_TABLE_NAME, TYPE1_ID_NAME,
366 noSqlDb.execute(deleteFromType1, ID1, versionMapper.toUDT(VERSION01));
367 noSqlDb.execute(deleteFromType1, ID1, versionMapper.toUDT(VERSION02));
368 noSqlDb.execute(deleteFromType1, ID1, versionMapper.toUDT(VERSION11));
370 versioningManager.register(TYPE1,
371 new VersionableEntityMetadata(TYPE1_TABLE_NAME, TYPE1_ID_NAME, TYPE1_VERSION_NAME));
372 versioningManager.register(TYPE2,
373 new VersionableEntityMetadata(TYPE2_TABLE_NAME, TYPE2_ID_NAME, TYPE2_VERSION_NAME));
379 public void testRegister() throws Exception {
380 VersionableEntityMetadata entityMetadata =
381 new VersionableEntityMetadata(TYPE1_TABLE_NAME, TYPE1_ID_NAME, TYPE1_VERSION_NAME);
382 versioningManager.register(TYPE1, entityMetadata);
384 Map<String, Set<VersionableEntityMetadata>> versionableEntities =
385 versionableEntitiesCapture.capture();
386 Set<VersionableEntityMetadata> type1Entities = versionableEntities.get(TYPE1);
387 Assert.assertNotNull(type1Entities);
388 Assert.assertTrue(type1Entities.contains(entityMetadata));
392 @Test(expectedExceptions = CoreException.class)
393 public void testCreateAlreadyExisting() {
394 doReturn(new VersionInfoEntity()).when(versionInfoDaoMock).get(anyObject());
395 versioningManager.create(TYPE1, ID1, USR1);
399 public void testCreate() {
400 Version version = versioningManager.create(TYPE1, ID1, USR1);
401 Assert.assertEquals(version, VERSION01);
404 /* createVersionableEntityRecord(TYPE1_TABLE_NAME, TYPE1_ID_NAME, TYPE1_VERSION_NAME, ID1,
407 verify(versionInfoDaoMock).create(versionInfoEntityArg.capture());
408 VersionInfoEntity versionInfoEntity = versionInfoEntityArg.getValue();
409 assretVersionInfoEntity(versionInfoEntity, TYPE1, ID1, new Version(0, 0), VERSION01, USR1,
410 VersionStatus.Locked, new HashSet<>(), null);
413 @Test(expectedExceptions = CoreException.class)
414 public void testDeleteNonExisting() {
415 versioningManager.delete(TYPE1, ID1, USR1);
418 @Test(expectedExceptions = CoreException.class)
419 public void testDeleteLocked() {
420 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION0,
421 new UserCandidateVersion(USR1, VERSION01), Collections.emptySet(), null);
422 versioningManager.delete(TYPE1, ID1, USR1);
426 public void testDelete() {
427 VersionInfoEntity versionInfoEntity = new VersionInfoEntity();
428 versionInfoEntity.setStatus(VersionStatus.Draft);
429 doReturn(versionInfoEntity).when(versionInfoDaoMock).get(anyObject());
431 versioningManager.delete(TYPE1, ID1, USR1);
433 verify(versionInfoDaoMock).delete(versionInfoEntity);
434 ArgumentCaptor<VersionInfoDeletedEntity> versionInfoDeletedEntityArg =
435 ArgumentCaptor.forClass(VersionInfoDeletedEntity.class);
436 verify(versionInfoDeletedDaoMock).create(versionInfoDeletedEntityArg.capture());
439 @Test(expectedExceptions = CoreException.class)
440 public void testUndoDeleteNonExisting() {
441 versioningManager.undoDelete(TYPE1, ID1, USR1);
445 public void testUndoDelete() {
446 VersionInfoDeletedEntity versionInfoDeletedEntity = new VersionInfoDeletedEntity();
447 versionInfoDeletedEntity.setStatus(VersionStatus.Draft);
448 doReturn(versionInfoDeletedEntity).when(versionInfoDeletedDaoMock).get(anyObject());
450 versioningManager.undoDelete(TYPE1, ID1, USR1);
452 verify(versionInfoDeletedDaoMock).delete(versionInfoDeletedEntity);
453 verify(versionInfoDaoMock).create(versionInfoEntityArg.capture());
456 VersionInfoDeletedEntity versionInfoDeletedEntity =
457 versionInfoDeletedDaoMock.get(new VersionInfoDeletedEntity(TYPE1, ID1));
458 Assert.assertNotNull(versionInfoDeletedEntity);
460 Map<String, VersionInfo> entitiesInfoMap =
461 versioningManager.listDeletedEntitiesVersionInfo(TYPE1, USR2, null);
462 Assert.assertEquals(entitiesInfoMap.size(), 1);
463 VersionInfoEntity versionInfoEntity = versionInfoDaoMock.get(new VersionInfoEntity(TYPE1, ID1));
464 Assert.assertNull(versionInfoEntity);
465 versioningManager.undoDelete(TYPE1, ID1, USR1);
466 versionInfoEntity = versionInfoDaoMock.get(new VersionInfoEntity(TYPE1, ID1));
467 Assert.assertNotNull(versionInfoEntity);*//*
471 @Test(expectedExceptions = CoreException.class)
472 public void testCheckoutNonExisting() {
473 versioningManager.checkout(TYPE1, ID1, USR2);
476 @Test(expectedExceptions = CoreException.class)
477 public void testCheckoutOnLockedSameUser() {
478 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION0,
479 new UserCandidateVersion(USR1, VERSION01), Collections.emptySet(), null);
480 versioningManager.checkout(TYPE1, ID1, USR1);
483 @Test(expectedExceptions = CoreException.class)
484 public void testCheckoutOnLockedOtherUser() {
485 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION0,
486 new UserCandidateVersion(USR2, VERSION01), Collections.emptySet(), null);
487 versioningManager.checkout(TYPE1, ID1, USR1);
491 public void testCheckoutOnFinalized() {
492 Set<Version> viewableVersions = new HashSet<>();
493 viewableVersions.add(VERSION10);
494 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Certified, VERSION10, null, viewableVersions,
497 Version version = versioningManager.checkout(TYPE1, ID1, USR1);
498 Assert.assertEquals(version, VERSION11);
500 VersionInfoEntity versionInfoEntity = versionInfoDaoMock.get(new VersionInfoEntity(TYPE1, ID1));
501 assretVersionInfoEntity(versionInfoEntity, TYPE1, ID1, VERSION10, VERSION11, USR1,
502 VersionStatus.Locked, viewableVersions, VERSION10);
506 loadVersionableEntityRecord(TYPE1_TABLE_NAME, TYPE1_ID_NAME, TYPE1_VERSION_NAME, ID1,
508 Assert.assertTrue(results.iterator().hasNext());*//*
513 public void testCheckout() {
514 Set<Version> viewableVersions = new HashSet<>();
515 viewableVersions.add(VERSION01);
516 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Draft, VERSION01, null, viewableVersions,
519 Version version = versioningManager.checkout(TYPE1, ID1, USR1);
520 Assert.assertEquals(version, VERSION02);
522 verify(versionInfoDaoMock).update(versionInfoEntityArg.capture());
523 VersionInfoEntity versionInfoEntity = versionInfoEntityArg.getValue();
525 assretVersionInfoEntity(versionInfoEntity, TYPE1, ID1, VERSION01, VERSION02, USR1,
526 VersionStatus.Locked, viewableVersions, null);
529 /* ResultSet results =
530 loadVersionableEntityRecord(TYPE1_TABLE_NAME, TYPE1_ID_NAME, TYPE1_VERSION_NAME, ID1,
532 Assert.assertTrue(results.iterator().hasNext());*//*
536 @Test(expectedExceptions = CoreException.class)
537 public void testUndoCheckoutNonExisting() {
538 versioningManager.undoCheckout(TYPE1, ID1, USR1);
541 @Test(expectedExceptions = CoreException.class)
542 public void testUndoCheckoutOnAvailable() {
543 Set<Version> viewableVersions = new HashSet<>();
544 viewableVersions.add(VERSION01);
545 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Draft, VERSION01, null, viewableVersions,
548 versioningManager.undoCheckout(TYPE1, ID1, USR1);
551 @Test(expectedExceptions = CoreException.class)
552 public void testUndoCheckouOnFinalized() {
553 Set<Version> viewableVersions = new HashSet<>();
554 viewableVersions.add(VERSION10);
555 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Certified, VERSION10, null, viewableVersions,
557 versioningManager.undoCheckout(TYPE1, ID1, USR2);
560 @Test(expectedExceptions = CoreException.class)
561 public void testUndoCheckoutOnLockedOtherUser() {
562 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION0,
563 new UserCandidateVersion(USR2, VERSION01), Collections.emptySet(), null);
565 versioningManager.undoCheckout(TYPE1, ID1, USR1);
569 public void testUndoCheckout() {
570 HashSet<Version> viewableVersions = new HashSet<>();
571 viewableVersions.add(VERSION01);
572 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION01,
573 new UserCandidateVersion(USR1, VERSION02), viewableVersions, null);
575 Version version = versioningManager.undoCheckout(TYPE1, ID1, USR1);
576 Assert.assertEquals(version, VERSION01);
578 VersionInfoEntity versionInfoEntity = versionInfoDaoMock.get(new VersionInfoEntity(TYPE1, ID1));
579 assretVersionInfoEntity(versionInfoEntity, TYPE1, ID1, VERSION01, null, null,
580 VersionStatus.Draft, viewableVersions, null);
583 /* ResultSet results =
584 loadVersionableEntityRecord(TYPE1_TABLE_NAME, TYPE1_ID_NAME, TYPE1_VERSION_NAME, ID1,
586 Assert.assertFalse(results.iterator().hasNext());*//*
590 @Test(expectedExceptions = CoreException.class)
591 public void testCheckinNonExisting() {
592 versioningManager.checkin(TYPE1, ID1, USR1, "");
595 @Test(expectedExceptions = CoreException.class)
596 public void testCheckinOnAvailable() {
597 Set<Version> viewableVersions = new HashSet<>();
598 viewableVersions.add(VERSION01);
599 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Draft, VERSION01, null, viewableVersions,
602 versioningManager.checkin(TYPE1, ID1, USR1, "fail checkin");
606 @Test(expectedExceptions = CoreException.class)
607 public void testCheckinOnFinalized() {
608 Set<Version> viewableVersions = new HashSet<>();
609 viewableVersions.add(VERSION10);
610 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Certified, VERSION10, null, viewableVersions,
613 versioningManager.checkin(TYPE1, ID1, USR1, "failed checkin");
616 @Test(expectedExceptions = CoreException.class)
617 public void testCheckinOnLockedOtherUser() {
618 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION0,
619 new UserCandidateVersion(USR2, VERSION01), Collections.emptySet(), null);
621 versioningManager.checkin(TYPE1, ID1, USR1, "");
625 public void testCheckin() {
626 HashSet<Version> viewableVersions = new HashSet<>();
627 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION0,
628 new UserCandidateVersion(USR1, VERSION01), viewableVersions, null);
630 Version version = versioningManager.checkin(TYPE1, ID1, USR1, "checkin 0.1");
631 Assert.assertEquals(version, VERSION01);
633 verify(versionInfoDaoMock).update(versionInfoEntityArg.capture());
634 VersionInfoEntity versionInfoEntity = versionInfoEntityArg.getValue();
636 viewableVersions.add(VERSION01);
637 assretVersionInfoEntity(versionInfoEntity, TYPE1, ID1, VERSION01, null, null,
638 VersionStatus.Draft, viewableVersions, null);
641 @Test(expectedExceptions = CoreException.class)
642 public void testSubmitNonExisting() {
643 versioningManager.submit(TYPE1, ID1, USR2, "failed submit");
646 @Test(expectedExceptions = CoreException.class)
647 public void testSubmitOnLocked() {
648 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION0,
649 new UserCandidateVersion(USR1, VERSION01), Collections.emptySet(), null);
650 versioningManager.submit(TYPE1, ID1, USR2, "failed submit");
654 @Test(expectedExceptions = CoreException.class)
655 public void testSubmitOnFinalized() {
656 Set<Version> viewableVersions = new HashSet<>();
657 viewableVersions.add(VERSION10);
658 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Certified, VERSION10, null, viewableVersions,
660 versioningManager.submit(TYPE1, ID1, USR2, "failed submit");
664 public void testSubmit() {
665 Version version32 = new Version(3, 2);
666 Version version40 = new Version(4, 0);
668 Set<Version> viewableVersions = new HashSet<>();
669 viewableVersions.add(VERSION10);
670 viewableVersions.add(new Version(2, 0));
671 viewableVersions.add(new Version(3, 0));
672 viewableVersions.add(new Version(3, 1));
673 viewableVersions.add(version32);
674 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Draft, version32, null, viewableVersions,
677 Version version = versioningManager.submit(TYPE1, ID1, USR1, "submit msg");
678 Assert.assertEquals(version, version40);
679 viewableVersions.remove(new Version(3, 1));
680 viewableVersions.remove(version32);
681 viewableVersions.add(version40);
683 verify(versionInfoDaoMock).update(versionInfoEntityArg.capture());
684 VersionInfoEntity versionInfoEntity = versionInfoEntityArg.getValue();
686 assretVersionInfoEntity(versionInfoEntity, TYPE1, ID1, version40, null, null,
687 VersionStatus.Certified, viewableVersions, version40);
690 /* ResultSet results =
691 loadVersionableEntityRecord(TYPE1_TABLE_NAME, TYPE1_ID_NAME, TYPE1_VERSION_NAME, ID1,
693 Assert.assertTrue(results.iterator().hasNext());*//*
697 @Test(expectedExceptions = CoreException.class)
698 public void testGetVersionInfoOnNonExistingEntity() {
699 versioningManager.getEntityVersionInfo(TYPE1, ID1, USR1, VersionableEntityAction.Read);
703 public void testGetVersionInfoForReadOnAvailable() {
704 Set<Version> viewableVersions = new HashSet<>();
705 viewableVersions.add(VERSION01);
706 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Draft, VERSION01, null, viewableVersions,
709 VersionInfo versionInfo =
710 versioningManager.getEntityVersionInfo(TYPE1, ID1, USR1, VersionableEntityAction.Read);
711 assertVersionInfo(versionInfo, VERSION01, VersionStatus.Draft, null,
712 viewableVersions, null);
715 @Test(expectedExceptions = CoreException.class)
716 public void testGetVersionInfoForWriteOnAvailable() {
717 Set<Version> viewableVersions = new HashSet<>();
718 viewableVersions.add(VERSION01);
719 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Draft, VERSION01, null, viewableVersions,
722 versioningManager.getEntityVersionInfo(TYPE1, ID1, USR1, VersionableEntityAction.Write);
726 public void testGetVersionInfoForReadOnLockedSameUser() {
727 Set<Version> viewableVersions = new HashSet<>();
728 viewableVersions.add(VERSION01);
729 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION01,
730 new UserCandidateVersion(USR1, VERSION02), viewableVersions, null);
732 VersionInfo versionInfo =
733 versioningManager.getEntityVersionInfo(TYPE1, ID1, USR1, VersionableEntityAction.Read);
734 viewableVersions.add(VERSION02);
735 assertVersionInfo(versionInfo, VERSION02, VersionStatus.Locked, USR1, viewableVersions, null);
739 public void testGetVersionInfoForReadOnLockedOtherUser() {
740 Set<Version> viewableVersions = new HashSet<>();
741 viewableVersions.add(VERSION01);
742 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION01,
743 new UserCandidateVersion(USR2, VERSION02), viewableVersions, null);
745 VersionInfo versionInfo =
746 versioningManager.getEntityVersionInfo(TYPE1, ID1, USR1, VersionableEntityAction.Read);
747 Assert.assertEquals(versionInfo.getActiveVersion(), VERSION01);
748 assertVersionInfo(versionInfo, VERSION01, VersionStatus.Locked, USR2, viewableVersions, null);
751 @Test(expectedExceptions = CoreException.class)
752 public void testGetVersionInfoForWriteOnLockedOtherUser() {
753 Set<Version> viewableVersions = new HashSet<>();
754 viewableVersions.add(VERSION01);
755 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION01,
756 new UserCandidateVersion(USR2, VERSION02), viewableVersions, null);
758 versioningManager.getEntityVersionInfo(TYPE1, ID1, USR1, VersionableEntityAction.Write);
762 public void testGetVersionInfoForWriteOnLockedSameUser() {
763 Set<Version> viewableVersions = new HashSet<>();
764 viewableVersions.add(VERSION01);
765 mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION01,
766 new UserCandidateVersion(USR1, VERSION02), viewableVersions, null);
768 VersionInfo versionInfo =
769 versioningManager.getEntityVersionInfo(TYPE1, ID1, USR1, VersionableEntityAction.Write);
770 viewableVersions.add(VERSION02);
771 assertVersionInfo(versionInfo, VERSION02, VersionStatus.Locked, USR1, viewableVersions, null);
775 /* private void createVersionableEntityRecord(String tableName, String idName, String versionName,
776 String id, Version version) {
778 String.format("insert into %s (%s,%s) values (?,?)", tableName, idName, versionName), id,
779 versionMapper.toUDT(version));
782 private ResultSet loadVersionableEntityRecord(String tableName, String idName, String versionName,
783 String id, Version version) {
784 return noSqlDb.execute(
785 String.format("select * from %s where %s=? and %s=?", tableName, idName, versionName), id,
786 versionMapper.toUDT(version));
791 private static void assretVersionInfoEntity(VersionInfoEntity actual, String entityType,
792 String entityId, Version activeVersion,
793 Version candidateVersion, String candidateUser,
794 VersionStatus status, Set<Version> viewbleVersions,
795 Version latestFinalVersion) {
796 Assert.assertNotNull(actual);
797 Assert.assertEquals(actual.getEntityType(), entityType);
798 Assert.assertEquals(actual.getEntityId(), entityId);
799 Assert.assertEquals(actual.getActiveVersion(), activeVersion);
800 if (candidateVersion != null && candidateUser != null) {
801 Assert.assertEquals(actual.getCandidate().getVersion(), candidateVersion);
802 Assert.assertEquals(actual.getCandidate().getUser(), candidateUser);
804 Assert.assertNull(actual.getCandidate());
806 Assert.assertEquals(actual.getStatus(), status);
807 Assert.assertEquals(actual.getViewableVersions().size(), viewbleVersions.size());
808 Assert.assertEquals(actual.getViewableVersions(), viewbleVersions);
809 Assert.assertEquals(actual.getLatestFinalVersion(), latestFinalVersion);
812 private static void assertVersionInfo(VersionInfo actual, Version activeVersion,
813 VersionStatus status, String lockingUser,
814 Set<Version> viewableVersions, Version latestFinalVersion) {
815 Assert.assertNotNull(actual);
816 Assert.assertEquals(actual.getActiveVersion(), activeVersion);
817 Assert.assertEquals(actual.getStatus(), status);
818 Assert.assertEquals(actual.getLockingUser(), lockingUser);
819 Assert.assertEquals(actual.getViewableVersions().size(), viewableVersions.size());
820 Assert.assertEquals(actual.getViewableVersions(), viewableVersions);
821 Assert.assertEquals(actual.getLatestFinalVersion(), latestFinalVersion);
824 private VersionInfoEntity mockVersionInfoEntity(String entityType, String entityId,
825 VersionStatus status, Version activeVersion,
826 UserCandidateVersion candidate,
827 Set<Version> viewableVersions,
828 Version latestFinalVersion) {
829 VersionInfoEntity mock = new VersionInfoEntity();
830 mock.setEntityType(entityType);
831 mock.setEntityId(entityId);
832 mock.setStatus(status);
833 mock.setActiveVersion(activeVersion);
834 mock.setCandidate(candidate);
835 mock.setViewableVersions(viewableVersions);
836 mock.setLatestFinalVersion(latestFinalVersion);
838 doReturn(mock).when(versionInfoDaoMock).get(anyObject());