3ea17525f5fc302f0ef98931c8acebbc940cd9b1
[sdc.git] / openecomp-be / lib / openecomp-healing-lib / openecomp-sdc-healing-impl / src / test / java / org / openecomp / sdc / healing / healers / ManufacturerReferenceNumberHealerTest.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.healing.healers;
18
19 import static java.lang.Boolean.FALSE;
20 import static java.lang.Boolean.TRUE;
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.Mockito.doNothing;
23 import static org.mockito.Mockito.times;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDao;
33 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
34 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
35 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
36 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
37 import org.openecomp.sdc.versioning.dao.types.Version;
38
39 import java.util.ArrayList;
40 import java.util.Arrays;
41 import java.util.Collection;
42 import java.util.HashSet;
43 import java.util.Set;
44
45 public class ManufacturerReferenceNumberHealerTest {
46
47   @Mock
48   private VendorLicenseFacade vendorLicenseFacade;
49   @Mock
50   private FeatureGroupDao featureGroupDao;
51   private ManufacturerReferenceNumberHealer manufacturerReferenceNumberHealer;
52
53   private final String ITEM_ID = "ITEM_ID";
54   private final Version version = new Version();
55
56   @Before
57   public void init(){
58     MockitoAnnotations.initMocks(this);
59     manufacturerReferenceNumberHealer = new ManufacturerReferenceNumberHealer
60         (vendorLicenseFacade, featureGroupDao);
61   }
62
63   @Test
64   public void testIsHealingNeeded_Positive() {
65     Collection<EntitlementPoolEntity> entitlementPoolEntities = new ArrayList<>();
66     entitlementPoolEntities.add(new EntitlementPoolEntity(ITEM_ID, version, ""));
67
68     when(vendorLicenseFacade.listEntitlementPools(ITEM_ID, version )).thenReturn(entitlementPoolEntities);
69     Assert.assertEquals(TRUE, manufacturerReferenceNumberHealer.isHealingNeeded("ITEM_ID", version));
70   }
71
72   @Test
73   public void testIsHealingNeeded_Negative() {
74     Collection<EntitlementPoolEntity> entitlementPoolEntities = new ArrayList<>();
75     EntitlementPoolEntity entitlementPoolEntity = new EntitlementPoolEntity(ITEM_ID, version, "");
76     entitlementPoolEntity.setManufacturerReferenceNumber("MRN");
77     entitlementPoolEntities.add(entitlementPoolEntity);
78
79     when(vendorLicenseFacade.listEntitlementPools(ITEM_ID, version )).thenReturn(entitlementPoolEntities);
80     Assert.assertEquals(FALSE, manufacturerReferenceNumberHealer.isHealingNeeded("ITEM_ID", version));
81   }
82
83   @Test
84   public void testHeal() throws Exception {
85
86     Collection<EntitlementPoolEntity> entitlementPoolEntities = getEntitlementPoolEntities();
87     when(vendorLicenseFacade.listEntitlementPools(ITEM_ID, version)).thenReturn(entitlementPoolEntities);
88     doNothing().when(vendorLicenseFacade).updateEntitlementPool(any());
89     Collection<LicenseKeyGroupEntity> licenseKeyGroupEntities = getLicenseKeyGroupEntities();
90     when(vendorLicenseFacade.listLicenseKeyGroups(ITEM_ID, version)).thenReturn
91         (licenseKeyGroupEntities);
92     doNothing().when(vendorLicenseFacade).updateLicenseKeyGroup(any());
93
94     FeatureGroupEntity featureGroupEntity = new FeatureGroupEntity();
95     when(vendorLicenseFacade.getFeatureGroup(any())).thenReturn(featureGroupEntity);
96
97     Collection<FeatureGroupEntity> featureGroupEntities = new ArrayList<>();
98     featureGroupEntities.add(featureGroupEntity);
99     when(vendorLicenseFacade.listFeatureGroups(ITEM_ID, version)).thenReturn
100         (featureGroupEntities);
101     doNothing().when(featureGroupDao).update(any());
102
103     manufacturerReferenceNumberHealer.heal(ITEM_ID, version);
104
105     verify(vendorLicenseFacade, times(1)).updateEntitlementPool(any());
106     verify(vendorLicenseFacade,times(1)).updateLicenseKeyGroup(any());
107   }
108
109   private Collection<EntitlementPoolEntity> getEntitlementPoolEntities() {
110     Set<String> oneReferencingFeatureGroups = new HashSet<String>(Arrays.asList("1"));
111     Collection<EntitlementPoolEntity> entitlementPoolEntities = new ArrayList<>();
112     EntitlementPoolEntity entitlementPoolEntity = new EntitlementPoolEntity();
113     entitlementPoolEntity.setReferencingFeatureGroups(oneReferencingFeatureGroups);
114     entitlementPoolEntities.add(entitlementPoolEntity);
115     return entitlementPoolEntities;
116   }
117
118   private Collection<LicenseKeyGroupEntity> getLicenseKeyGroupEntities() {
119     Set<String> oneReferencingFeatureGroups = new HashSet<String>(Arrays.asList("1"));
120     Collection<LicenseKeyGroupEntity> licenseKeyGroupEntities = new ArrayList<>();
121     LicenseKeyGroupEntity licenseKeyGroupEntity = new LicenseKeyGroupEntity();
122     licenseKeyGroupEntity.setReferencingFeatureGroups(oneReferencingFeatureGroups);
123     licenseKeyGroupEntities.add(licenseKeyGroupEntity);
124     return  licenseKeyGroupEntities;
125   }
126 }