2  * Copyright © 2016-2018 European Support Limited
 
   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
 
   8  *   http://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  17 package org.openecomp.sdc.vendorlicense;
 
  19 import org.junit.After;
 
  20 import org.junit.Assert;
 
  21 import org.junit.Before;
 
  22 import org.junit.Test;
 
  23 import org.mockito.InjectMocks;
 
  24 import org.mockito.Mock;
 
  25 import org.mockito.MockitoAnnotations;
 
  26 import org.mockito.Spy;
 
  27 import org.openecomp.sdc.common.errors.CoreException;
 
  28 import org.openecomp.sdc.vendorlicense.dao.LimitDao;
 
  29 import org.openecomp.sdc.vendorlicense.dao.types.AggregationFunction;
 
  30 import org.openecomp.sdc.vendorlicense.dao.types.LimitEntity;
 
  31 import org.openecomp.sdc.vendorlicense.dao.types.LimitType;
 
  32 import org.openecomp.sdc.vendorlicense.errors.VendorLicenseErrorCodes;
 
  33 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
 
  34 import org.openecomp.sdc.vendorlicense.impl.VendorLicenseManagerImpl;
 
  35 import org.openecomp.sdc.versioning.dao.types.Version;
 
  36 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
 
  37 import org.openecomp.sdc.versioning.types.VersionInfo;
 
  38 import java.util.ArrayList;
 
  39 import java.util.Arrays;
 
  40 import java.util.Collection;
 
  41 import java.util.List;
 
  43 import static org.mockito.Matchers.anyObject;
 
  44 import static org.mockito.Mockito.doReturn;
 
  45 import static org.mockito.Mockito.verify;
 
  47 public class LimitTest {
 
  49   private final String USER1 = "limitTestUser1";
 
  50   private final String LT1_NAME = "LT1 name";
 
  52   private static final String VLM_ID = "VLM_ID";
 
  53   private static final Version VERSION = new Version(0, 1);
 
  54   private static final String EPLKG_ID = "ID";
 
  55   private static final String LIMIT1_ID = "limit1";
 
  56   private static final String LIMIT2_ID = "limit2";
 
  59   private VendorLicenseFacade vendorLicenseFacade;
 
  62   private LimitDao limitDao;
 
  66   private VendorLicenseManagerImpl vendorLicenseManagerImpl;
 
  68   public static LimitEntity createLimitEntity(String name, LimitType type, String description,
 
  69                                               Version version, String metric,
 
  70                                               AggregationFunction aggregationFunction, int unit,
 
  72     LimitEntity limitEntity = new LimitEntity();
 
  73     limitEntity.setName(name);
 
  74     limitEntity.setType(type);
 
  75     limitEntity.setDescription(description);
 
  76     limitEntity.setVersion(version);
 
  77     limitEntity.setMetric(metric);
 
  78     limitEntity.setAggregationFunction(aggregationFunction);
 
  79     limitEntity.setUnit(String.valueOf(unit));
 
  80     limitEntity.setTime(time);
 
  85   public void setUp() throws Exception {
 
  86     MockitoAnnotations.openMocks(this);
 
  90   public void tearDown(){
 
  91     vendorLicenseManagerImpl = null;
 
  95   public void testUpdateLimit() {
 
  96     Version version = new Version();
 
  97     LimitEntity limitEntity1 = createLimitEntity(LT1_NAME, LimitType.Vendor, "string", version,
 
  98         "Core", AggregationFunction.Average, 10, "Hour");
 
  99     LimitEntity limitEntity2 = createLimitEntity(LT1_NAME, LimitType.Vendor, "string", version,
 
 100         "Tokens", AggregationFunction.Peak, 12, "Month");
 
 101     VersionInfo info = new VersionInfo();
 
 102     info.getViewableVersions().add(version);
 
 103     info.setActiveVersion(version);
 
 105     /*doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(), anyObject(), anyObject());*/
 
 106     doReturn(true).when(limitDao).isLimitPresent(anyObject());
 
 107     doReturn(limitEntity1).when(limitDao).get(anyObject());
 
 109     List<LimitEntity> limitEntityList = new ArrayList<>();
 
 110     limitEntityList.add(limitEntity1);
 
 111     limitEntityList.add(limitEntity2);
 
 112     limitEntity1.setId("1234");
 
 113     limitEntity2.setId("1234");
 
 114     doReturn(limitEntityList).when(vendorLicenseFacade)
 
 115         .listLimits(anyObject(), anyObject(), anyObject());
 
 117     vendorLicenseManagerImpl.updateLimit(limitEntity2);
 
 119     verify(vendorLicenseFacade).updateLimit(anyObject());
 
 123   public void testUpdateLimitErrorWithSameNameType() {
 
 125       Version version = new Version();
 
 126       LimitEntity limitEntity1 = createLimitEntity(LT1_NAME, LimitType.Vendor, "string", version,
 
 127           "Core", AggregationFunction.Average, 10, "Hour");
 
 128       LimitEntity limitEntity2 = createLimitEntity(LT1_NAME, LimitType.Vendor, "string", version,
 
 129           "Tokens", AggregationFunction.Peak, 12, "Month");
 
 130       VersionInfo info = new VersionInfo();
 
 131       info.getViewableVersions().add(version);
 
 132       info.setActiveVersion(version);
 
 134 /*      doReturn(info).when(vendorLicenseFacade)
 
 135           .getVersionInfo(anyObject(), anyObject(), anyObject());*/
 
 136       doReturn(limitEntity1).when(limitDao).get(anyObject());
 
 138       List<LimitEntity> limitEntityList = new ArrayList<>();
 
 139       limitEntityList.add(limitEntity1);
 
 140       limitEntityList.add(limitEntity2);
 
 141       limitEntity1.setId("1234");
 
 142       limitEntity2.setId("9632");
 
 143       doReturn(limitEntityList).when(vendorLicenseFacade)
 
 144           .listLimits(anyObject(), anyObject(), anyObject());
 
 146       vendorLicenseManagerImpl.updateLimit(limitEntity2);
 
 148     } catch (CoreException exception) {
 
 149       Assert.assertEquals(exception.code().id(),
 
 150           VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
 
 155   public void testDeleteLimit() {
 
 156     Version version = new Version();
 
 157     LimitEntity limitEntity = createLimitEntity(LT1_NAME, LimitType.Vendor, "string", version,
 
 158         "Core", AggregationFunction.Average, 10, "Hour");
 
 159     VersionInfo info = new VersionInfo();
 
 160     info.getViewableVersions().add(version);
 
 161     info.setActiveVersion(version);
 
 163     /*doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(), anyObject(), anyObject());*/
 
 164     doReturn(true).when(limitDao).isLimitPresent(anyObject());
 
 165     doReturn(limitEntity).when(limitDao).get(anyObject());
 
 167     List<LimitEntity> limitEntityList = new ArrayList<>();
 
 168     limitEntityList.add(limitEntity);
 
 169     limitEntity.setId("1234");
 
 171     vendorLicenseManagerImpl.deleteLimit(limitEntity);
 
 173     verify(vendorLicenseManagerImpl).deleteLimit(anyObject());
 
 177   public void testUpdateLimitErrorWithInvalidId() {
 
 179       Version version = new Version();
 
 180       LimitEntity limitEntity1 = createLimitEntity(LT1_NAME, LimitType.Vendor, "string", version,
 
 181           "Core", AggregationFunction.Average, 10, "Hour");
 
 182       LimitEntity limitEntity2 = createLimitEntity(LT1_NAME, LimitType.Vendor, "string", version,
 
 183           "Tokens", AggregationFunction.Peak, 12, "Month");
 
 184       VersionInfo info = new VersionInfo();
 
 185       info.getViewableVersions().add(version);
 
 186       info.setActiveVersion(version);
 
 188 /*      doReturn(info).when(vendorLicenseFacade)
 
 189           .getVersionInfo(anyObject(), anyObject(), anyObject());*/
 
 190       doReturn(null).when(limitDao).get(anyObject());
 
 192       vendorLicenseManagerImpl.updateLimit(limitEntity2);
 
 194     } catch (CoreException exception) {
 
 195       Assert.assertEquals(exception.code().id(),
 
 196           VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
 
 201   public void testList() {
 
 202     doReturn(Arrays.asList(
 
 203         createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID),
 
 204         createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT2_ID)))
 
 205         .when(vendorLicenseFacade).listLimits(VLM_ID, VERSION, EPLKG_ID);
 
 207     final Collection<LimitEntity> limits =
 
 208         vendorLicenseManagerImpl.listLimits(VLM_ID, VERSION, EPLKG_ID);
 
 209     Assert.assertEquals(limits.size(), 2);
 
 210     for (LimitEntity limit : limits) {
 
 211       Assert.assertEquals(limit.getName(),
 
 212           LIMIT1_ID.equals(limit.getId()) ? LIMIT1_ID + " name" : LIMIT2_ID + " name");
 
 217   public void testCreateLimit() {
 
 218     LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
 
 219     VersionInfo info = new VersionInfo();
 
 220     info.getViewableVersions().add(VERSION);
 
 221     info.setActiveVersion(VERSION);
 
 223     /*doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(), anyObject(), anyObject());*/
 
 225     vendorLicenseManagerImpl.createLimit(expected);
 
 226     verify(vendorLicenseFacade).createLimit(expected);
 
 230   public void testCreateWithDuplicateName() {
 
 231     LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
 
 232     expected.setType(LimitType.Vendor);
 
 234     LimitEntity expectedDiffName = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT2_ID);
 
 235     expectedDiffName.setName(LIMIT1_ID + " name");
 
 236     expectedDiffName.setType(LimitType.Vendor);
 
 238     List<LimitEntity> vfcImageList = new ArrayList<LimitEntity>();
 
 239     vfcImageList.add(expectedDiffName);
 
 240     doReturn(vfcImageList).when(vendorLicenseFacade)
 
 241         .listLimits(anyObject(), anyObject(), anyObject());
 
 243     VersionInfo info = new VersionInfo();
 
 244     info.getViewableVersions().add(VERSION);
 
 245     info.setActiveVersion(VERSION);
 
 247 /*    doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(), anyObject(), anyObject());*/
 
 250       vendorLicenseManagerImpl.createLimit(expected);
 
 252     } catch (CoreException ex) {
 
 253       Assert.assertEquals(ex.code().id(),
 
 254           VendorLicenseErrorCodes.DUPLICATE_LIMIT_NAME_NOT_ALLOWED);
 
 259   public void testGetNonExistingLimitId_negative() {
 
 260     LimitEntity limit = createLimit(VLM_ID, VERSION, EPLKG_ID, "non existing limit id");
 
 261     VersionInfo info = new VersionInfo();
 
 262     info.getViewableVersions().add(VERSION);
 
 263     info.setActiveVersion(VERSION);
 
 265     /*doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(), anyObject(), anyObject());*/
 
 268       vendorLicenseManagerImpl.getLimit(limit);
 
 270     } catch (CoreException exception) {
 
 271       Assert.assertEquals(exception.code().id(),
 
 272           VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
 
 277   public void testGet() {
 
 278     LimitEntity expected = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
 
 279     expected.setType(LimitType.Vendor);
 
 280     expected.setValue(String.valueOf(100));
 
 281     expected.setUnit(String.valueOf(10));
 
 282     expected.setAggregationFunction(AggregationFunction.Average);
 
 283     expected.setMetric("BWTH");
 
 284     expected.setTime("Day");
 
 286     doReturn(true).when(limitDao).isLimitPresent(anyObject());
 
 287     doReturn(expected).when(limitDao).get(anyObject());
 
 288     VersionInfo info = new VersionInfo();
 
 289     info.getViewableVersions().add(VERSION);
 
 290     info.setActiveVersion(VERSION);
 
 292     /*doReturn(info).when(vendorLicenseFacade).getVersionInfo(anyObject(), anyObject(), anyObject());*/
 
 294     LimitEntity actual = createLimit(VLM_ID, VERSION, EPLKG_ID, LIMIT1_ID);
 
 295     vendorLicenseManagerImpl.getLimit(actual);
 
 296     Assert.assertEquals(actual.getId(), expected.getId());
 
 297     Assert.assertEquals(actual.getName(), expected.getName());
 
 298     Assert.assertEquals(actual.getUnit(), expected.getUnit());
 
 299     Assert.assertEquals(actual.getValue(), expected.getValue());
 
 300     Assert.assertEquals(actual.getAggregationFunction().name(), expected.getAggregationFunction()
 
 302     Assert.assertEquals(actual.getMetric(), expected.getMetric());
 
 306   static LimitEntity createLimit(String vlmId, Version version, String epLkgId, String limitId) {
 
 307     LimitEntity limitEntity = new LimitEntity(vlmId, version, epLkgId, limitId);
 
 308     limitEntity.setName(limitId + " name");
 
 309     limitEntity.setDescription(limitId + " desc");
 
 310     limitEntity.setVersion(version);
 
 311     limitEntity.setMetric("BWTH");
 
 312     limitEntity.setAggregationFunction(AggregationFunction.Average);
 
 313     limitEntity.setUnit(String.valueOf(10));
 
 314     limitEntity.setTime("Day");
 
 315     limitEntity.setValue(String.valueOf(100));