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