Change code in appc dispatcher for new LCMs in R6
[appc.git] / appc-dispatcher / appc-license-manager / appc-license-manager-core / src / test / java / org / onap / appc / licmgr / impl / LicenseManagerImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.licmgr.impl;
25
26
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;
31
32 import java.io.InputStream;
33 import java.util.Collections;
34 import java.util.HashMap;
35 import org.junit.Before;
36 import java.util.Map;
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;
45
46 @RunWith(MockitoJUnitRunner.class)
47 public class LicenseManagerImplTest {
48
49     private static final String VNF_TYPE = "default_vnf_type";
50     private static final String VNF_VERSION = "default_vnf_version";
51     private LicenseManagerImpl licenseManager;
52
53     @Mock
54     private LicenseDataAccessService licenseDataAccessService;
55
56     @Before
57     public void setUp() {
58         licenseManager = new LicenseManagerImpl();
59         licenseManager.setDAService(licenseDataAccessService);
60     }
61
62     @Test
63     public void retrieveLicenseModel_shouldThrowException_whenRetrieveLicenseModelDataIsEmpty() {
64
65         // GIVEN
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());
70
71         // WHEN THEN
72         assertThatExceptionOfType(DataAccessException.class)
73                 .isThrownBy(() -> licenseManager.retrieveLicenseModel(VNF_TYPE, VNF_VERSION))
74                 .withMessage(expectedMessage);
75     }
76
77     @Test
78     public void retrieveLicenseModel_shouldThrowException_whenXmlIsMalformed() {
79
80         // GIVEN
81         String malformedXml = "xyz";
82
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);
87
88         // WHEN THEN
89         assertThatExceptionOfType(DataAccessException.class)
90                 .isThrownBy(() -> licenseManager.retrieveLicenseModel(VNF_TYPE, VNF_VERSION));
91     }
92
93     @Test
94     public void retrieveLicenseModel_shouldReturnCorrectLicenseModel_whenCorrectXmlExists() {
95
96         // GIVEN
97         String expectedEntitlementPool = "default_entitlement_pool_uuid";
98         String expectedKeyGroup = "default_lkg_uuid";
99
100         ClassLoader classLoader = getClass().getClassLoader();
101         InputStream inputStream = classLoader.getResourceAsStream("test-vf-license-model.xml");
102         String correctlyFormedXml = new Scanner(inputStream).useDelimiter("\\A").next();
103
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);
108
109         // WHEN
110         LicenseModel licenseModel = licenseManager.retrieveLicenseModel(VNF_TYPE, VNF_VERSION);
111
112         // THEN
113         assertEquals(expectedEntitlementPool, licenseModel.getEntitlementPoolUuid());
114         assertEquals(expectedKeyGroup, licenseModel.getLicenseKeyGroupUuid());
115     }
116 }