2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * 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.junit.Assert.assertNull;
28 import static org.mockito.BDDMockito.given;
29 import static org.onap.appc.licmgr.Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_CONTENT;
31 import java.util.Collections;
33 import java.util.HashMap;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.runners.MockitoJUnitRunner;
39 import org.onap.appc.licmgr.LicenseDataAccessService;
40 import org.onap.appc.licmgr.exception.DataAccessException;
41 import org.onap.appc.licmgr.objects.LicenseModel;
43 @RunWith(MockitoJUnitRunner.class)
44 public class LicenseManagerImplTest {
46 private static final String VNF_TYPE = "default_vnf_type";
47 private static final String VNF_VERSION = "default_vnf_version";
48 private LicenseManagerImpl licenseManager;
51 private LicenseDataAccessService licenseDataAccessService;
55 licenseManager = new LicenseManagerImpl();
59 public void retrieveLicenseModel_shouldThrowException_whenRetrieveLicenseModelDataIsEmpty() {
62 String expectedMessage = String
63 .format("License model not found for vnfType='%s' and vnfVersion='%s'", VNF_TYPE, VNF_VERSION);
64 given(licenseDataAccessService.retrieveLicenseModelData(VNF_TYPE, VNF_VERSION))
65 .willReturn(Collections.emptyMap());
68 licenseManager.setDAService(licenseDataAccessService);
70 assertThatExceptionOfType(DataAccessException.class)
71 .isThrownBy(() -> licenseManager.retrieveLicenseModel(VNF_TYPE, VNF_VERSION))
72 .withMessage(expectedMessage);
76 public void retrieveLicenseModel_shouldReturnLicenseModelWithNullValues_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 licenseManager.setDAService(licenseDataAccessService);
88 LicenseModel licenseModel = licenseManager.retrieveLicenseModel(VNF_TYPE, VNF_VERSION);
91 assertNull(licenseModel.getEntitlementPoolUuid());
92 assertNull(licenseModel.getLicenseKeyGroupUuid());
96 public void retrieveLicenseModel_shouldReturnCorrectLicenseModel_whenCorrectXmlExists() {
99 String expectedEntitlementPool = "default_entitlement_pool";
100 String expectedKeyGroup = "default_key_group";
102 String correctlyFormedXml = String.format("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
104 + "<entitlement-pool-uuid>%s</entitlement-pool-uuid>\n"
105 + "<license-key-group-uuid>%s</license-key-group-uuid>\n"
106 + "</parent>", expectedEntitlementPool, expectedKeyGroup);
108 Map<String, String> licenseModelData = new HashMap<>();
109 licenseModelData.put(ARTIFACT_CONTENT.name(), correctlyFormedXml);
110 given(licenseDataAccessService.retrieveLicenseModelData(VNF_TYPE, VNF_VERSION))
111 .willReturn(licenseModelData);
114 licenseManager.setDAService(licenseDataAccessService);
115 LicenseModel licenseModel = licenseManager.retrieveLicenseModel(VNF_TYPE, VNF_VERSION);
118 assertEquals(expectedEntitlementPool, licenseModel.getEntitlementPoolUuid());
119 assertEquals(expectedKeyGroup, licenseModel.getLicenseKeyGroupUuid());