From ab053807b33b6464a32c1a8052908024f543398e Mon Sep 17 00:00:00 2001 From: Mohamed Rashd Date: Tue, 26 Apr 2022 11:08:08 +0530 Subject: [PATCH] Fix application managment test --- .../application/mgt/common/Base64File.java | 9 ++++ .../application/mgt/core/util/APIUtil.java | 22 ++++++---- .../management/ApplicationManagementTest.java | 43 +++++++++++-------- .../device/mgt/core/common/util/FileUtil.java | 20 ++++++++- 4 files changed, 66 insertions(+), 28 deletions(-) diff --git a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/Base64File.java b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/Base64File.java index 67ca3429f54..ef1f1d3204e 100644 --- a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/Base64File.java +++ b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/Base64File.java @@ -23,6 +23,15 @@ public class Base64File { private String name; private String base64String; + public Base64File() { + + } + + public Base64File(String name, String base64String) { + this.name = name; + this.base64String = base64String; + } + public String getName() { return name; } diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/APIUtil.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/APIUtil.java index 0821341aa2c..7eba3034511 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/APIUtil.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/util/APIUtil.java @@ -91,19 +91,23 @@ public class APIUtil { } public static ApplicationManager getApplicationManager() { - if (applicationManager == null) { - synchronized (APIUtil.class) { - if (applicationManager == null) { - PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); - applicationManager = - (ApplicationManager) ctx.getOSGiService(ApplicationManager.class, null); + try { + if (applicationManager == null) { + synchronized (APIUtil.class) { if (applicationManager == null) { - String msg = "ApplicationDTO Manager service has not initialized."; - log.error(msg); - throw new IllegalStateException(msg); + applicationManager = ApplicationManagementUtil.getApplicationManagerInstance(); + if (applicationManager == null) { + String msg = "ApplicationDTO Manager service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } } } } + } catch (Exception e) { + String msg = "Error occurred while getting the application manager"; + log.error(msg); + throw new IllegalStateException(msg); } return applicationManager; } diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/test/java/io/entgra/application/mgt/core/management/ApplicationManagementTest.java b/components/application-mgt/io.entgra.application.mgt.core/src/test/java/io/entgra/application/mgt/core/management/ApplicationManagementTest.java index 06c8283bad4..baccaed9996 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/test/java/io/entgra/application/mgt/core/management/ApplicationManagementTest.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/test/java/io/entgra/application/mgt/core/management/ApplicationManagementTest.java @@ -42,6 +42,7 @@ import io.entgra.application.mgt.core.impl.ApplicationManagerImpl; import io.entgra.application.mgt.core.internal.DataHolder; import io.entgra.application.mgt.core.util.ConnectionManagerUtil; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; +import org.wso2.carbon.device.mgt.core.common.util.FileUtil; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceTypeVersion; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; @@ -50,6 +51,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -100,28 +102,33 @@ public class ApplicationManagementTest extends BaseTestCase { releaseWrapper.setReleaseType("free"); releaseWrapper.setPrice(5.7); releaseWrapper.setSupportedOsVersions("4.0-7.0"); - entAppReleaseWrappers.add(releaseWrapper); - applicationWrapper.setEntAppReleaseWrappers(entAppReleaseWrappers); - ApplicationArtifact applicationArtifact = new ApplicationArtifact(); - applicationArtifact.setBannerName("My First Banner"); File banner = new File("src/test/resources/samples/app1/banner1.jpg"); - InputStream bannerStream = new FileInputStream(banner); - applicationArtifact.setBannerStream(bannerStream); - applicationArtifact.setIconName("My First Icon"); - applicationArtifact.setIconStream(new FileInputStream(new File("src/test/resources/samples/app1/icon.png"))); - applicationArtifact.setInstallerName("Test Android App"); - applicationArtifact.setInstallerStream(new FileInputStream(new File("src/test/resources/samples/app1/sample.apk"))); - - Map screenshots = new HashMap<>(); - screenshots.put("shot1", new FileInputStream(new File("src/test/resources/samples/app1/shot1.png"))); - screenshots.put("shot2", new FileInputStream(new File("src/test/resources/samples/app1/shot2.png"))); - screenshots.put("shot3", new FileInputStream(new File("src/test/resources/samples/app1/shot3.png"))); - - applicationArtifact.setScreenshots(screenshots); + File icon = new File("src/test/resources/samples/app1/icon.png"); + File ss1 = new File("src/test/resources/samples/app1/shot1.png"); + File ss2 = new File("src/test/resources/samples/app1/shot2.png"); + File ss3 = new File("src/test/resources/samples/app1/shot3.png"); + + Base64File bannerBase64 = new Base64File("banner", FileUtil.fileToBase64String(banner)); + Base64File iconBase64 = new Base64File("icon", FileUtil.fileToBase64String(icon)); + Base64File ss1Base64 = new Base64File("ss1", FileUtil.fileToBase64String(ss1)); + Base64File ss2Base64 = new Base64File("ss2", FileUtil.fileToBase64String(ss2)); + Base64File ss3Base64 = new Base64File("ss3", FileUtil.fileToBase64String(ss3)); + + File apk = new File("src/test/resources/samples/app1/sample.apk"); + Base64File apkBase64 = new Base64File("apk", FileUtil.fileToBase64String(apk)); + + + releaseWrapper.setBanner(bannerBase64); + releaseWrapper.setIcon(iconBase64); + releaseWrapper.setBinaryFile(apkBase64); + releaseWrapper.setScreenshots(Arrays.asList(ss1Base64, ss2Base64, ss3Base64)); + + entAppReleaseWrappers.add(releaseWrapper); + applicationWrapper.setEntAppReleaseWrappers(entAppReleaseWrappers); + ApplicationManager manager = new ApplicationManagerImpl(); - log.error(manager); manager.createApplication(applicationWrapper, false); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/common/util/FileUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/common/util/FileUtil.java index d90e586412c..9626c8198f8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/common/util/FileUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/common/util/FileUtil.java @@ -18,7 +18,10 @@ package org.wso2.carbon.device.mgt.core.common.util; +import org.apache.commons.io.FileUtils; import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; import java.io.InputStream; import java.util.Base64; @@ -50,6 +53,17 @@ public class FileUtil { return new ByteArrayInputStream(base64Bytes); } + /** + * Useful to convert input stream to base64 string + * + * @param file stream to be converted + * @return base64 string of the provided input stream + */ + public static String fileToBase64String(File file) throws IOException { + byte[] fileContent = FileUtils.readFileToByteArray(file); + return Base64.getEncoder().encodeToString(fileContent); + } + /** * This generates file name with a suffix depending on the duplicate name count, useful when saving * files with the same name @@ -102,6 +116,10 @@ public class FileUtil { * @return file name without file extension */ private static String extractFileNameWithoutExtension(String fileName) { - return fileName.substring(0, fileName.lastIndexOf('.')); + int lastIndexOfDot = fileName.lastIndexOf('.'); + if (lastIndexOfDot != -1) { + return fileName.substring(0, fileName.lastIndexOf('.')); + } + return fileName; } }