2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Modifications Copyright (C) 2018 Nokia
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.
19 * ============LICENSE_END=========================================================
22 package org.onap.appc.licmgr.impl;
25 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
26 import static org.junit.Assert.assertEquals;
27 import static org.mockito.BDDMockito.given;
28 import static org.onap.appc.licmgr.Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_CONTENT;
30 import java.io.InputStream;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import org.junit.Before;
35 import java.util.Scanner;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mock;
39 import org.mockito.runners.MockitoJUnitRunner;
40 import org.onap.appc.licmgr.LicenseDataAccessService;
41 import org.onap.appc.licmgr.exception.DataAccessException;
42 import org.onap.appc.licmgr.objects.LicenseModel;
44 @RunWith(MockitoJUnitRunner.class)
45 public class LicenseManagerImplTest {
47 private static final String VNF_TYPE = "default_vnf_type";
48 private static final String VNF_VERSION = "default_vnf_version";
49 private LicenseManagerImpl licenseManager;
52 private LicenseDataAccessService licenseDataAccessService;
56 licenseManager = new LicenseManagerImpl();
57 licenseManager.setDAService(licenseDataAccessService);
61 public void retrieveLicenseModel_shouldThrowException_whenRetrieveLicenseModelDataIsEmpty() {
64 String expectedMessage = String
65 .format("License model not found for vnfType='%s' and vnfVersion='%s'", VNF_TYPE, VNF_VERSION);
66 given(licenseDataAccessService.retrieveLicenseModelData(VNF_TYPE, VNF_VERSION))
67 .willReturn(Collections.emptyMap());
70 assertThatExceptionOfType(DataAccessException.class)
71 .isThrownBy(() -> licenseManager.retrieveLicenseModel(VNF_TYPE, VNF_VERSION))
72 .withMessage(expectedMessage);
76 public void retrieveLicenseModel_shouldThrowException_whenXmlIsMalformed() {
79 String malformedXml = "xyz";
81 Map<String, String> licenseModelData = new HashMap<>();
82 licenseModelData.put(ARTIFACT_CONTENT.name(), malformedXml);
83 given(licenseDataAccessService.retrieveLicenseModelData(VNF_TYPE, VNF_VERSION))
84 .willReturn(licenseModelData);
87 assertThatExceptionOfType(DataAccessException.class)
88 .isThrownBy(() -> licenseManager.retrieveLicenseModel(VNF_TYPE, VNF_VERSION));
92 public void retrieveLicenseModel_shouldReturnCorrectLicenseModel_whenCorrectXmlExists() {
95 String expectedEntitlementPool = "default_entitlement_pool_uuid";
96 String expectedKeyGroup = "default_lkg_uuid";
98 ClassLoader classLoader = getClass().getClassLoader();
99 InputStream inputStream = classLoader.getResourceAsStream("test-vf-license-model.xml");
100 String correctlyFormedXml = new Scanner(inputStream).useDelimiter("\\A").next();
102 Map<String, String> licenseModelData = new HashMap<>();
103 licenseModelData.put(ARTIFACT_CONTENT.name(), correctlyFormedXml);
104 given(licenseDataAccessService.retrieveLicenseModelData(VNF_TYPE, VNF_VERSION))
105 .willReturn(licenseModelData);
108 LicenseModel licenseModel = licenseManager.retrieveLicenseModel(VNF_TYPE, VNF_VERSION);
111 assertEquals(expectedEntitlementPool, licenseModel.getEntitlementPoolUuid());
112 assertEquals(expectedKeyGroup, licenseModel.getLicenseKeyGroupUuid());