aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/test/java/org/openecomp/sdc/versioning/impl/VersioningManagerImplTest.java
blob: a58356a1a4be717fafb2bc402fd39dc0c37dc9e4 (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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.openecomp.sdc.versioning.impl;

import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.openecomp.sdc.common.errors.CoreException;
import org.openecomp.sdc.versioning.ItemManager;
import org.openecomp.sdc.versioning.VersionCalculator;
import org.openecomp.sdc.versioning.dao.VersionDao;
import org.openecomp.sdc.versioning.dao.types.*;
import org.openecomp.sdc.versioning.types.VersionCreationMethod;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;
import static org.openecomp.sdc.versioning.dao.types.SynchronizationState.OutOfSync;
import static org.openecomp.sdc.versioning.dao.types.SynchronizationState.UpToDate;
import static org.openecomp.sdc.versioning.dao.types.VersionStatus.Certified;
import static org.openecomp.sdc.versioning.dao.types.VersionStatus.Draft;

public class VersioningManagerImplTest {
  private static final String ITEM_ID = "itemId";
  private static final String VERSION_ID = "versionId";

  @Mock
  private VersionDao versionDaoMock;
  @Mock
  private VersionCalculator versionCalculatorMock;
  @Mock
  private ItemManager asdcItemManager;
  @InjectMocks
  private VersioningManagerImpl versioningManager;

  @BeforeMethod
  public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
  }

  @Test
  public void testListWhenNone() throws Exception {
    doReturn(new ArrayList<>()).when(versionDaoMock).list(ITEM_ID);

    List<Version> versions = versioningManager.list(ITEM_ID);

    Assert.assertTrue(versions.isEmpty());
  }

  @Test
  public void testList() throws Exception {
    List<Version> returnedVersions = Stream.of(createVersion("1", null, null, false),
        createVersion("2", null, null, false),
        createVersion("3", null, null, false)).collect(Collectors.toList());
    doReturn(returnedVersions).when(versionDaoMock).list(ITEM_ID);

    List<Version> versions = versioningManager.list(ITEM_ID);
    Assert.assertEquals(versions, returnedVersions);
  }

  @Test(expectedExceptions = Exception.class)
  public void testGetNonExisting() throws Exception {
    Version version = new Version(VERSION_ID);

    doReturn(Optional.empty()).when(versionDaoMock).get(ITEM_ID, version);
    doThrow(new Exception()).when(versionDaoMock).sync(ITEM_ID, version);

    versioningManager.get(ITEM_ID, version);
  }

  @Test
  public void testGetNonExistingForUser() throws Exception {
    Version requestedVersion = new Version(VERSION_ID);

    Version returnedVersion = createVersion(VERSION_ID, Draft, UpToDate, false);
    doReturn(Optional.empty()).doReturn(Optional.of(returnedVersion))
        .when(versionDaoMock).get(ITEM_ID, requestedVersion);

    Version version = versioningManager.get(ITEM_ID, requestedVersion);
    Assert.assertEquals(version, returnedVersion);

    verify(versionDaoMock, times(2)).get(ITEM_ID, requestedVersion);
    verify(versionDaoMock).sync(ITEM_ID, requestedVersion);
  }

  @Test
  public void testGetOutOfSyncCertified() throws Exception {
    Version requestedVersion = new Version(VERSION_ID);

    Version returnedVersion = createVersion(VERSION_ID, Certified, UpToDate, false);
    doReturn(Optional.of(createVersion(VERSION_ID, Certified, OutOfSync, false)))
        .doReturn(Optional.of(returnedVersion))
        .when(versionDaoMock).get(ITEM_ID, requestedVersion);

    Version version = versioningManager.get(ITEM_ID, requestedVersion);
    Assert.assertEquals(version, returnedVersion);

    verify(versionDaoMock, times(2)).get(ITEM_ID, requestedVersion);
    verify(versionDaoMock).forceSync(ITEM_ID, requestedVersion);
  }

  @Test
  public void testGet() throws Exception {
    Version requestedVersion = new Version(VERSION_ID);

    Version returnedVersion = createVersion(VERSION_ID, Draft, OutOfSync, true);
    doReturn(Optional.of(returnedVersion)).when(versionDaoMock).get(ITEM_ID, requestedVersion);

    Version version = versioningManager.get(ITEM_ID, requestedVersion);
    Assert.assertEquals(version, returnedVersion);

    verify(versionDaoMock).get(ITEM_ID, requestedVersion);
    verify(versionDaoMock, never()).sync(any(), any());
    verify(versionDaoMock, never()).forceSync(any(), any());
  }

  @Test
  public void testCreate() throws Exception {
    Version requestedVersion = new Version();

    String versionName = "versionName";
    doReturn(versionName).when(versionCalculatorMock).calculate(null, VersionCreationMethod.major);

    doReturn(Stream.of(createVersion("1", null, null, false),
        createVersion("2", null, null, false),
        createVersion("3", null, null, false)).collect(Collectors.toList()))
        .when(versionDaoMock).list(ITEM_ID);

    Version version =
        versioningManager.create(ITEM_ID, requestedVersion, VersionCreationMethod.major);
    Assert.assertNotNull(version);
    Assert.assertEquals(version.getName(), versionName);

    verify(versionDaoMock).create(ITEM_ID, requestedVersion);
    verify(asdcItemManager).updateVersionStatus(ITEM_ID, Draft, null);
    verify(versionDaoMock)
        .publish(eq(ITEM_ID), eq(requestedVersion), eq("Create version: versionName"));
  }

  @Test
  public void testCreateBasedOn() throws Exception {
    Version requestedVersion = new Version();
    requestedVersion.setBaseId("baseVersionId");

    Version baseVersion = createVersion(requestedVersion.getBaseId(), Certified, UpToDate, false);
    // TODO: 12/13/2017 fix to eq(new Version("baseVersionId")) when version.equals will be fixed
    doReturn(Optional.of(baseVersion)).when(versionDaoMock).get(eq(ITEM_ID), any(Version.class));

    String versionName = "4.0";
    doReturn(versionName)
        .when(versionCalculatorMock).calculate(baseVersion.getName(), VersionCreationMethod.major);

    doReturn(Stream.of(createVersion("1", null, null, false),
        createVersion("2", null, null, false),
        createVersion("3", null, null, false)).collect(Collectors.toList()))
        .when(versionDaoMock).list(ITEM_ID);

    Version version =
        versioningManager.create(ITEM_ID, requestedVersion, VersionCreationMethod.major);
    Assert.assertNotNull(version);
    Assert.assertEquals(version.getName(), versionName);

    verify(versionDaoMock).create(ITEM_ID, requestedVersion);
    verify(asdcItemManager).updateVersionStatus(ITEM_ID, Draft, null);
    verify(versionDaoMock).publish(eq(ITEM_ID), eq(requestedVersion), eq("Create version: 4.0"));
  }

  @Test(expectedExceptions = CoreException.class, expectedExceptionsMessageRegExp =
      "Item itemId: create version failed, a version with the name 2.0 already exist")
  public void testCreateWithExistingName() throws Exception {
    Version version = new Version();
    version.setBaseId("baseVersionId");

    Version baseVersion = createVersion(version.getBaseId(), Certified, UpToDate, false);
    // TODO: 12/13/2017 fix to eq(new Version("baseVersionId")) when version.equals will be fixed
    doReturn(Optional.of(baseVersion)).when(versionDaoMock).get(eq(ITEM_ID), any(Version.class));

    String versionName = "2.0";
    doReturn(versionName)
        .when(versionCalculatorMock).calculate(baseVersion.getName(), VersionCreationMethod.major);

    doReturn(Stream.of(createVersion("1", null, null, false),
        createVersion("2", null, null, false),
        createVersion("3", null, null, false)).collect(Collectors.toList()))
        .when(versionDaoMock).list(ITEM_ID);

    versioningManager.create(ITEM_ID, version, VersionCreationMethod.major);
  }

  @Test(expectedExceptions = CoreException.class, expectedExceptionsMessageRegExp =
      "Item itemId: submit version failed, version versionId is already Certified")
  public void testSubmitCertified() throws Exception {
    Version version = new Version(VERSION_ID);

    Version returnedVersion = createVersion(VERSION_ID, Certified, UpToDate, false);
    doReturn(Optional.of(returnedVersion)).when(versionDaoMock).get(ITEM_ID, version);

    versioningManager.submit(ITEM_ID, version, "Submit message");
  }

  @Test
  public void testSubmit() throws Exception {
    Version version = new Version(VERSION_ID);

    ArgumentCaptor<Version> versionArgumentCaptor = ArgumentCaptor.forClass(Version.class);

    Version returnedVersion = createVersion(VERSION_ID, Draft, UpToDate, false);
    doReturn(Optional.of(returnedVersion)).when(versionDaoMock).get(ITEM_ID, version);

    String submitDescription = "Submit message";
    versioningManager.submit(ITEM_ID, version, submitDescription);

    verify(versionDaoMock).update(eq(ITEM_ID), versionArgumentCaptor.capture());
    Assert.assertEquals(Certified, versionArgumentCaptor.getValue().getStatus());
    verify(versionDaoMock).publish(ITEM_ID, version, submitDescription);
    verify(asdcItemManager).updateVersionStatus(ITEM_ID, Certified, Draft);
  }

  @Test
  public void testPublish() throws Exception {
    Version version = new Version(VERSION_ID);
    String publishDescription = "Publish message";

    versioningManager.publish(ITEM_ID, version, publishDescription);

    verify(versionDaoMock).publish(ITEM_ID, version, publishDescription);
  }

  @Test
  public void testSync() throws Exception {
    Version version = new Version(VERSION_ID);

    versioningManager.sync(ITEM_ID, version);

    verify(versionDaoMock).sync(ITEM_ID, version);
  }

  @Test
  public void testForceSync() throws Exception {
    Version version = new Version(VERSION_ID);

    versioningManager.forceSync(ITEM_ID, version);

    verify(versionDaoMock).forceSync(ITEM_ID, version);
  }

  @Test
  public void testRevert() throws Exception {
    Version version = new Version(VERSION_ID);
    String revisionId = "revisionId";

    versioningManager.revert(ITEM_ID, version, revisionId);

    verify(versionDaoMock).revert(ITEM_ID, version, revisionId);
  }

  @Test
  public void testListRevisions() throws Exception {
    Version version = new Version(VERSION_ID);

    List<Revision> returnedRevisions =
        Stream.of(new Revision(), new Revision()).collect(Collectors.toList());
    doReturn(returnedRevisions)
        .when(versionDaoMock).listRevisions(ITEM_ID, version);

    List<Revision> revisions = versioningManager.listRevisions(ITEM_ID, version);
    Assert.assertEquals(revisions, returnedRevisions);
  }

  private Version createVersion(String id, VersionStatus status,
                                SynchronizationState syncState, boolean dirty) {
    Version version = new Version(id);
    version.setName(id + ".0");
    version.setDescription(id + " desc");
    version.setStatus(status);

    VersionState state = new VersionState();
    state.setSynchronizationState(syncState);
    state.setDirty(dirty);
    version.setState(state);
    return version;
  }

/*
  private static final String USR1 = "usr1";
  private static final String USR2 = "usr2";
  private static final String TYPE1 = "Type1";
*/
/*  private static final String TYPE2 = "Type2";*//*

  private static final String ID1 = "Id1";
*/
/*  private static final String ID2 = "Id2";
  private static final String ID3 = "Id3";
  private static final String TYPE1_TABLE_NAME = "vendor_license_model";
  private static final String TYPE1_ID_NAME = "vlm_id";
  private static final String TYPE1_VERSION_NAME = "version";
  private static final String TYPE2_TABLE_NAME = "feature_group";
  private static final String TYPE2_ID_NAME = "vlm_id";
  private static final String TYPE2_VERSION_NAME = "version";*//*

  private static final Version VERSION0 = new Version(0, 0);
  private static final Version VERSION01 = new Version(0, 1);
  private static final Version VERSION02 = new Version(0, 2);
  private static final Version VERSION10 = new Version(1, 0);
  private static final Version VERSION11 = new Version(1, 1);

  */
/*  private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();

    private static UDTMapper<Version> versionMapper =
        noSqlDb.getMappingManager().udtMapper(Version.class);*//*

  @Mock
  private VersionInfoDao versionInfoDaoMock;
  @Mock
  private VersionInfoDeletedDao versionInfoDeletedDaoMock;
  @InjectMocks
  private VersioningManagerImpl versioningManager;

  @Captor
  private ArgumentCaptor<VersionInfoEntity> versionInfoEntityArg;

  @BeforeMethod
  public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
  }

  */
/*  @BeforeClass
    private void init() {
      versionInfoDaoMock.delete(new VersionInfoEntity(TYPE1, ID1));
      versionInfoDaoMock.delete(new VersionInfoEntity(TYPE1, ID2));
      versionInfoDaoMock.delete(new VersionInfoEntity(TYPE2, ID3));
      String deleteFromType1 = String
          .format("delete from %s where %s=? and %s=?", TYPE1_TABLE_NAME, TYPE1_ID_NAME,
              TYPE1_VERSION_NAME);
      noSqlDb.execute(deleteFromType1, ID1, versionMapper.toUDT(VERSION01));
      noSqlDb.execute(deleteFromType1, ID1, versionMapper.toUDT(VERSION02));
      noSqlDb.execute(deleteFromType1, ID1, versionMapper.toUDT(VERSION11));

      versioningManager.register(TYPE1,
          new VersionableEntityMetadata(TYPE1_TABLE_NAME, TYPE1_ID_NAME, TYPE1_VERSION_NAME));
      versioningManager.register(TYPE2,
          new VersionableEntityMetadata(TYPE2_TABLE_NAME, TYPE2_ID_NAME, TYPE2_VERSION_NAME));
    }*//*


*/
/*  @Test
  public void testRegister() throws Exception {
    VersionableEntityMetadata entityMetadata =
        new VersionableEntityMetadata(TYPE1_TABLE_NAME, TYPE1_ID_NAME, TYPE1_VERSION_NAME);
    versioningManager.register(TYPE1, entityMetadata);

    Map<String, Set<VersionableEntityMetadata>> versionableEntities =
        versionableEntitiesCapture.capture();
    Set<VersionableEntityMetadata> type1Entities = versionableEntities.get(TYPE1);
    Assert.assertNotNull(type1Entities);
    Assert.assertTrue(type1Entities.contains(entityMetadata));
  }*//*


  @Test(expectedExceptions = CoreException.class)
  public void testCreateAlreadyExisting() {
    doReturn(new VersionInfoEntity()).when(versionInfoDaoMock).get(anyObject());
    versioningManager.create(TYPE1, ID1, USR1);
  }

  @Test
  public void testCreate() {
    Version version = versioningManager.create(TYPE1, ID1, USR1);
    Assert.assertEquals(version, VERSION01);

*/
/*    createVersionableEntityRecord(TYPE1_TABLE_NAME, TYPE1_ID_NAME, TYPE1_VERSION_NAME, ID1,
        version);*//*

    verify(versionInfoDaoMock).create(versionInfoEntityArg.capture());
    VersionInfoEntity versionInfoEntity = versionInfoEntityArg.getValue();
    assretVersionInfoEntity(versionInfoEntity, TYPE1, ID1, new Version(0, 0), VERSION01, USR1,
        VersionStatus.Locked, new HashSet<>(), null);
  }

  @Test(expectedExceptions = CoreException.class)
  public void testDeleteNonExisting() {
    versioningManager.delete(TYPE1, ID1, USR1);
  }

  @Test(expectedExceptions = CoreException.class)
  public void testDeleteLocked() {
    mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION0,
        new UserCandidateVersion(USR1, VERSION01), Collections.emptySet(), null);
    versioningManager.delete(TYPE1, ID1, USR1);
  }

  @Test
  public void testDelete() {
    VersionInfoEntity versionInfoEntity = new VersionInfoEntity();
    versionInfoEntity.setStatus(VersionStatus.Draft);
    doReturn(versionInfoEntity).when(versionInfoDaoMock).get(anyObject());

    versioningManager.delete(TYPE1, ID1, USR1);

    verify(versionInfoDaoMock).delete(versionInfoEntity);
    ArgumentCaptor<VersionInfoDeletedEntity> versionInfoDeletedEntityArg =
        ArgumentCaptor.forClass(VersionInfoDeletedEntity.class);
    verify(versionInfoDeletedDaoMock).create(versionInfoDeletedEntityArg.capture());
  }

  @Test(expectedExceptions = CoreException.class)
  public void testUndoDeleteNonExisting() {
    versioningManager.undoDelete(TYPE1, ID1, USR1);
  }

  @Test
  public void testUndoDelete() {
    VersionInfoDeletedEntity versionInfoDeletedEntity = new VersionInfoDeletedEntity();
    versionInfoDeletedEntity.setStatus(VersionStatus.Draft);
    doReturn(versionInfoDeletedEntity).when(versionInfoDeletedDaoMock).get(anyObject());

    versioningManager.undoDelete(TYPE1, ID1, USR1);

    verify(versionInfoDeletedDaoMock).delete(versionInfoDeletedEntity);
    verify(versionInfoDaoMock).create(versionInfoEntityArg.capture());
*/
/*
    VersionInfoDeletedEntity versionInfoDeletedEntity =
        versionInfoDeletedDaoMock.get(new VersionInfoDeletedEntity(TYPE1, ID1));
    Assert.assertNotNull(versionInfoDeletedEntity);

    Map<String, VersionInfo> entitiesInfoMap =
        versioningManager.listDeletedEntitiesVersionInfo(TYPE1, USR2, null);
    Assert.assertEquals(entitiesInfoMap.size(), 1);
    VersionInfoEntity versionInfoEntity = versionInfoDaoMock.get(new VersionInfoEntity(TYPE1, ID1));
    Assert.assertNull(versionInfoEntity);
    versioningManager.undoDelete(TYPE1, ID1, USR1);
    versionInfoEntity = versionInfoDaoMock.get(new VersionInfoEntity(TYPE1, ID1));
    Assert.assertNotNull(versionInfoEntity);*//*

  }

  @Test(expectedExceptions = CoreException.class)
  public void testCheckoutNonExisting() {
    versioningManager.checkout(TYPE1, ID1, USR2);
  }

  @Test(expectedExceptions = CoreException.class)
  public void testCheckoutOnLockedSameUser() {
    mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION0,
        new UserCandidateVersion(USR1, VERSION01), Collections.emptySet(), null);
    versioningManager.checkout(TYPE1, ID1, USR1);
  }

  @Test(expectedExceptions = CoreException.class)
  public void testCheckoutOnLockedOtherUser() {
    mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION0,
        new UserCandidateVersion(USR2, VERSION01), Collections.emptySet(), null);
    versioningManager.checkout(TYPE1, ID1, USR1);
  }

  @Test
  public void testCheckoutOnFinalized() {
    Set<Version> viewableVersions = new HashSet<>();
    viewableVersions.add(VERSION10);
    mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Certified, VERSION10, null, viewableVersions,
        VERSION10);

    Version version = versioningManager.checkout(TYPE1, ID1, USR1);
    Assert.assertEquals(version, VERSION11);

    VersionInfoEntity versionInfoEntity = versionInfoDaoMock.get(new VersionInfoEntity(TYPE1, ID1));
    assretVersionInfoEntity(versionInfoEntity, TYPE1, ID1, VERSION10, VERSION11, USR1,
        VersionStatus.Locked, viewableVersions, VERSION10);
*/
/*
    ResultSet results =
        loadVersionableEntityRecord(TYPE1_TABLE_NAME, TYPE1_ID_NAME, TYPE1_VERSION_NAME, ID1,
            VERSION11);
    Assert.assertTrue(results.iterator().hasNext());*//*

  }

  @Test
  public void testCheckout() {
    Set<Version> viewableVersions = new HashSet<>();
    viewableVersions.add(VERSION01);
    mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Draft, VERSION01, null, viewableVersions,
        null);

    Version version = versioningManager.checkout(TYPE1, ID1, USR1);
    Assert.assertEquals(version, VERSION02);

    verify(versionInfoDaoMock).update(versionInfoEntityArg.capture());
    VersionInfoEntity versionInfoEntity = versionInfoEntityArg.getValue();

    assretVersionInfoEntity(versionInfoEntity, TYPE1, ID1, VERSION01, VERSION02, USR1,
        VersionStatus.Locked, viewableVersions, null);

*/
/*    ResultSet results =
        loadVersionableEntityRecord(TYPE1_TABLE_NAME, TYPE1_ID_NAME, TYPE1_VERSION_NAME, ID1,
            VERSION02);
    Assert.assertTrue(results.iterator().hasNext());*//*

  }

  @Test(expectedExceptions = CoreException.class)
  public void testUndoCheckoutNonExisting() {
    versioningManager.undoCheckout(TYPE1, ID1, USR1);
  }

  @Test(expectedExceptions = CoreException.class)
  public void testUndoCheckoutOnAvailable() {
    Set<Version> viewableVersions = new HashSet<>();
    viewableVersions.add(VERSION01);
    mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Draft, VERSION01, null, viewableVersions,
        null);

    versioningManager.undoCheckout(TYPE1, ID1, USR1);
  }

  @Test(expectedExceptions = CoreException.class)
  public void testUndoCheckouOnFinalized() {
    Set<Version> viewableVersions = new HashSet<>();
    viewableVersions.add(VERSION10);
    mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Certified, VERSION10, null, viewableVersions,
        VERSION10);
    versioningManager.undoCheckout(TYPE1, ID1, USR2);
  }

  @Test(expectedExceptions = CoreException.class)
  public void testUndoCheckoutOnLockedOtherUser() {
    mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION0,
        new UserCandidateVersion(USR2, VERSION01), Collections.emptySet(), null);

    versioningManager.undoCheckout(TYPE1, ID1, USR1);
  }

  @Test
  public void testUndoCheckout() {
    HashSet<Version> viewableVersions = new HashSet<>();
    viewableVersions.add(VERSION01);
    mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION01,
        new UserCandidateVersion(USR1, VERSION02), viewableVersions, null);

    Version version = versioningManager.undoCheckout(TYPE1, ID1, USR1);
    Assert.assertEquals(version, VERSION01);

    VersionInfoEntity versionInfoEntity = versionInfoDaoMock.get(new VersionInfoEntity(TYPE1, ID1));
    assretVersionInfoEntity(versionInfoEntity, TYPE1, ID1, VERSION01, null, null,
        VersionStatus.Draft, viewableVersions, null);

*/
/*    ResultSet results =
        loadVersionableEntityRecord(TYPE1_TABLE_NAME, TYPE1_ID_NAME, TYPE1_VERSION_NAME, ID1,
            VERSION02);
    Assert.assertFalse(results.iterator().hasNext());*//*

  }

  @Test(expectedExceptions = CoreException.class)
  public void testCheckinNonExisting() {
    versioningManager.checkin(TYPE1, ID1, USR1, "");
  }

  @Test(expectedExceptions = CoreException.class)
  public void testCheckinOnAvailable() {
    Set<Version> viewableVersions = new HashSet<>();
    viewableVersions.add(VERSION01);
    mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Draft, VERSION01, null, viewableVersions,
        null);

    versioningManager.checkin(TYPE1, ID1, USR1, "fail checkin");
  }


  @Test(expectedExceptions = CoreException.class)
  public void testCheckinOnFinalized() {
    Set<Version> viewableVersions = new HashSet<>();
    viewableVersions.add(VERSION10);
    mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Certified, VERSION10, null, viewableVersions,
        VERSION10);

    versioningManager.checkin(TYPE1, ID1, USR1, "failed checkin");
  }

  @Test(expectedExceptions = CoreException.class)
  public void testCheckinOnLockedOtherUser() {
    mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION0,
        new UserCandidateVersion(USR2, VERSION01), Collections.emptySet(), null);

    versioningManager.checkin(TYPE1, ID1, USR1, "");
  }

  @Test
  public void testCheckin() {
    HashSet<Version> viewableVersions = new HashSet<>();
    mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION0,
        new UserCandidateVersion(USR1, VERSION01), viewableVersions, null);

    Version version = versioningManager.checkin(TYPE1, ID1, USR1, "checkin 0.1");
    Assert.assertEquals(version, VERSION01);

    verify(versionInfoDaoMock).update(versionInfoEntityArg.capture());
    VersionInfoEntity versionInfoEntity = versionInfoEntityArg.getValue();

    viewableVersions.add(VERSION01);
    assretVersionInfoEntity(versionInfoEntity, TYPE1, ID1, VERSION01, null, null,
        VersionStatus.Draft, viewableVersions, null);
  }

  @Test(expectedExceptions = CoreException.class)
  public void testSubmitNonExisting() {
    versioningManager.submit(TYPE1, ID1, USR2, "failed submit");
  }

  @Test(expectedExceptions = CoreException.class)
  public void testSubmitOnLocked() {
    mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION0,
        new UserCandidateVersion(USR1, VERSION01), Collections.emptySet(), null);
    versioningManager.submit(TYPE1, ID1, USR2, "failed submit");
  }


  @Test(expectedExceptions = CoreException.class)
  public void testSubmitOnFinalized() {
    Set<Version> viewableVersions = new HashSet<>();
    viewableVersions.add(VERSION10);
    mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Certified, VERSION10, null, viewableVersions,
        VERSION10);
    versioningManager.submit(TYPE1, ID1, USR2, "failed submit");
  }

  @Test
  public void testSubmit() {
    Version version32 = new Version(3, 2);
    Version version40 = new Version(4, 0);

    Set<Version> viewableVersions = new HashSet<>();
    viewableVersions.add(VERSION10);
    viewableVersions.add(new Version(2, 0));
    viewableVersions.add(new Version(3, 0));
    viewableVersions.add(new Version(3, 1));
    viewableVersions.add(version32);
    mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Draft, version32, null, viewableVersions,
        new Version(3, 0));

    Version version = versioningManager.submit(TYPE1, ID1, USR1, "submit msg");
    Assert.assertEquals(version, version40);
    viewableVersions.remove(new Version(3, 1));
    viewableVersions.remove(version32);
    viewableVersions.add(version40);

    verify(versionInfoDaoMock).update(versionInfoEntityArg.capture());
    VersionInfoEntity versionInfoEntity = versionInfoEntityArg.getValue();

    assretVersionInfoEntity(versionInfoEntity, TYPE1, ID1, version40, null, null,
        VersionStatus.Certified, viewableVersions, version40);

*/
/*    ResultSet results =
        loadVersionableEntityRecord(TYPE1_TABLE_NAME, TYPE1_ID_NAME, TYPE1_VERSION_NAME, ID1,
            VERSION10);
    Assert.assertTrue(results.iterator().hasNext());*//*

  }

  @Test(expectedExceptions = CoreException.class)
  public void testGetVersionInfoOnNonExistingEntity() {
    versioningManager.getEntityVersionInfo(TYPE1, ID1, USR1, VersionableEntityAction.Read);
  }

  @Test
  public void testGetVersionInfoForReadOnAvailable() {
    Set<Version> viewableVersions = new HashSet<>();
    viewableVersions.add(VERSION01);
    mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Draft, VERSION01, null, viewableVersions,
        null);

    VersionInfo versionInfo =
        versioningManager.getEntityVersionInfo(TYPE1, ID1, USR1, VersionableEntityAction.Read);
    assertVersionInfo(versionInfo, VERSION01, VersionStatus.Draft, null,
        viewableVersions, null);
  }

  @Test(expectedExceptions = CoreException.class)
  public void testGetVersionInfoForWriteOnAvailable() {
    Set<Version> viewableVersions = new HashSet<>();
    viewableVersions.add(VERSION01);
    mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Draft, VERSION01, null, viewableVersions,
        null);

    versioningManager.getEntityVersionInfo(TYPE1, ID1, USR1, VersionableEntityAction.Write);
  }

  @Test
  public void testGetVersionInfoForReadOnLockedSameUser() {
    Set<Version> viewableVersions = new HashSet<>();
    viewableVersions.add(VERSION01);
    mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION01,
        new UserCandidateVersion(USR1, VERSION02), viewableVersions, null);

    VersionInfo versionInfo =
        versioningManager.getEntityVersionInfo(TYPE1, ID1, USR1, VersionableEntityAction.Read);
    viewableVersions.add(VERSION02);
    assertVersionInfo(versionInfo, VERSION02, VersionStatus.Locked, USR1, viewableVersions, null);
  }

  @Test
  public void testGetVersionInfoForReadOnLockedOtherUser() {
    Set<Version> viewableVersions = new HashSet<>();
    viewableVersions.add(VERSION01);
    mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION01,
        new UserCandidateVersion(USR2, VERSION02), viewableVersions, null);

    VersionInfo versionInfo =
        versioningManager.getEntityVersionInfo(TYPE1, ID1, USR1, VersionableEntityAction.Read);
    Assert.assertEquals(versionInfo.getActiveVersion(), VERSION01);
    assertVersionInfo(versionInfo, VERSION01, VersionStatus.Locked, USR2, viewableVersions, null);
  }

  @Test(expectedExceptions = CoreException.class)
  public void testGetVersionInfoForWriteOnLockedOtherUser() {
    Set<Version> viewableVersions = new HashSet<>();
    viewableVersions.add(VERSION01);
    mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION01,
        new UserCandidateVersion(USR2, VERSION02), viewableVersions, null);

    versioningManager.getEntityVersionInfo(TYPE1, ID1, USR1, VersionableEntityAction.Write);
  }

  @Test
  public void testGetVersionInfoForWriteOnLockedSameUser() {
    Set<Version> viewableVersions = new HashSet<>();
    viewableVersions.add(VERSION01);
    mockVersionInfoEntity(TYPE1, ID1, VersionStatus.Locked, VERSION01,
        new UserCandidateVersion(USR1, VERSION02), viewableVersions, null);

    VersionInfo versionInfo =
        versioningManager.getEntityVersionInfo(TYPE1, ID1, USR1, VersionableEntityAction.Write);
    viewableVersions.add(VERSION02);
    assertVersionInfo(versionInfo, VERSION02, VersionStatus.Locked, USR1, viewableVersions, null);
  }

*/
/*  private void createVersionableEntityRecord(String tableName, String idName, String versionName,
                                             String id, Version version) {
    noSqlDb.execute(
        String.format("insert into %s (%s,%s) values (?,?)", tableName, idName, versionName), id,
        versionMapper.toUDT(version));
  }

  private ResultSet loadVersionableEntityRecord(String tableName, String idName, String versionName,
                                                String id, Version version) {
    return noSqlDb.execute(
        String.format("select * from %s where %s=? and %s=?", tableName, idName, versionName), id,
        versionMapper.toUDT(version));
  }*//*



  private static void assretVersionInfoEntity(VersionInfoEntity actual, String entityType,
                                              String entityId, Version activeVersion,
                                              Version candidateVersion, String candidateUser,
                                              VersionStatus status, Set<Version> viewbleVersions,
                                              Version latestFinalVersion) {
    Assert.assertNotNull(actual);
    Assert.assertEquals(actual.getEntityType(), entityType);
    Assert.assertEquals(actual.getEntityId(), entityId);
    Assert.assertEquals(actual.getActiveVersion(), activeVersion);
    if (candidateVersion != null && candidateUser != null) {
      Assert.assertEquals(actual.getCandidate().getVersion(), candidateVersion);
      Assert.assertEquals(actual.getCandidate().getUser(), candidateUser);
    } else {
      Assert.assertNull(actual.getCandidate());
    }
    Assert.assertEquals(actual.getStatus(), status);
    Assert.assertEquals(actual.getViewableVersions().size(), viewbleVersions.size());
    Assert.assertEquals(actual.getViewableVersions(), viewbleVersions);
    Assert.assertEquals(actual.getLatestFinalVersion(), latestFinalVersion);
  }

  private static void assertVersionInfo(VersionInfo actual, Version activeVersion,
                                        VersionStatus status, String lockingUser,
                                        Set<Version> viewableVersions, Version latestFinalVersion) {
    Assert.assertNotNull(actual);
    Assert.assertEquals(actual.getActiveVersion(), activeVersion);
    Assert.assertEquals(actual.getStatus(), status);
    Assert.assertEquals(actual.getLockingUser(), lockingUser);
    Assert.assertEquals(actual.getViewableVersions().size(), viewableVersions.size());
    Assert.assertEquals(actual.getViewableVersions(), viewableVersions);
    Assert.assertEquals(actual.getLatestFinalVersion(), latestFinalVersion);
  }

  private VersionInfoEntity mockVersionInfoEntity(String entityType, String entityId,
                                                  VersionStatus status, Version activeVersion,
                                                  UserCandidateVersion candidate,
                                                  Set<Version> viewableVersions,
                                                  Version latestFinalVersion) {
    VersionInfoEntity mock = new VersionInfoEntity();
    mock.setEntityType(entityType);
    mock.setEntityId(entityId);
    mock.setStatus(status);
    mock.setActiveVersion(activeVersion);
    mock.setCandidate(candidate);
    mock.setViewableVersions(viewableVersions);
    mock.setLatestFinalVersion(latestFinalVersion);

    doReturn(mock).when(versionInfoDaoMock).get(anyObject());
    return mock;
  }
*/
}