2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Modifications Copyright (C) 2018 Nokia
6 * ================================================================================
7 * Modifications Copyright (C) 2019 AT&T Intellectual Property
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.licmgr.impl;
27 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
28 import static org.junit.Assert.assertEquals;
29 import static org.mockito.BDDMockito.given;
30 import static org.onap.appc.licmgr.Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_CONTENT;
32 import java.io.InputStream;
33 import java.util.Collections;
34 import java.util.HashMap;
35 import org.junit.Before;
37 import java.util.Scanner;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.runners.MockitoJUnitRunner;
42 import org.onap.appc.licmgr.LicenseDataAccessService;
43 import org.onap.appc.licmgr.exception.DataAccessException;
44 import org.onap.appc.licmgr.objects.LicenseModel;
46 @RunWith(MockitoJUnitRunner.class)
47 public class LicenseManagerImplTest {
49 private static final String VNF_TYPE = "default_vnf_type";
50 private static final String VNF_VERSION = "default_vnf_version";
51 private LicenseManagerImpl licenseManager;
54 private LicenseDataAccessService licenseDataAccessService;
58 licenseManager = new LicenseManagerImpl();
59 licenseManager.setDAService(licenseDataAccessService);
63 public void retrieveLicenseModel_shouldThrowException_whenRetrieveLicenseModelDataIsEmpty() {
66 String expectedMessage =
67 String.format("License model not found for vnfType='%s' and vnfVersion='%s'", VNF_TYPE, VNF_VERSION);
68 given(licenseDataAccessService.retrieveLicenseModelData(VNF_TYPE, VNF_VERSION))
69 .willReturn(Collections.emptyMap());
72 assertThatExceptionOfType(DataAccessException.class)
73 .isThrownBy(() -> licenseManager.retrieveLicenseModel(VNF_TYPE, VNF_VERSION))
74 .withMessage(expectedMessage);
78 public void retrieveLicenseModel_shouldThrowException_whenXmlIsMalformed() {
81 String malformedXml = "xyz";
83 Map<String, String> licenseModelData = new HashMap<>();
84 licenseModelData.put(ARTIFACT_CONTENT.name(), malformedXml);
85 given(licenseDataAccessService.retrieveLicenseModelData(VNF_TYPE, VNF_VERSION))
86 .willReturn(licenseModelData);
89 assertThatExceptionOfType(DataAccessException.class)
90 .isThrownBy(() -> licenseManager.retrieveLicenseModel(VNF_TYPE, VNF_VERSION));
94 public void retrieveLicenseModel_shouldReturnCorrectLicenseModel_whenCorrectXmlExists() {
97 String expectedEntitlementPool = "default_entitlement_pool_uuid";
98 String expectedKeyGroup = "default_lkg_uuid";
100 ClassLoader classLoader = getClass().getClassLoader();
101 InputStream inputStream = classLoader.getResourceAsStream("test-vf-license-model.xml");
102 String correctlyFormedXml = new Scanner(inputStream).useDelimiter("\\A").next();
104 Map<String, String> licenseModelData = new HashMap<>();
105 licenseModelData.put(ARTIFACT_CONTENT.name(), correctlyFormedXml);
106 given(licenseDataAccessService.retrieveLicenseModelData(VNF_TYPE, VNF_VERSION))
107 .willReturn(licenseModelData);
110 LicenseModel licenseModel = licenseManager.retrieveLicenseModel(VNF_TYPE, VNF_VERSION);
113 assertEquals(expectedEntitlementPool, licenseModel.getEntitlementPoolUuid());
114 assertEquals(expectedKeyGroup, licenseModel.getLicenseKeyGroupUuid());