use of com.mobiledgex.matchingengine.MatchingEngine in project edge-cloud-sampleapps by mobiledgex.
the class EngineCallTest method appConnectionTestTcp002.
/**
* Tests the MatchingEngine SDK supplied HTTP connection to the edge cloudlet. FIXME: TLS Test with certs.
*/
@Test
public void appConnectionTestTcp002() {
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
MatchingEngine me = new MatchingEngine(context);
me.setUseWifiOnly(useWifiOnly);
AppConnectionManager appConnect = me.getAppConnectionManager();
me.setMatchingEngineLocationAllowed(true);
me.setAllowSwitchIfNoSubscriberInfo(true);
OkHttpClient httpClient = null;
// Test against Http Echo.
String carrierName = "TDG";
String appName = "HttpEcho";
String orgName = "MobiledgeX";
String appVersion = "20191204";
try {
String data = "{\"Data\": \"food\"}";
AppClient.RegisterClientRequest req = me.createDefaultRegisterClientRequest(context, orgName).setCarrierName(carrierName).setAppName(appName).setAppVers(appVersion).build();
AppClient.RegisterClientReply registerReply;
// FIXME: Need/want a secondary cloudlet for this AppInst test.
if (true) {
registerReply = me.registerClient(req, hostOverride, portOverride, GRPC_TIMEOUT_MS);
} else {
registerReply = me.registerClient(req, GRPC_TIMEOUT_MS);
}
assertTrue("Register did not succeed for HttpEcho appInst", registerReply.getStatus() == AppClient.ReplyStatus.RS_SUCCESS);
Location location = getTestLocation(47.6062, 122.3321);
AppClient.FindCloudletRequest findCloudletRequest = me.createDefaultFindCloudletRequest(context, location).setCarrierName(carrierName).build();
assertEquals("Session cookies don't match!", registerReply.getSessionCookie(), findCloudletRequest.getSessionCookie());
AppClient.FindCloudletReply findCloudletReply;
if (true) {
findCloudletReply = me.findCloudlet(findCloudletRequest, hostOverride, portOverride, GRPC_TIMEOUT_MS);
} else {
findCloudletReply = me.findCloudlet(findCloudletRequest, GRPC_TIMEOUT_MS);
}
// SSL:
Future<OkHttpClient> httpClientFuture = null;
httpClientFuture = appConnect.getHttpClient((int) GRPC_TIMEOUT_MS);
// FIXME: UI Console exposes HTTP as TCP only, so test here use getTcpMap().
String url = null;
assertTrue("No AppPorts!", findCloudletReply.getPortsCount() > 0);
HashMap<Integer, AppPort> portMap = appConnect.getTCPMap(findCloudletReply);
// Choose the port that we happen to know the internal port for, 3001.
AppPort one = portMap.get(3001);
url = appConnect.createUrl(findCloudletReply, one, one.getPublicPort());
assertTrue("URL for server seems very incorrect. ", url != null && url.length() > "http://:".length());
assertFalse("Failed to get an SSL Socket!", httpClientFuture == null);
// Interface bound TCP socket, has default timeout equal to NetworkManager.
httpClient = httpClientFuture.get();
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON, data);
Request request = new Request.Builder().url(url).post(body).build();
Response response = httpClient.newCall(request).execute();
String output = response.body().string();
boolean found = output.indexOf("food") != -1 ? true : false;
;
assertTrue("Didn't find json data [" + data + "] in response!", found == true);
} catch (PackageManager.NameNotFoundException nnfe) {
} catch (IOException ioe) {
Log.e(TAG, Log.getStackTraceString(ioe));
assertFalse("appConnectionTestTcp002: IOException", true);
} catch (DmeDnsException dde) {
Log.e(TAG, Log.getStackTraceString(dde));
assertFalse("appConnectionTestTcp002: DmeDnsException", true);
} catch (ExecutionException ee) {
Log.i(TAG, Log.getStackTraceString(ee));
assertFalse("appConnectionTestTcp002: ExecutionException!", true);
} catch (StatusRuntimeException sre) {
Log.i(TAG, sre.getMessage());
Log.i(TAG, Log.getStackTraceString(sre));
assertFalse("appConnectionTestTcp002: StatusRuntimeException!", true);
} catch (InterruptedException ie) {
Log.i(TAG, Log.getStackTraceString(ie));
assertFalse("appConnectionTestTcp002: InterruptedException!", true);
} finally {
enableMockLocation(context, false);
}
}
use of com.mobiledgex.matchingengine.MatchingEngine in project edge-cloud-sampleapps by mobiledgex.
the class EngineCallTest method appConnectionTestTcp001.
/**
* Tests the MatchingEngine SDK supplied TCP connection to the edge cloudlet.
*
* This is a raw stream to a test echo server, so there are no explicit message delimiters.
*/
@Test
public void appConnectionTestTcp001() {
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
MatchingEngine me = new MatchingEngine(context);
me.setUseWifiOnly(useWifiOnly);
AppConnectionManager appConnect = me.getAppConnectionManager();
enableMockLocation(context, true);
Socket s = null;
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try {
// Test against Http Echo.
String carrierName = "TDG";
String appName = "HttpEcho";
String orgName = "MobiledgeX";
String appVersion = "20191204";
// Exercise and override the default:
// The app version will be null, but we can build from scratch for test
AppClient.RegisterClientRequest regRequest = AppClient.RegisterClientRequest.newBuilder().setCarrierName(me.retrieveNetworkCarrierName(context)).setOrgName(orgName).setAppName(appName).setAppVers(appVersion).build();
AppClient.RegisterClientReply registerClientReply;
if (true) {
registerClientReply = me.registerClient(regRequest, hostOverride, portOverride, GRPC_TIMEOUT_MS);
} else {
registerClientReply = me.registerClient(regRequest, GRPC_TIMEOUT_MS);
}
assertTrue("Register did not succeed for HttpEcho appInst", registerClientReply.getStatus() == AppClient.ReplyStatus.RS_SUCCESS);
Location location = getTestLocation(47.6062, 122.3321);
// Defaults:
AppClient.FindCloudletRequest findCloudletRequest = me.createDefaultFindCloudletRequest(context, location).setCarrierName(findCloudletCarrierOverride).build();
AppClient.FindCloudletReply findCloudletReply;
if (true) {
findCloudletReply = me.findCloudlet(findCloudletRequest, hostOverride, portOverride, GRPC_TIMEOUT_MS);
} else {
findCloudletReply = me.findCloudlet(findCloudletRequest, GRPC_TIMEOUT_MS);
}
// Just using first one. This depends entirely on the server design.
List<AppPort> appPorts = findCloudletReply.getPortsList();
assertTrue("AppPorts is null", appPorts != null);
assertTrue("AppPorts is empty!", appPorts.size() > 0);
HashMap<Integer, AppPort> portMap = appConnect.getTCPMap(findCloudletReply);
// This internal port depends entirely the AppInst configuration/Docker image.
AppPort one = portMap.get(3001);
assertTrue("EndPort is expected to be 0 for this AppInst", one.getEndPort() == 0);
// The actual mapped Public port, or one between getPublicPort() to getEndPort(), inclusive.
Future<Socket> fs = appConnect.getTcpSocket(findCloudletReply, one, one.getPublicPort(), (int) GRPC_TIMEOUT_MS);
// Interface bound TCP socket.
// Nothing to do. Await value.
s = fs.get();
try {
bos = new BufferedOutputStream(s.getOutputStream());
String data = "{\"Data\": \"food\"}";
String rawpost = "POST / HTTP/1.1\r\n" + "Host: 10.227.66.62:3000\r\n" + "User-Agent: curl/7.54.0\r\n" + "Accept: */*\r\n" + "Content-Length: " + data.length() + "\r\n" + "Content-Type: application/json\r\n" + "\r\n" + data;
bos.write(rawpost.getBytes());
bos.flush();
// Some arbitrary object Monitor.
Object aMon = new Object();
synchronized (aMon) {
aMon.wait(1000);
}
bis = new BufferedInputStream(s.getInputStream());
int available = bis.available();
// Probably true.
assertTrue("No bytes available in response.", available > 0);
byte[] b = new byte[4096];
int numRead = bis.read(b);
assertTrue("Didn't get response!", numRead > 0);
String output = new String(b);
// Not an http client, so we're just going to get the substring of something stable:
boolean found = output.indexOf("food") != -1 ? true : false;
;
assertTrue("Didn't find json data [" + data + "] in response!", found == true);
} catch (IOException ioe) {
assertTrue("Failed to get output stream for socket!", false);
}
} catch (DmeDnsException dde) {
Log.e(TAG, Log.getStackTraceString(dde));
assertFalse("appConnectionTestTcp001: DmeDnsException", true);
} catch (ExecutionException ee) {
Log.i(TAG, Log.getStackTraceString(ee));
assertFalse("appConnectionTestTcp001: ExecutionException!", true);
} catch (StatusRuntimeException sre) {
Log.i(TAG, sre.getMessage());
Log.i(TAG, Log.getStackTraceString(sre));
assertFalse("appConnectionTestTcp001: StatusRuntimeException!", true);
} catch (InterruptedException ie) {
Log.i(TAG, Log.getStackTraceString(ie));
assertFalse("appConnectionTestTcp001: InterruptedException!", true);
} catch (PackageManager.NameNotFoundException nnfe) {
Log.i(TAG, Log.getStackTraceString(nnfe));
assertFalse("appConnectionTestTcp001: NameNotFoundException!", true);
} finally {
try {
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
if (s != null) {
s.close();
}
} catch (IOException ioe) {
assertFalse("IO Exceptions trying to close socket.", true);
}
me.setNetworkSwitchingEnabled(true);
}
}
use of com.mobiledgex.matchingengine.MatchingEngine in project edge-cloud-sampleapps by mobiledgex.
the class EngineCallTest method verifyLocationFutureTest.
@Test
public void verifyLocationFutureTest() {
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
MatchingEngine me = new MatchingEngine(context);
me.setUseWifiOnly(useWifiOnly);
me.setMatchingEngineLocationAllowed(true);
me.setAllowSwitchIfNoSubscriberInfo(true);
AppClient.VerifyLocationReply verifyLocationReply = null;
Future<AppClient.VerifyLocationReply> verifyLocationReplyFuture = null;
try {
Location location = getTestLocation(47.6062, 122.3321);
String carrierName = me.retrieveNetworkCarrierName(context);
registerClient(me);
AppClient.VerifyLocationRequest verifyLocationRequest = me.createDefaultVerifyLocationRequest(context, location).setCarrierName(carrierName).build();
if (useHostOverride) {
verifyLocationReplyFuture = me.verifyLocationFuture(verifyLocationRequest, hostOverride, portOverride, GRPC_TIMEOUT_MS);
} else {
verifyLocationReplyFuture = me.verifyLocationFuture(verifyLocationRequest, GRPC_TIMEOUT_MS);
}
verifyLocationReply = verifyLocationReplyFuture.get();
} catch (DmeDnsException dde) {
Log.e(TAG, Log.getStackTraceString(dde));
assertFalse("verifyLocationFutureTest: DmeDnsException", true);
} catch (ExecutionException ee) {
Log.e(TAG, Log.getStackTraceString(ee));
assertFalse("verifyLocationFutureTest: ExecutionException Failed!", true);
} catch (InterruptedException ie) {
Log.e(TAG, Log.getStackTraceString(ie));
assertFalse("verifyLocationFutureTest: InterruptedException!", true);
}
// Temporary.
assertEquals(0, verifyLocationReply.getVer());
assertEquals(AppClient.VerifyLocationReply.TowerStatus.TOWER_UNKNOWN, verifyLocationReply.getTowerStatus());
assertEquals(AppClient.VerifyLocationReply.GPSLocationStatus.LOC_ROAMING_COUNTRY_MATCH, verifyLocationReply.getGpsLocationStatus());
}
use of com.mobiledgex.matchingengine.MatchingEngine in project edge-cloud-sampleapps by mobiledgex.
the class EngineCallTest method getQosPositionKpiFutureTest.
@Test
public void getQosPositionKpiFutureTest() {
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
MatchingEngine me = new MatchingEngine(context);
me.setUseWifiOnly(useWifiOnly);
me.setMatchingEngineLocationAllowed(true);
me.setAllowSwitchIfNoSubscriberInfo(true);
enableMockLocation(context, true);
// The test must use a location where data exists on QOS server.
Location location = MockUtils.createLocation("getQosPositionKpiTest", 8.5821, 50.11);
try {
registerClient(me);
double totalDistanceKm = 20;
double increment = 0.1;
double direction = 45d;
ArrayList<AppClient.QosPosition> kpiRequests = MockUtils.createQosPositionArray(location, direction, totalDistanceKm, increment);
AppClient.QosPositionRequest request = me.createQoSPositionRequest(kpiRequests, 0, null, 0, null);
assertFalse("SessionCookie must not be empty.", request.getSessionCookie().isEmpty());
Future<ChannelIterator<AppClient.QosPositionKpiReply>> replyFuture = null;
if (useHostOverride) {
replyFuture = me.getQosPositionKpiFuture(request, hostOverride, portOverride, GRPC_TIMEOUT_MS);
} else {
replyFuture = me.getQosPositionKpiFuture(request, GRPC_TIMEOUT_MS);
}
// A stream of QosPositionKpiReply(s), with a non-stream block of responses.
// Wait for value with get().
ChannelIterator<AppClient.QosPositionKpiReply> responseIterator = replyFuture.get();
long total = 0;
while (responseIterator.hasNext()) {
AppClient.QosPositionKpiReply aR = responseIterator.next();
for (int i = 0; i < aR.getPositionResultsCount(); i++) {
System.out.println(aR.getPositionResults(i));
}
total += aR.getPositionResultsCount();
}
responseIterator.shutdown();
assertEquals((long) (kpiRequests.size()), total);
} catch (DmeDnsException dde) {
Log.i(TAG, Log.getStackTraceString(dde));
assertFalse("getQosPositionKpiFutureTest: DmeDnsException!", true);
} catch (ExecutionException ee) {
Log.i(TAG, Log.getStackTraceString(ee));
assertFalse("getQosPositionKpiFutureTest: ExecutionException!", true);
} catch (StatusRuntimeException sre) {
Log.i(TAG, sre.getMessage());
Log.i(TAG, Log.getStackTraceString(sre));
assertFalse("getQosPositionKpiFutureTest: StatusRuntimeException!", true);
} catch (InterruptedException ie) {
Log.i(TAG, Log.getStackTraceString(ie));
assertFalse("getQosPositionKpiFutureTest: InterruptedException!", true);
} finally {
enableMockLocation(context, false);
}
}
use of com.mobiledgex.matchingengine.MatchingEngine in project edge-cloud-sampleapps by mobiledgex.
the class EngineCallTest method appConnectionTestTcp_Http_001.
/**
* NOTE: HttpEcho may only be installed on wifi.dme domain
*/
@Test
public void appConnectionTestTcp_Http_001() {
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
MatchingEngine me = new MatchingEngine(context);
me.setUseWifiOnly(useWifiOnly);
AppConnectionManager appConnect = me.getAppConnectionManager();
me.setMatchingEngineLocationAllowed(true);
me.setAllowSwitchIfNoSubscriberInfo(true);
try {
String data = "{\"Data\": \"food\"}";
String carrierName = "TDG";
String orgName = "MobiledgeX";
String appName = "HttpEcho";
String appVersion = "20191204";
AppClient.RegisterClientRequest req = me.createDefaultRegisterClientRequest(context, orgName).setCarrierName(carrierName).setAppName(appName).setAppVers(appVersion).build();
AppClient.RegisterClientReply registerClientReply;
// FIXME: Need/want a secondary cloudlet for this AppInst test.
if (true) {
registerClientReply = me.registerClient(req, hostOverride, portOverride, GRPC_TIMEOUT_MS);
} else {
registerClientReply = me.registerClient(req, GRPC_TIMEOUT_MS);
}
assertTrue("Register did not succeed for HttpEcho appInst", registerClientReply.getStatus() == AppClient.ReplyStatus.RS_SUCCESS);
Location location = getTestLocation(47.6062, 122.3321);
AppClient.FindCloudletRequest findCloudletRequest = me.createDefaultFindCloudletRequest(context, location).setCarrierName(carrierName).build();
// TODO: Validate JWT
AppClient.FindCloudletReply findCloudletReply;
if (true) {
findCloudletReply = me.findCloudlet(findCloudletRequest, hostOverride, portOverride, GRPC_TIMEOUT_MS);
} else {
findCloudletReply = me.findCloudlet(findCloudletRequest, GRPC_TIMEOUT_MS);
}
// SSL:
Future<OkHttpClient> httpClientFuture = null;
httpClientFuture = appConnect.getHttpClient(GRPC_TIMEOUT_MS);
assertTrue("HttpClientFuture is NULL!", httpClientFuture != null);
// FIXME: UI Console exposes HTTP as TCP only, so the test here uses getTcpList().
String url = null;
HashMap<Integer, AppPort> portMap = appConnect.getTCPMap(findCloudletReply);
// Choose the TCP port, and we happen to know our server is on one port only: 3001.
AppPort one = portMap.get(3001);
assertTrue("Did not find server! ", one != null);
url = appConnect.createUrl(findCloudletReply, one, one.getPublicPort());
assertTrue("URL for server seems very incorrect. ", url != null && url.length() > "http://:".length());
// Interface bound TCP socket, has default timeout equal to NetworkManager.
OkHttpClient httpClient = httpClientFuture.get();
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON, data);
Request request = new Request.Builder().url(url).post(body).build();
Response response = httpClient.newCall(request).execute();
String output = response.body().string();
boolean found = output.indexOf("food") != -1 ? true : false;
assertTrue("Didn't find json data [" + data + "] in response!", found == true);
Request mobiledgeXSiteRequest = new Request.Builder().url("https://mobiledgex.com").build();
Response mexSiteResponse = httpClient.newCall(mobiledgeXSiteRequest).execute();
int httpStatus = mexSiteResponse.code();
assertEquals("Did not reach our home site. Status: ", 200, httpStatus);
// This certificate goes to artifactory.mobiledgex.net, it *should* fail, but "connect" with
// HTTP Status 200 OK.
boolean failedVerification = false;
mobiledgeXSiteRequest = new Request.Builder().url("https://mobiledgex.net").build();
try {
mexSiteResponse = httpClient.newCall(mobiledgeXSiteRequest).execute();
} catch (SSLPeerUnverifiedException e) {
failedVerification = true;
httpStatus = mexSiteResponse.code();
assertEquals("Should fail SSL Host verification, but still be 200 OK. Status: ", 200, httpStatus);
}
assertTrue("Did not fail hostname SSL verification!", failedVerification);
} catch (PackageManager.NameNotFoundException nnfe) {
Log.e(TAG, nnfe.getMessage());
Log.i(TAG, Log.getStackTraceString(nnfe));
assertFalse("appConnectionTestTcp001: Package Info is missing!", true);
} catch (IOException ioe) {
Log.e(TAG, Log.getStackTraceString(ioe));
assertFalse("appConnectionTestTcp001: IOException", true);
} catch (DmeDnsException dde) {
Log.e(TAG, Log.getStackTraceString(dde));
assertFalse("appConnectionTestTcp001: DmeDnsException", true);
} catch (ExecutionException ee) {
Log.i(TAG, Log.getStackTraceString(ee));
assertFalse("appConnectionTestTcp001: ExecutionException!", true);
} catch (StatusRuntimeException sre) {
Log.e(TAG, sre.getMessage());
Log.i(TAG, Log.getStackTraceString(sre));
assertFalse("appConnectionTestTcp001: StatusRuntimeException!", true);
} catch (InterruptedException ie) {
Log.i(TAG, Log.getStackTraceString(ie));
assertFalse("appConnectionTestTcp001: InterruptedException!", true);
}
}
Aggregations