Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

Commit

Permalink
added complete functional test for the use of Microprofile Config
Browse files Browse the repository at this point in the history
interfaces using a non-resource injection pattern.
  • Loading branch information
csturacloudreach committed Sep 10, 2019
1 parent 8ccd507 commit f8a6eeb
Show file tree
Hide file tree
Showing 3 changed files with 653 additions and 407 deletions.
205 changes: 205 additions & 0 deletions core/src/test/java/org/eclipse/jemo/JemoBaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,32 @@
import org.eclipse.jemo.internal.model.*;
import org.eclipse.jemo.runtime.MemoryRuntime;
import org.eclipse.jemo.sys.JemoPluginManager;
import org.eclipse.jemo.sys.auth.JemoAuthentication;
import org.eclipse.jemo.sys.auth.JemoGroup;
import org.eclipse.jemo.sys.auth.JemoUser;
import org.eclipse.jemo.sys.internal.JarEntry;
import org.eclipse.jemo.sys.internal.Util;
import org.eclipse.jemo.sys.microprofile.MicroProfileConfigSource;
import org.eclipse.jemo.sys.microprofile.MicroProfileConfigSourceTest.FixedTestModule;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.stream.Collectors;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;

import org.junit.AfterClass;

import com.fasterxml.jackson.core.JsonProcessingException;

import static org.eclipse.jemo.api.JemoParameter.*;
import static org.eclipse.jemo.internal.model.AmazonAWSRuntime.AWS_REGION_PROP;
import static org.eclipse.jemo.internal.model.GCPRuntime.GCP_REGION_PROP;
Expand Down Expand Up @@ -166,6 +180,10 @@ protected void uploadPlugin(int pluginId, double pluginVersion, String pluginNam
uploadPlugin(pluginId, pluginVersion, pluginName, new ByteArrayInputStream(jarBytes));
}

protected void startFixedProcesses() throws InterruptedException {
processBatch(0, 1.0, JemoPluginManager.PluginManagerModule.class.getName());
}

protected void sendMessage(int moduleId,double moduleVersion,Class<? extends Module> moduleClass,String location,KeyValue... attributes) throws Throwable {
jemoServer.getPluginManager().runWithModuleContext(Void.class, x -> {
JemoMessage msg = new JemoMessage();
Expand All @@ -183,4 +201,191 @@ protected void sendMessage(int moduleId,double moduleVersion,Class<? extends Mod
return null;
});
}

protected ServletInputStream toInputStream(Object obj) throws JsonProcessingException {
return toInputStream(Util.toJSONString(obj).getBytes(Util.UTF8_CHARSET));
}

protected ServletInputStream toInputStream(byte[] data) {
final ByteArrayInputStream configStream = new ByteArrayInputStream(data);
return new ServletInputStream() {

@Override
public int read() throws IOException {
// TODO Auto-generated method stub
return configStream.read();
}

@Override
public void setReadListener(ReadListener readListener) {}

@Override
public boolean isReady() {
// TODO Auto-generated method stub
return true;
}

@Override
public boolean isFinished() {
// TODO Auto-generated method stub
return false;
}
};
}

protected void setModuleConfig(final int applicationId, KeyValue<String> ... configuration) throws Throwable {
AtomicInteger status = new AtomicInteger(200);
final JemoUser adminUser = getTestAdminUser();
getPluginManagerModule().process(new HttpServletRequestAdapter() {

@Override
public String getMethod() {
return "POST";
}

@Override
public String getContentType() {
return "application/json";
}

@Override
public String getParameter(String paramName) {
if("ID".equals(paramName)) {
return String.valueOf(applicationId);
}
return null;
}

@Override
public String getRequestURI() {
return "/jemo";
}

@Override
public String getHeader(String headerName) {
switch(headerName) {
case "Authorization":
return "Basic "+Base64.getEncoder().encodeToString((adminUser.getUsername()+":"+adminUser.getPassword()).getBytes(Util.UTF8_CHARSET));
}

return super.getHeader(headerName);
}

@Override
public ServletInputStream getInputStream() throws IOException {
ModuleConfiguration config = new ModuleConfiguration();
if(configuration != null) {
Arrays.asList(configuration).forEach(cfg -> {
ModuleConfigurationParameter param = new ModuleConfigurationParameter();
param.setKey(cfg.getKey());
param.setValue(cfg.getValue());
param.setOperation(ModuleConfigurationOperation.upsert);
config.getParameters().add(param);
});
}
return toInputStream(config);
}

}, new StatusResponseAdapter(status));
if(status.get() != 200) {
throw new Exception("Error the configuration could not be set correctly. The response code was: "+status.get());
} else {
//we should poll the configuration for this plugin until we see the new value appear
long started = System.currentTimeMillis();
do {
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
if(jemoServer.getPluginManager().getModuleConfiguration(applicationId).entrySet()
.stream().allMatch(e -> Arrays.asList(configuration)
.stream()
.anyMatch(cfg -> cfg.getKey().equals(e.getKey()) && cfg.getValue().equals(e.getValue())))) {
break;
}
} while(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - started) < 10);
}
}

protected String getAdminGroupId() throws Throwable {
HttpServletRequestAdapter request = new HttpServletRequestAdapter() {
@Override
public String getMethod() {
return "GET";
}

@Override
public String getRequestURI() {
return "/jemo/authentication/group";
}
};
HttpServletResponseAdapter response = new HttpServletResponseAdapter() {};
//we need an admin user to get the Admin group
JemoAuthentication.processRequest(JemoAuthentication.getDefaultAdminUser(), request, response);
JemoGroup[] groupList = (JemoGroup[]) Jemo.fromJSONString(Jemo.classOf(new JemoGroup[] {}), response.getResponseBody());
Field groupAdminField = JemoAuthentication.class.getDeclaredField("GROUP_ADMIN");
groupAdminField.setAccessible(true);
for(JemoGroup group : groupList) {
if(group.getId().equals(Util.md5((String)groupAdminField.get(JemoAuthentication.class)))) {
return group.getId();
}
}

return null;
}

private JemoUser TEST_ADMIN_USER = null;

private static class StatusResponseAdapter extends HttpServletResponseAdapter {

private final AtomicInteger status;

public StatusResponseAdapter(AtomicInteger status) {
this.status = status;
this.status.set(200);
}

@Override
public void setStatus(int i) {
// TODO Auto-generated method stub
status.set(i);
}

@Override
public void setStatus(int i, String string) {
// TODO Auto-generated method stub
setStatus(i);
}
}

private JemoUser getTestAdminUser() throws Throwable {
if(TEST_ADMIN_USER == null) {
TEST_ADMIN_USER = new JemoUser();
TEST_ADMIN_USER.setAdmin(true);
TEST_ADMIN_USER.setGroupIds(Arrays.asList(getAdminGroupId()));
TEST_ADMIN_USER.setUsername(UUID.randomUUID().toString());
TEST_ADMIN_USER.setPassword(UUID.randomUUID().toString());
final AtomicInteger status = new AtomicInteger();
JemoAuthentication.processRequest(JemoAuthentication.getDefaultAdminUser(), new HttpServletRequestAdapter() {
@Override
public String getRequestURI() {
return "/jemo/authentication/user";
}

@Override
public String getMethod() {
return "POST";
}


@Override
public ServletInputStream getInputStream() throws IOException {
return toInputStream(TEST_ADMIN_USER);
}
}, new StatusResponseAdapter(status));
if(status.get() != 200) {
TEST_ADMIN_USER = null;
throw new Exception("Could not create default TEST ADMIN user. The error was "+status.get());
}
}

return TEST_ADMIN_USER;
}
}
Loading

0 comments on commit f8a6eeb

Please sign in to comment.