Commit af2122b8 by amir

add tests

parent a01b2bc2
Pipeline #79 skipped in 0 seconds
package microservice;
import java.util.Optional;
import org.junit.Test;
import rx.Observable;
import microservice.types.BaseRestResponse;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import microservice.io.iface.ICommandClient;
import microservice.params.BaseClientParams;
import microservice.params.CommandParams;
public class TestCommandClient {
private final ObjectMapper objMapper = new ObjectMapper();
public class ASM
{
String ae;
String provider;
public String getAe() {
return ae;
}
public void setAe(String ae) {
this.ae = ae;
}
public String getProvider() {
return provider;
}
public void setProvider(String provider) {
this.provider = provider;
}
}
public class ASMClient implements ICommandClient
{
ASM asm;
public ASMClient(ASM asm) {
super();
this.asm = asm;
}
@Override
public BaseRestResponse create(CommandParams reqCtx) {
BaseRestResponse brr = new BaseRestResponse(true, null);
Optional<Observable<BaseRestResponse>> brro = Optional.empty();
switch(reqCtx.getEntity())
{
case "AE":
brro = Optional.of((new ICommandClient.Command(reqCtx,"createAE") {
@Override
protected BaseRestResponse run() throws Exception {
BaseRestResponse brr = new BaseRestResponse(true, null);
asm.setAe(reqCtx.getParamsString());
System.out.println("set ae to: " + asm.getAe());
return brr;
}
}).toObservable());
break;
case "Provider":
brro = Optional.of((new ICommandClient.Command(reqCtx,"createProvider") {
@Override
protected BaseRestResponse run() throws Exception {
BaseRestResponse brr = new BaseRestResponse(true, null);
asm.setProvider(reqCtx.getParamsString());
return brr;
}
}).toObservable());
break;
}
// do something on return from async call
brro.ifPresent(x -> x.subscribe((br) ->
{
System.out.println(br.success);
},(exp) -> {
exp.printStackTrace();
}));
return brr;
}
@Override
public BaseRestResponse read(CommandParams reqCtx) {
// getAE sync
BaseRestResponse res = null;
try {
if ("AE".equals(reqCtx.getEntity()))
{
res = new ICommandClient.Command(reqCtx,"getAE") {
@Override
protected BaseRestResponse run() throws Exception {
BaseRestResponse brr = new BaseRestResponse(true, null);
brr.objectNode = objMapper.createObjectNode().put("AE",asm.getAe());
return brr;
}
}.queue().get();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}
@Override
public BaseRestResponse update(CommandParams reqCtx) {
// TODO Auto-generated method stub
return null;
}
@Override
public BaseRestResponse delete(CommandParams reqCtx) {
// TODO Auto-generated method stub
return null;
}
@Override
public JsonNode getMetrics() {
// TODO Auto-generated method stub
return null;
}
}
@Test
public void testCommand() throws InterruptedException
{
ASM asm = new ASM();
ASMClient asmClient = new ASMClient(asm);
MicroserviceClient msClient = new MicroserviceClient(asmClient, new BaseClientParams("", false, 0));
CommandParams reqCtx = new CommandParams("AE", "ae1", null, null, null);
msClient.create(reqCtx);
System.out.println("sleeping");
//Thread.sleep(5000);
reqCtx = new CommandParams("AE", "ae1", null, null, null);
BaseRestResponse readBrr = msClient.read(reqCtx);
System.out.println(readBrr.objectNode.toString());
System.out.println("end test");
}
}
package microservice;
import microservice.io.iface.IConfiguration;
import microservice.io.impl.IConfigurationConfigPropImpl;
import microservice.io.impl.IServiceDiscoveryConsulImpl;
import org.junit.Test;
public class TestConfiguration {
@Test
public void testConfProviderConsulImpl()
{
IServiceDiscoveryConsulImpl iServiceDiscovery = new IServiceDiscoveryConsulImpl("localhost",8500);
iServiceDiscovery.registerService("myService", "2", "1.2.3.4", 32002);
IConfiguration config = new IConfigurationConfigPropImpl();
config.addConfigurationProvider(iServiceDiscovery);
config.reload();
Object value1 = config.getString("key1",null);
System.out.println(value1);
}
}
package microservice;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.junit.Test;
import microservice.io.iface.INotifyCallback;
import microservice.io.iface.IPubSub;
import microservice.io.impl.IPubSubMQTTImpl;
import com.fasterxml.jackson.databind.JsonNode;
/**
* send-receive test for the MQTT server
* @author franck
*
*/
public class TestMQTT{
@Test
public void testMQTT(){
class ClientMqtt extends Thread{
public void run(){
IPubSub ourClient = null;
try {
ourClient = new IPubSubMQTTImpl("serverAddress", 0, null, 0);
} catch (MqttException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
INotifyCallback notifyCallback = new INotifyCallback() {
@Override
public void onMessage(String topic, JsonNode messageNode) {
System.out.println("we received:\n" + messageNode.asText()
+ "\nfrom:\n" + topic);
}
@Override
public void onError(String topic, String error) {
// TODO Auto-generated method stub
}
};
ourClient.subscribe("MQTTTopic", notifyCallback);
System.out.println("subscribing to MQTT server \"testMQTT\"");
}
}
class ServerMqtt extends Thread{
public void run(){
IPubSub ourServer = null;
try {
ourServer = new IPubSubMQTTImpl("serverAddress", 0, null, 0);
} catch (MqttException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("sending a message to \"TestMQTT\" topic");
ourServer.publish("MQTTTopic", "sent from server");
}
}
// RestClientParams params = new RestClientParams("ds",true,0, "172.16.1.244:8080", 100);
try
{
System.out.println("Start Testing");
Thread MQTTclient = new ClientMqtt();
Thread MQTTserver = new ServerMqtt();
MQTTclient.start();
MQTTserver.start();
MQTTclient.interrupt();
MQTTserver.interrupt();
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package microservice;
import org.junit.Test;
import microservice.MicroserviceClient;
import microservice.MicroserviceClient.EnumRestClientType;
import microservice.params.CommandParams;
import microservice.params.RestClientParams;
import com.fasterxml.jackson.databind.JsonNode;
import microservice.types.BaseRestResponse;
public class TestMicroClient
{
public static int MAX_ITERATION = 10000;
@Test
public void testMicroClient()
{
MicroserviceClient client = null;
BaseRestResponse brr = null;
RestClientParams params = new RestClientParams("ds",true,0, "172.16.1.244:8080",null,100);
try
{
client = new MicroserviceClient(EnumRestClientType.E_HTTP, params);
CommandParams cmdParams = new CommandParams("entities", "MCX/entities/lili/person", null, null, null);
System.out.println("Start Testing");
for (int i = 0 ; i < MAX_ITERATION; i++)
{
brr = client.read(cmdParams);
if (brr.success)
System.out.println(brr.objectNode.toString());
else
System.out.println(brr.error);
Thread.sleep(500);
JsonNode metrics = client.getMetrics();
if (i % 10 == 0)
System.out.println("metrics: " + metrics.toString());
}
System.out.println("End\nMetrics: " + client.getMetrics().toString());
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package microservice;
import microservice.MicroserviceClient.EnumRestClientType;
import microservice.io.impl.IRestServerUndertowImpl;
import microservice.io.impl.IServiceDiscoveryConsulImpl;
import microservice.params.BaseClientParams;
import microservice.params.RestClientParams;
import microservice.params.RestServerParams;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.junit.Test;
/**
* Created by amir on 06/04/16.
*/
public class TestMicroserviceApp {
@Test
public void testApp() throws MqttException, Exception
{
BaseClientParams clientParams = new RestClientParams("other-service", true, 10, "localhost:32010","localhost:6379");
new microservice.MicroserviceApp("myService")
.withMetrics()
.withMonitoring()
.withPubSub(new microservice.io.impl.IPubSubMQTTImpl("tcp://localhost",0,null,0))
.withServiceDiscovery(new IServiceDiscoveryConsulImpl("localhost",8500))
// .addHandler("/mon",new microservice.handlers.MonitorHandler())
.addMicroserviceClient(new MicroserviceClient(EnumRestClientType.E_HTTP,clientParams))
//.addMicroserviceClient("rabbit-service",new MicroserviceClient(EnumRestClientType.E_RABBITMQ,clientParams))
.addRestServer(new IRestServerUndertowImpl(new RestServerParams(32000, "172.16.1.244", 2)))
//.addRestServer(new IRestServerRMQImplnew MBIParams("1", 0, 200, "rmq"))
.build()
.run();
}
}
package microservice;
import microservice.io.impl.IServiceDiscoveryConsulImpl;
import org.junit.Test;
import java.net.UnknownHostException;
/**
* Created by amir on 05/04/16.
*/
public class TestServiceDiscovery {
@Test
public void testConsul() throws InterruptedException, UnknownHostException {
System.out.println("registering service");
IServiceDiscoveryConsulImpl serviceDiscovery = new IServiceDiscoveryConsulImpl("localhost",8500);
serviceDiscovery.registerService("testService","1","localhost",32000);
//serviceDiscovery.registerService("testService","2",32005);
Thread.sleep(5000);
serviceDiscovery.getAvailableServiceNodes("testService");
serviceDiscovery.unregisterService();
Thread.sleep(2000);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment