use of nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy in project mule-coap-connector by teslanet-nl.
the class ObserveTest method testPermanentObserve.
/**
* Test permanent observe
* @throws Exception should not happen in this test
*/
@Test(timeout = 100000L)
public void testPermanentObserve() throws Exception {
final MuleEventSpy spy = new MuleEventSpy("permanent");
spy.clear();
// let asynchronous work happen
pauze();
for (int i = 1; i < contents.size(); i++) {
pauze();
Event result = flowRunner("do_put_permanent").withPayload(contents.get(i)).run();
Message response = result.getMessage();
assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
assertEquals("put nr: " + i + " gave wrong response", ResponseCode.CHANGED.name(), attributes.getResponseCode());
}
await().atMost(10, TimeUnit.SECONDS).until(() -> {
return spy.getEvents().size() == contents.size();
});
int obsOffset = 0;
for (int i = 0; i < spy.getEvents().size(); i++) {
Message response = (Message) spy.getEvents().get(i).getContent();
assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
if (i == 0)
obsOffset = attributes.getOptions().getObserve().intValue();
assertNotEquals("observation nr: " + i + " is empty", null, response.getPayload());
assertTrue("observation nr: " + i + " indicates failure", attributes.isSuccess());
assertEquals("observation nr: " + i + " has wrong content", contents.get(i), new String((byte[]) response.getPayload().getValue()));
assertEquals("observation nr: " + i + " has wrong observe option", obsOffset + i, attributes.getOptions().getObserve().intValue());
}
}
use of nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy in project mule-coap-connector by teslanet-nl.
the class ObserveTest method testTemporaryObserve.
/**
* Test temporary observe
* @throws Exception should not happen in this test
*/
@Test(timeout = 100000L)
public void testTemporaryObserve() throws Exception {
Event result;
Message response;
MuleEventSpy spy = new MuleEventSpy("temporary");
spy.clear();
// let asynchronous work happen
pauze();
assertEquals("unexpected observation on start test", 0, spy.getEvents().size());
for (int i = 1; i < contents.size(); i++) {
pauze();
result = flowRunner("do_put_temporary").withPayload(contents.get(i)).run();
response = result.getMessage();
assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
assertEquals("1st series put nr: " + i + " gave wrong response", ResponseCode.CHANGED.name(), attributes.getResponseCode());
}
assertEquals("unexpected observation after 1st test series", 0, spy.getEvents().size());
result = flowRunner("start_temporary").withPayload("nothing_important").run();
response = result.getMessage();
await("missing observation start response").atMost(10, TimeUnit.SECONDS).until(() -> {
return spy.getEvents().size() == 1;
});
for (int i = 1; i < contents.size(); i++) {
pauze();
result = flowRunner("do_put_temporary").withPayload(contents.get(i)).run();
response = result.getMessage();
assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
assertEquals("2nd series put nr: " + i + " gave wrong response", ResponseCode.CHANGED.name(), attributes.getResponseCode());
}
await("number of observation after 2nd test series").atMost(10, TimeUnit.SECONDS).until(() -> {
return spy.getEvents().size() == contents.size();
});
result = flowRunner("stop_temporary").withPayload("nothing_important").run();
response = result.getMessage();
await("observation stop response").atMost(10, TimeUnit.SECONDS).until(() -> {
return spy.getEvents().size() == contents.size() + 1;
});
for (int i = 1; i < contents.size(); i++) {
pauze();
result = flowRunner("do_put_temporary").withPayload(contents.get(i)).run();
response = result.getMessage();
assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
assertEquals("3rd series put nr: " + i + " gave wrong response", ResponseCode.CHANGED.name(), attributes.getResponseCode());
}
await("number of observation after 3rd test series").atMost(10, TimeUnit.SECONDS).until(() -> {
return spy.getEvents().size() == contents.size() + 1;
});
int obsOffset = 0;
for (int i = 1; i < contents.size(); i++) {
response = (Message) spy.getEvents().get(i).getContent();
assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
if (i == 1)
obsOffset = attributes.getOptions().getObserve().intValue() - 1;
assertNotEquals("observation nr: " + i + " is empty", null, response.getPayload().getValue());
assertTrue("observation nr: " + i + " indicates failure", attributes.isSuccess());
assertEquals("observation nr: " + i + " has wrong content", contents.get(i), new String((byte[]) response.getPayload().getValue()));
assertEquals("observation nr: " + i + " has wrong observe option", obsOffset + i, attributes.getOptions().getObserve().intValue());
}
}
use of nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy in project mule-coap-connector by teslanet-nl.
the class ObserveTest method testTemporaryObserveNon.
/**
* Test temporary observe using NON requests
* @throws Exception should not happen in this test
*/
@Test(timeout = 100000L)
public void testTemporaryObserveNon() throws Exception {
Event result;
Message response;
MuleEventSpy spy = new MuleEventSpy("temporary");
spy.clear();
// let asynchronous work happen
pauze();
assertEquals("unexpected observation on start test", 0, spy.getEvents().size());
for (int i = 1; i < contents.size(); i++) {
pauze();
result = flowRunner("do_put_temporary").withPayload(contents.get(i)).run();
response = result.getMessage();
assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
assertEquals("1st series put nr: " + i + " gave wrong response", ResponseCode.CHANGED.name(), attributes.getResponseCode());
}
assertEquals("unexpected observation after 1st test series", 0, spy.getEvents().size());
result = flowRunner("start_temporary").withPayload("nothing_important").withVariable("msgType", "NON_CONFIRMABLE").run();
response = result.getMessage();
await("missing observation start response").atMost(10, TimeUnit.SECONDS).until(() -> {
return spy.getEvents().size() == 1;
});
for (int i = 1; i < contents.size(); i++) {
pauze();
result = flowRunner("do_put_temporary").withPayload(contents.get(i)).run();
response = result.getMessage();
assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
assertEquals("2nd series put nr: " + i + " gave wrong response", ResponseCode.CHANGED.name(), attributes.getResponseCode());
}
await("number of observation after 2nd test series").atMost(10, TimeUnit.SECONDS).until(() -> {
return spy.getEvents().size() == contents.size();
});
result = flowRunner("stop_temporary").withPayload("nothing_important").withVariable("msgType", "NON_CONFIRMABLE").run();
response = result.getMessage();
await("observation stop response").atMost(10, TimeUnit.SECONDS).until(() -> {
return spy.getEvents().size() == contents.size() + 1;
});
for (int i = 1; i < contents.size(); i++) {
pauze();
result = flowRunner("do_put_temporary").withPayload(contents.get(i)).run();
response = result.getMessage();
assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
assertEquals("3rd series put nr: " + i + " gave wrong response", ResponseCode.CHANGED.name(), attributes.getResponseCode());
}
await("number of observation after 3rd test series").atMost(10, TimeUnit.SECONDS).until(() -> {
return spy.getEvents().size() == contents.size() + 1;
});
int obsOffset = 0;
for (int i = 1; i < contents.size(); i++) {
response = (Message) spy.getEvents().get(i).getContent();
assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
if (i == 1)
obsOffset = attributes.getOptions().getObserve().intValue() - 1;
assertNotEquals("observation nr: " + i + " is empty", null, response.getPayload().getValue());
assertTrue("observation nr: " + i + " indicates failure", attributes.isSuccess());
assertEquals("observation nr: " + i + " has wrong content", contents.get(i), new String((byte[]) response.getPayload().getValue()));
assertEquals("observation nr: " + i + " has wrong observe option", obsOffset + i, attributes.getOptions().getObserve().intValue());
}
}
use of nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy in project mule-coap-connector by teslanet-nl.
the class AsyncBasicTest method testAsyncRequest.
/**
* Test Async request
* @throws Exception should not happen in this test
*/
@Test(timeout = 20000L)
public void testAsyncRequest() throws Exception {
MuleEventSpy spy = new MuleEventSpy("async-handler");
spy.clear();
Event result = flowRunner(flowName).withPayload("nothing_important").run();
Message response = result.getMessage();
assertTrue("wrong response payload", response.getPayload().getDataType().isCompatibleWith(DataType.STRING));
assertEquals("wrong response payload", "nothing_important", (String) response.getPayload().getValue());
// let handler do its asynchronous work
await().atMost(10, TimeUnit.SECONDS).until(() -> {
return spy.getEvents().size() == 1;
});
// assertions...
response = (Message) spy.getEvents().get(0).getContent();
assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
assertEquals("wrong request code", expectedRequestCode.name(), attributes.getRequestCode());
assertEquals("wrong request uri", expectedRequestUri, attributes.getRequestUri());
assertEquals("wrong response code", expectedResponseCode, attributes.getResponseCode());
assertArrayEquals("wrong response payload", (expectedPayload == null ? null : expectedPayload.getBytes(Defs.COAP_CHARSET)), (byte[]) response.getPayload().getValue());
}
use of nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy in project mule-coap-connector by teslanet-nl.
the class AsyncDynamicUriBasicTest method testAsyncRequest.
/**
* Test Async request
* @throws Exception should not happen in this test
*/
@Test(timeout = 15000L)
public void testAsyncRequest() throws Exception {
MuleEventSpy spy = new MuleEventSpy("handler_spy");
spy.clear();
Event result = flowRunner(flowName).withPayload("nothing_important").withVariable("code", requestCode).withVariable("host", host).withVariable("port", port).withVariable("path", path).run();
Message response = result.getMessage();
assertTrue("wrong response payload", response.getPayload().getDataType().isCompatibleWith(DataType.STRING));
assertEquals("wrong response payload", "nothing_important", (String) response.getPayload().getValue());
// let handler do its asynchronous work
await().atMost(10, TimeUnit.SECONDS).until(() -> {
return spy.getEvents().size() == 1;
});
// assertions
response = (Message) spy.getEvents().get(0).getContent();
assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
assertEquals("wrong request code", requestCode, attributes.getRequestCode());
assertEquals("wrong request uri", expectedRequestUri, attributes.getRequestUri());
assertEquals("wrong response code", expectedResponseCode, attributes.getResponseCode());
byte[] responsePayload = (byte[]) TypedValue.unwrap(response.getPayload());
assertEquals("wrong response payload", expectedPayload, responsePayload == null ? "" : new String(responsePayload, Defs.COAP_CHARSET));
}
Aggregations