Fix application managment test

temp
Mohamed Rashd 3 years ago
parent d67cd9b07a
commit ab053807b3

@ -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;
}

@ -91,12 +91,11 @@ public class APIUtil {
}
public static ApplicationManager getApplicationManager() {
try {
if (applicationManager == null) {
synchronized (APIUtil.class) {
if (applicationManager == null) {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
applicationManager =
(ApplicationManager) ctx.getOSGiService(ApplicationManager.class, null);
applicationManager = ApplicationManagementUtil.getApplicationManagerInstance();
if (applicationManager == null) {
String msg = "ApplicationDTO Manager service has not initialized.";
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;
}

@ -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<String, InputStream> 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);
}

@ -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) {
int lastIndexOfDot = fileName.lastIndexOf('.');
if (lastIndexOfDot != -1) {
return fileName.substring(0, fileName.lastIndexOf('.'));
}
return fileName;
}
}

Loading…
Cancel
Save