Fix application managment test

master
Mohamed Rashd 3 years ago
parent d67cd9b07a
commit ab053807b3

@ -23,6 +23,15 @@ public class Base64File {
private String name; private String name;
private String base64String; private String base64String;
public Base64File() {
}
public Base64File(String name, String base64String) {
this.name = name;
this.base64String = base64String;
}
public String getName() { public String getName() {
return name; return name;
} }

@ -91,12 +91,11 @@ public class APIUtil {
} }
public static ApplicationManager getApplicationManager() { public static ApplicationManager getApplicationManager() {
try {
if (applicationManager == null) { if (applicationManager == null) {
synchronized (APIUtil.class) { synchronized (APIUtil.class) {
if (applicationManager == null) { if (applicationManager == null) {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); applicationManager = ApplicationManagementUtil.getApplicationManagerInstance();
applicationManager =
(ApplicationManager) ctx.getOSGiService(ApplicationManager.class, null);
if (applicationManager == null) { if (applicationManager == null) {
String msg = "ApplicationDTO Manager service has not initialized."; String msg = "ApplicationDTO Manager service has not initialized.";
log.error(msg); log.error(msg);
@ -105,6 +104,11 @@ public class APIUtil {
} }
} }
} }
} catch (Exception e) {
String msg = "Error occurred while getting the application manager";
log.error(msg);
throw new IllegalStateException(msg);
}
return applicationManager; return applicationManager;
} }

@ -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.internal.DataHolder;
import io.entgra.application.mgt.core.util.ConnectionManagerUtil; import io.entgra.application.mgt.core.util.ConnectionManagerUtil;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; 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.DeviceType;
import org.wso2.carbon.device.mgt.core.dto.DeviceTypeVersion; import org.wso2.carbon.device.mgt.core.dto.DeviceTypeVersion;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl;
@ -50,6 +51,7 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -100,28 +102,33 @@ public class ApplicationManagementTest extends BaseTestCase {
releaseWrapper.setReleaseType("free"); releaseWrapper.setReleaseType("free");
releaseWrapper.setPrice(5.7); releaseWrapper.setPrice(5.7);
releaseWrapper.setSupportedOsVersions("4.0-7.0"); 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"); File banner = new File("src/test/resources/samples/app1/banner1.jpg");
InputStream bannerStream = new FileInputStream(banner); File icon = new File("src/test/resources/samples/app1/icon.png");
applicationArtifact.setBannerStream(bannerStream); File ss1 = new File("src/test/resources/samples/app1/shot1.png");
applicationArtifact.setIconName("My First Icon"); File ss2 = new File("src/test/resources/samples/app1/shot2.png");
applicationArtifact.setIconStream(new FileInputStream(new File("src/test/resources/samples/app1/icon.png"))); File ss3 = new File("src/test/resources/samples/app1/shot3.png");
applicationArtifact.setInstallerName("Test Android App");
applicationArtifact.setInstallerStream(new FileInputStream(new File("src/test/resources/samples/app1/sample.apk"))); Base64File bannerBase64 = new Base64File("banner", FileUtil.fileToBase64String(banner));
Base64File iconBase64 = new Base64File("icon", FileUtil.fileToBase64String(icon));
Map<String, InputStream> screenshots = new HashMap<>(); Base64File ss1Base64 = new Base64File("ss1", FileUtil.fileToBase64String(ss1));
screenshots.put("shot1", new FileInputStream(new File("src/test/resources/samples/app1/shot1.png"))); Base64File ss2Base64 = new Base64File("ss2", FileUtil.fileToBase64String(ss2));
screenshots.put("shot2", new FileInputStream(new File("src/test/resources/samples/app1/shot2.png"))); Base64File ss3Base64 = new Base64File("ss3", FileUtil.fileToBase64String(ss3));
screenshots.put("shot3", new FileInputStream(new File("src/test/resources/samples/app1/shot3.png")));
File apk = new File("src/test/resources/samples/app1/sample.apk");
applicationArtifact.setScreenshots(screenshots); 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(); ApplicationManager manager = new ApplicationManagerImpl();
log.error(manager);
manager.createApplication(applicationWrapper, false); manager.createApplication(applicationWrapper, false);
} }

@ -18,7 +18,10 @@
package org.wso2.carbon.device.mgt.core.common.util; package org.wso2.carbon.device.mgt.core.common.util;
import org.apache.commons.io.FileUtils;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Base64; import java.util.Base64;
@ -50,6 +53,17 @@ public class FileUtil {
return new ByteArrayInputStream(base64Bytes); 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 * This generates file name with a suffix depending on the duplicate name count, useful when saving
* files with the same name * files with the same name
@ -102,6 +116,10 @@ public class FileUtil {
* @return file name without file extension * @return file name without file extension
*/ */
private static String extractFileNameWithoutExtension(String fileName) { private static String extractFileNameWithoutExtension(String fileName) {
int lastIndexOfDot = fileName.lastIndexOf('.');
if (lastIndexOfDot != -1) {
return fileName.substring(0, fileName.lastIndexOf('.')); return fileName.substring(0, fileName.lastIndexOf('.'));
} }
return fileName;
}
} }

Loading…
Cancel
Save