Add additional junits for LicenseManagerImpl
[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  * 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.junit.Assert.assertNull;
28 import static org.mockito.BDDMockito.given;
29 import static org.onap.appc.licmgr.Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_CONTENT;
30
31 import java.util.Collections;
32 import java.util.Map;
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;
42
43 @RunWith(MockitoJUnitRunner.class)
44 public class LicenseManagerImplTest {
45
46     private static final String VNF_TYPE = "default_vnf_type";
47     private static final String VNF_VERSION = "default_vnf_version";
48     private LicenseManagerImpl licenseManager;
49
50     @Mock
51     private LicenseDataAccessService licenseDataAccessService;
52
53     @Before
54     public void setUp() {
55         licenseManager = new LicenseManagerImpl();
56     }
57
58     @Test
59     public void retrieveLicenseModel_shouldThrowException_whenRetrieveLicenseModelDataIsEmpty() {
60
61         // GIVEN
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());
66
67         // WHEN THEN
68         licenseManager.setDAService(licenseDataAccessService);
69
70         assertThatExceptionOfType(DataAccessException.class)
71             .isThrownBy(() -> licenseManager.retrieveLicenseModel(VNF_TYPE, VNF_VERSION))
72             .withMessage(expectedMessage);
73     }
74
75     @Test
76     public void retrieveLicenseModel_shouldReturnLicenseModelWithNullValues_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
87         licenseManager.setDAService(licenseDataAccessService);
88         LicenseModel licenseModel = licenseManager.retrieveLicenseModel(VNF_TYPE, VNF_VERSION);
89
90         // THEN
91         assertNull(licenseModel.getEntitlementPoolUuid());
92         assertNull(licenseModel.getLicenseKeyGroupUuid());
93     }
94
95     @Test
96     public void retrieveLicenseModel_shouldReturnCorrectLicenseModel_whenCorrectXmlExists() {
97
98         // GIVEN
99         String expectedEntitlementPool = "default_entitlement_pool";
100         String expectedKeyGroup = "default_key_group";
101
102         String correctlyFormedXml = String.format("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
103             + "<parent>\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);
107
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);
112
113         // WHEN
114         licenseManager.setDAService(licenseDataAccessService);
115         LicenseModel licenseModel = licenseManager.retrieveLicenseModel(VNF_TYPE, VNF_VERSION);
116
117         // THEN
118         assertEquals(expectedEntitlementPool, licenseModel.getEntitlementPoolUuid());
119         assertEquals(expectedKeyGroup, licenseModel.getLicenseKeyGroupUuid());
120     }
121 }