Add additional junits for LicenseManagerImpl 51/56851/3
authorJoanna Jeremicz <joanna.jeremicz@nokia.com>
Tue, 26 Jun 2018 07:54:34 +0000 (09:54 +0200)
committerTakamune Cho <tc012c@att.com>
Thu, 19 Jul 2018 19:00:02 +0000 (19:00 +0000)
- Positive and negative case

Issue-ID: APPC-1024
Change-Id: Ic619553cc305517bebecf0754a7b9f7e8dd7ce51
Signed-off-by: Joanna Jeremicz <joanna.jeremicz@nokia.com>
appc-dispatcher/appc-license-manager/appc-license-manager-core/src/test/java/org/onap/appc/licmgr/impl/LicenseManagerImplTest.java

index 6fd9cb1..c025c98 100644 (file)
@@ -23,38 +23,99 @@ package org.onap.appc.licmgr.impl;
 
 
 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
 import static org.mockito.BDDMockito.given;
+import static org.onap.appc.licmgr.Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_CONTENT;
 
 import java.util.Collections;
+import java.util.Map;
+import java.util.HashMap;
+import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.runners.MockitoJUnitRunner;
 import org.onap.appc.licmgr.LicenseDataAccessService;
 import org.onap.appc.licmgr.exception.DataAccessException;
+import org.onap.appc.licmgr.objects.LicenseModel;
 
 @RunWith(MockitoJUnitRunner.class)
 public class LicenseManagerImplTest {
 
+    private static final String VNF_TYPE = "default_vnf_type";
+    private static final String VNF_VERSION = "default_vnf_version";
+    private LicenseManagerImpl licenseManager;
+
     @Mock
     private LicenseDataAccessService licenseDataAccessService;
 
+    @Before
+    public void setUp() {
+        licenseManager = new LicenseManagerImpl();
+    }
+
     @Test
     public void retrieveLicenseModel_shouldThrowException_whenRetrieveLicenseModelDataIsEmpty() {
 
         // GIVEN
-        String vnfType = "dummy1";
-        String vnfVersion = "dummy2";
         String expectedMessage = String
-            .format("License model not found for vnfType='%s' and vnfVersion='%s'", vnfType, vnfVersion);
-        given(licenseDataAccessService.retrieveLicenseModelData(vnfType, vnfVersion)).willReturn(Collections.emptyMap());
+            .format("License model not found for vnfType='%s' and vnfVersion='%s'", VNF_TYPE, VNF_VERSION);
+        given(licenseDataAccessService.retrieveLicenseModelData(VNF_TYPE, VNF_VERSION))
+            .willReturn(Collections.emptyMap());
 
         // WHEN THEN
-        LicenseManagerImpl licenseManager = new LicenseManagerImpl();
         licenseManager.setDAService(licenseDataAccessService);
 
         assertThatExceptionOfType(DataAccessException.class)
-            .isThrownBy(() -> licenseManager.retrieveLicenseModel(vnfType, vnfVersion))
+            .isThrownBy(() -> licenseManager.retrieveLicenseModel(VNF_TYPE, VNF_VERSION))
             .withMessage(expectedMessage);
     }
+
+    @Test
+    public void retrieveLicenseModel_shouldReturnLicenseModelWithNullValues_whenXmlIsMalformed() {
+
+        // GIVEN
+        String malformedXml = "xyz";
+
+        Map<String, String> licenseModelData = new HashMap<>();
+        licenseModelData.put(ARTIFACT_CONTENT.name(), malformedXml);
+        given(licenseDataAccessService.retrieveLicenseModelData(VNF_TYPE, VNF_VERSION))
+            .willReturn(licenseModelData);
+
+        // WHEN
+        licenseManager.setDAService(licenseDataAccessService);
+        LicenseModel licenseModel = licenseManager.retrieveLicenseModel(VNF_TYPE, VNF_VERSION);
+
+        // THEN
+        assertNull(licenseModel.getEntitlementPoolUuid());
+        assertNull(licenseModel.getLicenseKeyGroupUuid());
+    }
+
+    @Test
+    public void retrieveLicenseModel_shouldReturnCorrectLicenseModel_whenCorrectXmlExists() {
+
+        // GIVEN
+        String expectedEntitlementPool = "default_entitlement_pool";
+        String expectedKeyGroup = "default_key_group";
+
+        String correctlyFormedXml = String.format("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+            + "<parent>\n"
+            + "<entitlement-pool-uuid>%s</entitlement-pool-uuid>\n"
+            + "<license-key-group-uuid>%s</license-key-group-uuid>\n"
+            + "</parent>", expectedEntitlementPool, expectedKeyGroup);
+
+        Map<String, String> licenseModelData = new HashMap<>();
+        licenseModelData.put(ARTIFACT_CONTENT.name(), correctlyFormedXml);
+        given(licenseDataAccessService.retrieveLicenseModelData(VNF_TYPE, VNF_VERSION))
+            .willReturn(licenseModelData);
+
+        // WHEN
+        licenseManager.setDAService(licenseDataAccessService);
+        LicenseModel licenseModel = licenseManager.retrieveLicenseModel(VNF_TYPE, VNF_VERSION);
+
+        // THEN
+        assertEquals(expectedEntitlementPool, licenseModel.getEntitlementPoolUuid());
+        assertEquals(expectedKeyGroup, licenseModel.getLicenseKeyGroupUuid());
+    }
 }
\ No newline at end of file