/** * Test the creation of a single entity with generated UUID and given name. This method also tests * the contents of the JSON response and reasonable query and serialization time. */ public void test020CreateNamedTestObject() { // create named object String location = RestAssured.given() .contentType("application/json; charset=UTF-8") .body(" { \"name\" : \"test\" } ") .expect() .statusCode(201) .when() .post("/test_objects") .getHeader("Location"); // POST must return a Location header assertNotNull(location); String uuid = getUuidFromLocation(location); // POST must create a UUID assertNotNull(uuid); assertFalse(uuid.isEmpty()); assertTrue(uuid.matches("[a-f0-9]{32}")); // check for exactly one object RestAssured.given() .contentType("application/json; charset=UTF-8") .expect() .statusCode(200) .body("result_count", equalTo(1)) .body("query_time", lessThan("0.5")) .body("serialization_time", lessThan("0.05")) .body("result", isEntity(TestObject.class)) .when() .get("/test_objects/" + uuid); }
@Test public void sessionIMChatAccept18() throws JsonGenerationException, JsonMappingException, IOException { Setup.startTest("Checking that User 4 has received composing notification"); RestAssured.authentication = RestAssured.DEFAULT_AUTH; String url = (Setup.channelURL[4].split("username\\=")[0]) + "username="******"notificationList.messageNotification[0].isComposing.refresh", Matchers.equalTo(60), "notificationList.messageNotification[0].isComposing.status", Matchers.equalTo("idle"), "notificationList.messageNotification[0].sessionId", Matchers.equalTo(receiveSessionID), "notificationList.messageNotification[0].senderAddress", Matchers.containsString(Setup.cleanPrefix(Setup.TestUser3Contact))) .post(url); System.out.println("Response = " + response.getStatusCode() + " / " + response.asString()); Setup.endTest(); }
@Test public void sessionIMChatAccept13() throws JsonGenerationException, JsonMappingException, IOException { Setup.majorTest("Chat/IM", "isComposing functionality between users 3 and 4"); Setup.startTest("Send isComposing from User 3"); IsComposing isComposing = new IsComposing("active", new java.util.Date(), "text/plain", 60); ObjectMapper mapper = new ObjectMapper(); String jsonRequestData = "{\"isComposing\":" + mapper.writeValueAsString(isComposing) + "}"; System.out.println("Sending " + jsonRequestData); RestAssured.authentication = RestAssured.basic(Setup.TestUser3, Setup.applicationPassword); Response resp = RestAssured.given() .contentType("application/json") .body(jsonRequestData) .expect() .log() .ifError() .statusCode(204) .post(Setup.sendIMURL(Setup.TestUser3, Setup.TestUser4Contact, sessionID)); System.out.println("Response = " + resp.getStatusCode() + " / " + resp.asString()); Setup.endTest(); }
@Test public void sessionIMChatAccept23() throws JsonGenerationException, JsonMappingException, IOException { Setup.startTest("Checking that User 3 has received chat closed notification"); System.out.println("sessionID=" + sessionID); System.out.println("receiveSessionID=" + receiveSessionID); RestAssured.authentication = RestAssured.DEFAULT_AUTH; String url = (Setup.channelURL[3].split("username\\=")[0]) + "username="******"notificationList.chatEventNotification.eventType", Matchers.hasItem("SessionEnded"), "notificationList.chatEventNotification.sessionId", Matchers.hasItem(sessionID), "notificationList.chatEventNotification.link[0].rel", Matchers.hasItem("ChatSessionInformation")) .post(url); System.out.println("Response = " + response.getStatusCode() + " / " + response.asString()); Setup.endTest(); }
@Test public void sessionIMChatAccept8() throws JsonGenerationException, JsonMappingException, IOException { Setup.startTest("Checking that User 3 has received a chat message"); receiveMessageStatusURL = INVALID; RestAssured.authentication = RestAssured.DEFAULT_AUTH; String url = (Setup.channelURL[3].split("username\\=")[0]) + "username="******"notificationList.messageNotification.chatMessage.text", Matchers.hasItem("I am good"), "notificationList.messageNotification.sessionId", Matchers.hasItem(sessionID), "notificationList.messageNotification.messageId", Matchers.notNullValue(), "notificationList.messageNotification.senderAddress", Matchers.hasItem(Setup.TestUser4Contact), "notificationList.messageNotification.link", Matchers.notNullValue()) .post(url); System.out.println("Response = " + response.getStatusCode() + " / " + response.asString()); receiveMessageStatusURL = response.jsonPath().getString("notificationList.messageNotification[0].link[0].href"); System.out.println("message status URL = " + receiveMessageStatusURL); try { Thread.sleep(1000); } catch (InterruptedException e) { } Setup.endTest(); }
@Test public void adhocIMChat6() throws JsonGenerationException, JsonMappingException, IOException { Setup.startTest("Checking that User 1 has received a chat message"); receiveMessageStatusURL = INVALID; String url = (Setup.channelURL[1].split("username\\=")[0]) + "username="******"notificationList.messageNotification.chatMessage.text", Matchers.hasItem("how are you"), "notificationList.messageNotification.sessionId", Matchers.hasItem("adhoc"), "notificationList.messageNotification.messageId", Matchers.notNullValue(), "notificationList.messageNotification.senderAddress", Matchers.hasItem(Setup.TestUser2Contact), "notificationList.messageNotification.link", Matchers.notNullValue()) .post(url); System.out.println("Response = " + response.getStatusCode() + " / " + response.asString()); receiveMessageStatusURL = response.jsonPath().getString("notificationList.messageNotification[0].link[0].href"); System.out.println("message status URL = " + receiveMessageStatusURL); Setup.endTest(); }
@Test public void sessionIMChatAccept5() { Setup.startTest("Checking IM notifications for user 4"); String url = (Setup.channelURL[4].split("username\\=")[0]) + "username="******"notificationList.messageNotification[0].senderAddress", Matchers.containsString(Setup.cleanPrefix(Setup.TestUser3Contact)), "notificationList.messageNotification[0].chatMessage.text", Matchers.equalTo("hello user4")) .post(url); System.out.println( "Response = " + notifications4.getStatusCode() + " / " + notifications4.asString()); /* * {"notificationList":[{"messageNotification": * {"callbackData":"GSMA3","link": * [{"rel":"MessageStatusReport","href":"http://api.oneapi-gw.gsma.com/chat/0.1/%2B15554000004/oneToOne/tel%3A%2B15554000003/-797939895/messages/1352475373833-1790113032/status"}], * "dateTime":"2012-11-09T15:36:13Z","chatMessage":{"text":"hello user4"},"sessionId":"-797939895","messageId":"1352475373833-1790113032", * "senderAddress":"tel:+15554000003"}}]} */ try { Thread.sleep(1000); } catch (InterruptedException e) { } Setup.endTest(); }
private void subscribeToAddressBookNotifications(String userID, int i) { String test = "Subscribing User 1 to Address Book Notifications"; startTest(test); String clientCorrelator = Long.toString(System.currentTimeMillis()); String callback = callbackURL[i]; String requestData = requestDataClean(addressBookRequestData, clientCorrelator, callback); String url = replace(addressBookSubscriptionURL, apiVersion, userID); RestAssured.authentication = RestAssured.basic(userID, applicationPassword); Response response = RestAssured.given() .contentType("application/json") .body(requestData) .expect() .log() .ifError() .statusCode(201) .post(url); JsonPath jsonData = response.jsonPath(); subscriptionURL[i] = jsonData.getString("abChangesSubscription.resourceURL"); LOGGER.info("Response received = " + response.getStatusCode()); LOGGER.info("Body = " + response.asString()); endTest(test); }
@Test public void sessionIMChatAccept2() { Setup.startTest("Checking IM notifications for user 3"); RestAssured.authentication = RestAssured.DEFAULT_AUTH; String url = (Setup.channelURL[3].split("username\\=")[0]) + "username="******"notificationList", Matchers.notNullValue()) .post(url); System.out.println( "Response = " + notifications3.getStatusCode() + " / " + notifications3.asString()); /* * {"notificationList":[{"messageStatusNotification": {"callbackData":"GSMA3","link": [{"rel":"ChatMessage","href":"http://api.oneapi-gw.gsma.com/chat/0.1/%2B15554000003/oneToOne/tel%3A%2B15554000004//messages/1352666437776-470267184"}], * "status":"Delivered","messageId":"1352666437776-470267184"}}, * {"messageStatusNotification": {"callbackData":"GSMA3","link": [{"rel":"ChatMessage","href":"http://api.oneapi-gw.gsma.com/chat/0.1/%2B15554000003/oneToOne/tel%3A%2B15554000004//messages/1352666437776-470267184"}], * "status":"Displayed","messageId":"1352666437776-470267184"}}, * {"chatEventNotification": {"callbackData":"GSMA3","link": [{"rel":"ChatSessionInformation","href":"http://api.oneapi-gw.gsma.com/chat/0.1/%2B15554000003/oneToOne/tel%3A%2B15554000004/373305033"}], * "eventType":"Accepted","sessionId":"373305033"}}]} */ JsonPath jsonData = notifications3.jsonPath(); sentMessageID = jsonData.getString("notificationList.messageStatusNotification[0].messageId"); System.out.println("Extracted messageId=" + sentMessageID); sendSessionURL = jsonData.getString("notificationList.chatEventNotification.link[0].href[0]"); System.out.println("Extracted sendSessionURL=" + sendSessionURL); sessionID = jsonData.getString("notificationList.chatEventNotification.sessionId[0]"); System.out.println("Extracted sessionId=" + sessionID); Setup.endTest(); }
@Test public void sessionIMChatAccept1A() { Setup.startTest("Checking IM notifications for user 4"); String url = (Setup.channelURL[4].split("username\\=")[0]) + "username="******"notificationList.chatSessionInvitationNotification", Matchers.notNullValue(), "notificationList.messageNotification.dateTime", Matchers.notNullValue(), "notificationList.messageNotification.messageId", Matchers.notNullValue(), "notificationList.messageNotification.senderAddress[0]", Matchers.containsString(Setup.cleanPrefix(Setup.TestUser3Contact))) .post(url); System.out.println( "Response = " + notifications4.getStatusCode() + " / " + notifications4.asString()); JsonPath jsonData = notifications4.jsonPath(); receiveSessionURL = jsonData.getString("notificationList.chatSessionInvitationNotification[0].link[0].href"); System.out.println("Extracted receiveSessionURL=" + receiveSessionURL); receiveSessionID = jsonData.getString("notificationList.messageNotification.sessionId[0]"); System.out.println("Extracted receiveSessionID=" + receiveSessionID); receiveMessageID = jsonData.getString("notificationList.messageNotification.messageId[0]"); System.out.println("Extracted receiveMessageID=" + receiveMessageID); Setup.endTest(); }
@Test public void sessionIMChatAccept21() throws JsonGenerationException, JsonMappingException, IOException { Setup.startTest("Send invalid isComposing from User 4"); IsComposing isComposing = new IsComposing("rubbish", new java.util.Date(), "text/plain", 60); ObjectMapper mapper = new ObjectMapper(); String jsonRequestData = "{\"isComposing\":" + mapper.writeValueAsString(isComposing) + "}"; System.out.println("Sending " + jsonRequestData); RestAssured.authentication = RestAssured.basic(Setup.TestUser4, Setup.applicationPassword); Response resp = RestAssured.given() .contentType("application/json") .body(jsonRequestData) .expect() .log() .ifError() .statusCode(400) .post(Setup.sendIMURL(Setup.TestUser4, Setup.TestUser3Contact, receiveSessionID)); /* * 400 / {"requestError": {"serviceException": { * "messageId":"SVC002","variables": ["State is not valid. Received 'rubbish'"],"text":"Invalid input value for message. Part %0"}}} */ System.out.println("Response = " + resp.getStatusCode() + " / " + resp.asString()); Setup.endTest(); }
/** * Create a RequestSpecification object as template, with host/port, user/pass, and certificate * info. * * @throws Exception in case of any issue */ @Override public void connect() throws Exception { String host = sysConfig.getProperty(RestCommunication.SYSPROP_HOST, "localhost"); int port = sysConfig.getIntProperty(RestCommunication.SYSPROP_PORT, 443); RestAssured.useRelaxedHTTPSValidation(); this.reqSpec = RestAssured.given(); if (port % 1000 == 443) { this.reqSpec = this.reqSpec.baseUri("https://" + host + ":" + port); } else { this.reqSpec = this.reqSpec.baseUri("http://" + host + ":" + port); } String user = sysConfig.getProperty(RestCommunication.SYSPROP_USER); String pass = sysConfig.getProperty(RestCommunication.SYSPROP_PASS); if (null != user && null != pass) { this.reqSpec = this.reqSpec.auth().preemptive().basic(user, pass); } String clientCert = sysConfig.getProperty(RestCommunication.SYSPROP_CLIENT_CERT); String clientCertPass = sysConfig.getProperty(RestCommunication.SYSPROP_CLIENT_CERT_PASS); if (null != clientCert && null != clientCertPass) { this.reqSpec = this.reqSpec.auth().certificate(clientCert, clientCertPass); } }
/** Test the correct response for a non-existing entity */ public void test000NotFoundError() { // provoke 404 error with GET on non-existing resource RestAssured.given() .contentType("application/json; charset=UTF-8") .expect() .statusCode(404) .when() .get("/test_objects/abc123def456abc123def456abc123de"); }
/** Test the creation of a single unnamed entity. */ public void test010CreateEmptyTestObject() { // create empty object String location = RestAssured.given() .contentType("application/json; charset=UTF-8") .expect() .statusCode(201) .when() .post("/test_objects") .getHeader("Location"); // POST must return a Location header assertNotNull(location); String uuid = getUuidFromLocation(location); // POST must create a UUID assertNotNull(uuid); assertFalse(uuid.isEmpty()); assertTrue(uuid.matches("[a-f0-9]{32}")); // check for exactly one object Object name = RestAssured.given() .contentType("application/json; charset=UTF-8") .expect() .statusCode(200) .body("result_count", equalTo(1)) .body("query_time", lessThan("0.1")) .body("serialization_time", lessThan("0.02")) .body("result.id", equalTo(uuid)) .when() .get("/test_objects/" + uuid) .jsonPath() .get("result.name"); System.out.println("name (should be null): " + name); // name must be null assertNull(name); }
/** * Tests creation of a new customer. Expected result: HTTP status 200 and a response containing * JSON data should be returned. * * @throws Exception If error occurs. Indicates test failure. */ @Test public void testCreateNewCustomer() throws Exception { RestAssured.given() .contentType("application/json") .accept("application/json") .body(NEW_CUSTOMER_JSON) .when() .post(CustomersResource.CUSTOMERS_RESOURCE_PATH) .then() .statusCode(200) .contentType(ContentType.JSON); }
@Test public void metodoNoPermitido() { Response response = restAssured .given() .contentType(ContentType.JSON) .body(sitio) .when() .put("GsiamWeb2/sitios/agregar"); Assert.assertEquals(405, response.getStatusCode()); }
@Test public void testSubjectMustNotBePresent_subjectIsPresent_controllerExplicitlyUnrestricted() { running( testServer(PORT, app()), () -> { RestAssured.given() .cookie("user", "greet") .expect() .statusCode(401) .when() .get("/subject/not/present/m/subjectMustNotBePresentInUnrestrictedController"); }); }
@Test public void errorUrlCrearUsuario() { Response response = restAssured .given() .contentType(ContentType.JSON) .body(sitio) .when() .post("GsiamWeb2/sitios/agrega"); Assert.assertEquals(404, response.getStatusCode()); }
@Test public void testSubjectNotMustBePresent_subjectIsPresent() { running( testServer(PORT, app()), () -> { RestAssured.given() .cookie("user", "greet") .expect() .statusCode(401) .when() .get("/subject/not/present/m/subjectMustNotBePresent"); }); }
@Test public void adhocIMChat8() { Setup.startTest("Checking IM notifications for user 2"); RestAssured.authentication = RestAssured.basic(Setup.TestUser2, Setup.applicationPassword); String url = (Setup.channelURL[2].split("username\\=")[0]) + "username="******"notificationList.chatEventNotification.eventType", Matchers.hasItem("Accepted"), "notificationList.chatEventNotification.sessionId", Matchers.hasItem("adhoc") ).*/ post(url); System.out.println( "Response = " + notifications2.getStatusCode() + " / " + notifications2.asString()); Setup.endTest(); }
@Test public void adhocIMChat2() { Setup.startTest("Checking the sender status"); RestAssured.authentication = RestAssured.basic(Setup.TestUser1, Setup.applicationPassword); Response status = RestAssured.given() .expect() .log() .ifError() .statusCode(200) .body("messageStatusReport.status", Matchers.is("DISPLAYED")) .get(sendMessageStatusURL); System.out.println("Response = " + status.getStatusCode() + " / " + status.asString()); Setup.endTest(); }
@Test public void sessionIMChatAccept10() { Setup.startTest("Checking the receiver status"); RestAssured.authentication = RestAssured.basic(Setup.TestUser4, Setup.applicationPassword); Response status = RestAssured.given() .expect() .log() .ifError() .statusCode(200) .body("messageStatusReport.status", Matchers.equalTo("DISPLAYED")) .get(Setup.prepareForTest(receiveMessageStatusURL)); System.out.println("Response = " + status.getStatusCode() + " / " + status.asString()); Setup.endTest(); }
@Test public void sessionIMChatAccept22() throws JsonGenerationException, JsonMappingException, IOException { Setup.startTest("Close chat by User 4"); RestAssured.authentication = RestAssured.basic(Setup.TestUser4, Setup.applicationPassword); Response resp = RestAssured.given() .expect() .log() .ifError() .statusCode(204) .delete( Setup.chatSessionIMURL(Setup.TestUser4, Setup.TestUser3Contact, receiveSessionID)); System.out.println("Response = " + resp.getStatusCode() + " / " + resp.asString()); Setup.endTest(); }
@Test public void adhocIMChat4() { Setup.startTest("Checking no further IM notifications for user 2 "); String url = (Setup.channelURL[2].split("username\\=")[0]) + "username="******"notificationList", Matchers.nullValue()) .post(url); System.out.println( "Response = " + notifications2.getStatusCode() + " / " + notifications2.asString()); Setup.endTest(); }
@Test public void sessionIMChatAccept6() { Setup.startTest("Checking IM notifications for user 3"); System.out.println("Expecting messageId=" + sentMessageID); String url = (Setup.channelURL[3].split("username\\=")[0]) + "username="******"notificationList.messageStatusNotification", Matchers.notNullValue()) .post(url); System.out.println( "Response = " + notifications3.getStatusCode() + " / " + notifications3.asString()); Setup.endTest(); }
@Test public void updateSubscriptionForUser1() { String userID = user1; int i = 1; startNotificationChannel(userID, i); subscribeToAddressBookNotifications(userID, i); String test = "Updating/Extending the Subscription to Address Book Notifications for User 1"; startTest(test); String clientCorrelator = Long.toString(System.currentTimeMillis()); String callback = callbackURL[i]; String requestData = requestDataClean(updateAddressBookRequestData, clientCorrelator, callback); String cleanUserID = cleanPrefix(userID); String url = subscriptionURL[i]; url = prepare(url); RestAssured.authentication = RestAssured.basic(userID, applicationPassword); Response response = RestAssured.given() .contentType("application/json") .body(requestData) .expect() .log() .ifError() .statusCode(200) .body( "abChangesSubscription.resourceURL", StringContains.containsString(cleanUserID), "abChangesSubscription.callbackReference.callbackData", IsEqual.equalTo("GSMA1"), "abChangesSubscription.callbackReference.notifyURL", IsEqual.equalTo(callbackURL[i]), "abChangesSubscription.callbackReference.notificationFormat", IsEqual.equalTo("JSON")) .put(url); LOGGER.info("Response Received = " + response.getStatusCode()); LOGGER.info("Body = " + response.asString()); endTest(test); }
@Override protected RequestSpecification buildBaseRequestSpecification() { RequestSpecification spec = RestAssured.given(); if (auth != null) { spec.auth().preemptive().basic(auth.get("username"), auth.get("password")); } if (httpLog != null) { spec.config( RestAssured.config() .logConfig( logConfig() .enableLoggingOfRequestAndResponseIfValidationFails() .defaultStream(new PrintStream(new WriterOutputStream(httpLog))) .enablePrettyPrinting(true))); } return spec; }
@Test public void sessionIMChatAccept4() throws JsonGenerationException, JsonMappingException, IOException { Setup.startTest("Sending IM chat from User 3 to User 4"); ChatMessage chatMessage = new ChatMessage("hello user4", "Displayed"); ObjectMapper mapper = new ObjectMapper(); String jsonRequestData = "{\"chatMessage\":" + mapper.writeValueAsString(chatMessage) + "}"; System.out.println("Sending json=" + jsonRequestData); RestAssured.authentication = RestAssured.basic(Setup.TestUser3, Setup.applicationPassword); Response resp = RestAssured.given() .contentType("application/json") .body(jsonRequestData) .expect() .log() .ifError() .statusCode(201) .body( "resourceReference.resourceURL", StringContains.containsString( Setup.encodedValue(Setup.TestUser3) + "/oneToOne/" + Setup.encodedValue(Setup.TestUser4Contact) + "/" + sessionID + "/messages/")) .post(Setup.sendIMURL(Setup.TestUser3, Setup.TestUser4Contact, sessionID)); sendMessageStatusURL = resp.jsonPath().getString("resourceReference.resourceURL"); String[] parts = sendMessageStatusURL.split("/messages/"); sentMessageID = parts[1].replaceAll("/status", ""); System.out.println("Response = " + resp.getStatusCode() + " / " + resp.asString()); System.out.println("resourceURL = " + sendMessageStatusURL); System.out.println("sentMessageID = " + sentMessageID); try { Thread.sleep(1500); } catch (InterruptedException e) { } Setup.endTest(); }
@Test public void adhocIMChat3() { Setup.startTest("Checking IM notifications for user 1"); String url = (Setup.channelURL[1].split("username\\=")[0]) + "username="******"notificationList.chatEventNotification.eventType", Matchers.hasItem("Accepted"), "notificationList.chatEventNotification.sessionId", Matchers.hasItem("adhoc")) .post(url); System.out.println( "Response = " + notifications1.getStatusCode() + " / " + notifications1.asString()); Setup.endTest(); }
@Test public void unsubscribeUnsubscribedUserFromAddressBookSubscriptions() { String userID = user3; TestUtils.startNotificationChannel( userThree, notificationChannelURL, apiVersion, validLongPoll, applicationUsername, applicationPassword); // subscribeToAddressBookNotifications(userID, i); String test = "Unsubscribe User 1 from Address Book Subscriptions"; startTest(test); String url = replace(addressBookSubscriptionURL, apiVersion, userID); url = url + "/" + userID; RestAssured.authentication = RestAssured.basic(userID, applicationPassword); Response response = RestAssured.given() .auth() .preemptive() .basic(applicationUsername, applicationPassword) .expect() .log() .ifError() .statusCode(403) .delete(url); LOGGER.info("Received Response: " + response.getStatusCode()); JsonPath jsonData = response.jsonPath(); String errorCode = jsonData.get("requestError.serviceException.messageId"); String errorMessage = jsonData.get("requestError.serviceException.variables[0]"); LOGGER.info("Error Code: " + errorCode); LOGGER.info("Error Message: " + errorMessage); endTest(test); }