Introduce wrappers for API test results.

This commit is contained in:
Thomas Wiese committed 2019-04-15 14:34:20 +02:00
1 parent a5588a4f54
commit 04f47f4e10
10 files changed
+250 -76

No files matched your search

@@ -25,7 +25,7 @@ public class ApiContext {
public static final String SERVER_CONTEXT = "sailingserver"; //$NON-NLS-1$
public static final String SECURITY_CONTEXT = "security"; //$NON-NLS-1$
private static final Logger logger = Logger.getLogger(ApiContext.class.getName());
/** Jax-RS client. */
@@ -0,0 +1,21 @@
package com.sap.sailing.selenium.api.core;
import org.json.simple.JSONObject;
public class JsonWrapper {
private JSONObject json;
public JsonWrapper(JSONObject json) {
this.json = json;
}
public JSONObject getJson() {
return json;
}
@SuppressWarnings("unchecked")
public <T> T get(String key) {
return (T) json.get(key);
}
}
@@ -5,26 +5,81 @@ import java.util.TreeMap;
import org.json.simple.JSONObject;
import com.sap.sailing.domain.common.CompetitorRegistrationType;
import com.sap.sailing.selenium.api.core.ApiContext;
import com.sap.sailing.selenium.api.core.JsonWrapper;
public class EventApi {
private static final String CREATE_EVENT_URL = "/api/v1/events/createEvent";
private static final String LIST_EVENTS = "/api/v1/events";
public JSONObject createEvent(ApiContext ctx, String eventName, String boatclassname,
String competitorRegistrationType, String venuename) {
public Event createEvent(ApiContext ctx, String eventName, String boatclassname,
CompetitorRegistrationType competitorRegistrationType, String venuename) {
Map<String, String> formParams = new TreeMap<>();
formParams.put("boatclassname", boatclassname);
formParams.put("competitorRegistrationType", competitorRegistrationType);
formParams.put("competitorRegistrationType", competitorRegistrationType.name());
formParams.put("eventName", eventName);
formParams.put("venuename", venuename);
return ctx.post(CREATE_EVENT_URL, null, formParams);
EventForCreate event = new EventForCreate(ctx.post(CREATE_EVENT_URL, null, formParams));
return event;
}
public JSONObject getEvent(ApiContext ctx, String eventId) {
return ctx.get(LIST_EVENTS + "/" + eventId);
public Event getEvent(ApiContext ctx, String eventId) {
Event event = new Event(ctx.get(LIST_EVENTS + "/" + eventId));
return event;
}
public class Event extends JsonWrapper {
private Event(JSONObject json) {
super(json);
}
public String getId() {
return get("id");
}
public String getName() {
return get("name");
}
public String getSecret() {
return null;
}
public Long getStartDate() {
return get("startDate");
}
public Long getEndDate() {
return get("endDate");
}
}
private class EventForCreate extends Event {
private EventForCreate(JSONObject json) {
super(json);
}
public String getId() {
return get("eventid");
}
public String getName() {
return get("eventname");
}
public String getSecret() {
return get("registrationSecret");
}
public Long getStartDate() {
return get("eventstartdate");
}
public Long getEndDate() {
return (Long) get("eventenddate");
}
}
}
@@ -1,12 +1,15 @@
package com.sap.sailing.selenium.api.event;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import com.sap.sailing.domain.common.CompetitorRegistrationType;
import com.sap.sailing.selenium.api.core.ApiContext;
import com.sap.sailing.selenium.api.core.JsonWrapper;
public class RegattaApi {
@@ -15,8 +18,9 @@ public class RegattaApi {
private static final String COMPETITOR_CREATE_AND_ADD_WITH_BOAT = "/competitors/createandadd";
private static final String ADD_RACE_COLUMN_URL = "/addracecolumns";
public JSONObject getRegatta(ApiContext ctx, String regattaName) {
return ctx.get(REGATTAS + "/" + regattaName);
public Regatta getRegatta(ApiContext ctx, String regattaName) {
Regatta regatta = new Regatta(ctx.get(REGATTAS + "/" + regattaName));
return regatta;
}
public JSONObject getRegattaRaces(ApiContext ctx, String regattaName) {
@@ -33,7 +37,7 @@ public class RegattaApi {
queryParams.put("nationalityIOC", nationalityIOC);
return ctx.post(url, queryParams);
}
public JSONObject createAndAddCompetitorWithSecret(ApiContext ctx, String regattaName, String boatclass,
String competitorEmail, String competitorName, String nationalityIOC, String secret, String deviceUuid) {
String url = REGATTAS + "/" + regattaName + COMPETITOR_CREATE_AND_ADD_WITH_BOAT;
@@ -46,13 +50,52 @@ public class RegattaApi {
queryParams.put("deviceUuid", deviceUuid);
return ctx.post(url, queryParams);
}
public JSONArray addRaceColumn(ApiContext ctx, String regattaName, String prefix, Integer numberOfRaces) {
String url = REGATTAS + "/" + regattaName + ADD_RACE_COLUMN_URL;
Map<String, String> queryParams = new TreeMap<>();
queryParams.put("prefix", prefix);
queryParams.put("numberOfRaces", numberOfRaces != null ? numberOfRaces.toString() : null);
return ctx.post(url, queryParams);
}
public class Regatta extends JsonWrapper {
public Regatta(JSONObject json) {
super(json);
}
public String getName() {
return get("name");
}
public Date getStartDate() {
return get("startDate");
}
public Date getEndDate() {
return get("endDate");
}
public String getBoatClass() {
return get("boatclass");
}
public String getScoringSystem() {
return get("scoringSystem");
}
public String getCourseAreaId() {
return get("courseAreaId");
}
public Boolean canBoatsOfCompetitorsChangePerRace() {
return get("canBoatsOfCompetitorsChangePerRace");
}
public CompetitorRegistrationType getCompetitorRegistrationType() {
return CompetitorRegistrationType.valueOf(get("competitorRegistrationType"));
}
}
}
@@ -6,6 +6,7 @@ import java.util.Map;
import org.json.simple.JSONObject;
import com.sap.sailing.selenium.api.core.ApiContext;
import com.sap.sailing.selenium.api.core.JsonWrapper;
public class SecurityApi {
@@ -13,7 +14,7 @@ public class SecurityApi {
private static final String GET_USER_URL = "/api/restsecurity/user";
private static final String SAY_HELLO_URL = "/api/restsecurity/hello";
public JSONObject createUser(ApiContext ctx, String username, String fullName, String company,
public AccessToken createUser(ApiContext ctx, String username, String fullName, String company,
/* String email, */ String password) {
Map<String, String> queryParams = new HashMap<>();
queryParams.put("username", username);
@@ -21,16 +22,64 @@ public class SecurityApi {
queryParams.put("company", company);
// queryParams.put("email", email); //if email is provided a validation mail would be sent
queryParams.put("password", password);
return ctx.post(CREATE_USER_URL, queryParams);
AccessToken accessToken = new AccessToken(ctx.post(CREATE_USER_URL, queryParams));
return accessToken;
}
public JSONObject getUser(ApiContext ctx, String username) {
public User getUser(ApiContext ctx, String username) {
Map<String, String> queryParams = new HashMap<>();
queryParams.put("username", username);
return ctx.get(GET_USER_URL, queryParams);
User user = new User(ctx.get(GET_USER_URL, queryParams));
return user;
}
public JSONObject sayHello(ApiContext ctx) {
return ctx.get(SAY_HELLO_URL);
public Hello sayHello(ApiContext ctx) {
Hello hello = new Hello(ctx.get(SAY_HELLO_URL));
return hello;
}
public class AccessToken extends JsonWrapper {
public AccessToken(JSONObject json) {
super(json);
}
public String getUsername() {
return get("username");
}
public String getAccessToken() {
return get("access_token");
}
}
public class User extends JsonWrapper {
public User(JSONObject json) {
super(json);
}
public String getUsername() {
return get("username");
}
public String getFullName() {
return get("fullname");
}
}
public class Hello extends JsonWrapper {
public Hello(JSONObject json) {
super(json);
}
public String getPrincipal() {
return get("principal");
}
public Boolean isAuthenticated() {
return get("authenticated");
}
}
}
@@ -8,8 +8,10 @@ import org.json.simple.JSONObject;
import org.junit.Before;
import org.junit.Test;
import com.sap.sailing.domain.common.CompetitorRegistrationType;
import com.sap.sailing.selenium.api.core.ApiContext;
import com.sap.sailing.selenium.api.event.EventApi;
import com.sap.sailing.selenium.api.event.EventApi.Event;
import com.sap.sailing.selenium.test.AbstractSeleniumTest;
public class EventApiTest extends AbstractSeleniumTest {
@@ -26,23 +28,25 @@ public class EventApiTest extends AbstractSeleniumTest {
String eventName = "<ppp> loggingsession";
EventApi eventApi = new EventApi();
JSONObject createdEvent = eventApi.createEvent(ctx, eventName, "75QMNATIONALEKREUZER", "CLOSED", "default");
assertNotNull("create: event.eventid is missing", createdEvent.get("eventid"));
assertNotNull("create: event.eventstartdate is missing", createdEvent.get("eventstartdate"));
assertNotNull("create: event.eventenddate is missing", createdEvent.get("eventenddate"));
assertEquals("create: event.eventname is different", eventName, createdEvent.get("eventname"));
Event createdEvent = eventApi.createEvent(ctx, eventName, "75QMNATIONALEKREUZER",
CompetitorRegistrationType.CLOSED, "default");
assertNotNull("create: event.eventid is missing", createdEvent.getId());
assertNotNull("create: event.eventstartdate is missing", createdEvent.getStartDate());
assertNotNull("create: event.eventenddate is missing", createdEvent.getEndDate());
assertEquals("create: event.eventname is different", eventName, createdEvent.getName());
assertEquals("create: event.reagattaname is different", eventName, createdEvent.get("regatta"));
assertEquals("create: event.leaderboard is different", eventName, createdEvent.get("leaderboard"));
JSONObject foundEvent = eventApi.getEvent(ctx, (String) createdEvent.get("eventid"));
assertNotNull("read: event.id is missing", foundEvent.get("id"));
assertEquals("read: event.name is different", eventName, foundEvent.get("name"));
Event foundEvent = eventApi.getEvent(ctx, (String) createdEvent.getId());
assertNotNull("read: event.id is missing", foundEvent.getId());
assertEquals("read: event.name is different", eventName, foundEvent.getName());
assertEquals("read: event.description is different", eventName, foundEvent.get("description"));
assertEquals("read: event.officialWebsiteURL is different", null, foundEvent.get("officialWebsiteURL"));
assertEquals("read: event.officialWebsiteURL is different", null,
(String) foundEvent.get("officialWebsiteURL"));
assertEquals("read: event.baseUrl is different", getContextRoot() + ApiContext.SERVER_CONTEXT + "/api/",
foundEvent.get("baseURL"));
assertNotNull("read: event.startDate is missing", foundEvent.get("startDate"));
assertNotNull("read: event.endDate is missing", foundEvent.get("endDate"));
assertNotNull("read: event.startDate is missing", foundEvent.getStartDate());
assertNotNull("read: event.endDate is missing", foundEvent.getEndDate());
assertEquals("read: event.images should be empty", 0, ((JSONArray) foundEvent.get("images")).size());
assertEquals("read: event.videos should be empty", 0, ((JSONArray) foundEvent.get("videos")).size());
assertEquals("read: event.sailorsInfoWebsiteURLs", 0,
@@ -11,6 +11,7 @@ import org.json.simple.JSONObject;
import org.junit.Before;
import org.junit.Test;
import com.sap.sailing.domain.common.CompetitorRegistrationType;
import com.sap.sailing.selenium.api.core.ApiContext;
import com.sap.sailing.selenium.api.event.EventApi;
import com.sap.sailing.selenium.api.event.LeaderboardApi;
@@ -33,7 +34,8 @@ public class LeaderBoardApiTest extends AbstractSeleniumTest {
EventApi eventApi = new EventApi();
LeaderboardApi leaderboardApi = new LeaderboardApi();
eventApi.createEvent(ctx, leaderboardName, "75QMNATIONALEKREUZER", "CLOSED", "default");
eventApi.createEvent(ctx, leaderboardName, "75QMNATIONALEKREUZER", CompetitorRegistrationType.CLOSED,
"default");
JSONObject leaderBoard = leaderboardApi.getLeaderboard(ctx, leaderboardName);
assertEquals("read: leaderboard.name is different", leaderboardName, leaderBoard.get("name"));
@@ -62,20 +64,21 @@ public class LeaderBoardApiTest extends AbstractSeleniumTest {
RegattaApi regattaApi = new RegattaApi();
LeaderboardApi leaderboardApi = new LeaderboardApi();
eventApi.createEvent(ctx, leaderboardName, "75QMNATIONALEKREUZER", "CLOSED", "default");
eventApi.createEvent(ctx, leaderboardName, "75QMNATIONALEKREUZER", CompetitorRegistrationType.CLOSED,
"default");
JSONObject competitor = regattaApi.createAndAddCompetitor(ctx, leaderboardName, "75QMNATIONALEKREUZER",
"test@de", "Max Mustermann", "USA");
String competitorId = competitor.get("id").toString();
String boatId = "sdjkfhsdkjfh!";
String markId = "kjashfkfhskh";
String deviceUuid = UUID.randomUUID().toString();
String secret = "dskjshfkdjhfksh";
Long fromMillis = System.currentTimeMillis();
@SuppressWarnings("unused")//TODO: check result
@SuppressWarnings("unused") // TODO: check result
JSONObject dmStart = leaderboardApi.deviceMappingsStart(ctx, leaderboardName, competitorId, boatId, markId,
deviceUuid, secret, fromMillis);
@SuppressWarnings("unused") //TODO: check result
@SuppressWarnings("unused") // TODO: check result
JSONObject dmEnd = leaderboardApi.deviceMappingsEnd(ctx, leaderboardName, competitorId, boatId, markId,
deviceUuid, secret, fromMillis);
}
@@ -1,11 +1,12 @@
package com.sap.sailing.selenium.api.test;
import org.json.simple.JSONObject;
import org.junit.Before;
import org.junit.Test;
import com.sap.sailing.domain.common.CompetitorRegistrationType;
import com.sap.sailing.selenium.api.core.ApiContext;
import com.sap.sailing.selenium.api.event.EventApi;
import com.sap.sailing.selenium.api.event.EventApi.Event;
import com.sap.sailing.selenium.api.event.RegattaApi;
import com.sap.sailing.selenium.api.event.SecurityApi;
import com.sap.sailing.selenium.pages.adminconsole.AdminConsolePage;
@@ -36,18 +37,11 @@ public class OpenRegattaTest extends AbstractSeleniumTest {
adminConsole.goToLcalServerPanel().setSelfServiceServer(true);
}
// Event erzeugen
// Competitor anlegen owner
// Competitor anlegen
// Start Race
// GPS (n*)
// End Race
@Test
public void simpleTest() {
JSONObject event = eventApi.createEvent(ownerCtx, EVENT_NAME, BOAT_CLASS, "OPEN_UNMODERATED",
"Duckburg Harbour");
String registrationLinkSecret = (String) event.get("registrationSecret");
Event event = eventApi.createEvent(ownerCtx, EVENT_NAME, BOAT_CLASS,
CompetitorRegistrationType.OPEN_UNMODERATED, "Duckburg Harbour");
String registrationLinkSecret = event.getSecret();
regattaApi.createAndAddCompetitor(ownerCtx, EVENT_NAME, BOAT_CLASS, "", "Donald Duck", "USA");
regattaApi.createAndAddCompetitorWithSecret(sailorCtx, EVENT_NAME, BOAT_CLASS, "", "Mickey Mouse", "USA",
registrationLinkSecret, "00000000-0000-0000-0000-000000000001");
@@ -9,16 +9,18 @@ import org.json.simple.JSONObject;
import org.junit.Before;
import org.junit.Test;
import com.sap.sailing.domain.common.CompetitorRegistrationType;
import com.sap.sailing.selenium.api.core.ApiContext;
import com.sap.sailing.selenium.api.event.EventApi;
import com.sap.sailing.selenium.api.event.RegattaApi;
import com.sap.sailing.selenium.api.event.RegattaApi.Regatta;
import com.sap.sailing.selenium.test.AbstractSeleniumTest;
public class RegattaApiTest extends AbstractSeleniumTest {
private static String EVENT_NAME = "<ppp> loggingsession";
private static String BOAT_CLASS = "75QMNATIONALEKREUZER";
@Before
public void setUp() {
clearState(getContextRoot());
@@ -28,27 +30,26 @@ public class RegattaApiTest extends AbstractSeleniumTest {
public void testGetRegattaForCreatedEvent() {
ApiContext ctx = ApiContext.createApiContext(getContextRoot(), ApiContext.SERVER_CONTEXT, "admin", "admin");
String competitorRegistrationType = "CLOSED";
EventApi eventApi = new EventApi();
RegattaApi regattaApi = new RegattaApi();
eventApi.createEvent(ctx, EVENT_NAME, BOAT_CLASS, competitorRegistrationType, "default");
JSONObject regatta = regattaApi.getRegatta(ctx, EVENT_NAME);
eventApi.createEvent(ctx, EVENT_NAME, BOAT_CLASS, CompetitorRegistrationType.CLOSED, "default");
Regatta regatta = regattaApi.getRegatta(ctx, EVENT_NAME);
JSONArray series = (JSONArray) regatta.get("series");
JSONObject serie = (JSONObject) series.get(0);
JSONArray fleets = (JSONArray) serie.get("fleets");
JSONObject trackedRaces = (JSONObject) serie.get("trackedRaces");
assertEquals("read: regatta.name is different", EVENT_NAME, regatta.get("name"));
assertNull("read: regatta.startDate should be null", regatta.get("startDate"));
assertNull("read: regatta.endDate should be null", regatta.get("endDate"));
assertEquals("read: regatta.scoringSystem is different", "LOW_POINT", regatta.get("scoringSystem"));
assertEquals("read: regatta.boeatclass is different", BOAT_CLASS, regatta.get("boatclass"));
assertNotNull("read: regatta.courseAreaId is missing", regatta.get("courseAreaId"));
assertEquals("read: regatta.name is different", EVENT_NAME, regatta.getName());
assertNull("read: regatta.startDate should be null", regatta.getStartDate());
assertNull("read: regatta.endDate should be null", regatta.getEndDate());
assertEquals("read: regatta.scoringSystem is different", "LOW_POINT", regatta.getScoringSystem());
assertEquals("read: regatta.boeatclass is different", BOAT_CLASS, regatta.getBoatClass());
assertNotNull("read: regatta.courseAreaId is missing", regatta.getCourseAreaId());
assertEquals("read: regatta.canBoatsOfCompetitorsChangePerRace should be false", false,
regatta.get("canBoatsOfCompetitorsChangePerRace"));
assertEquals("read: regatta.competitorRegistrationType is different", competitorRegistrationType,
regatta.get("competitorRegistrationType"));
regatta.canBoatsOfCompetitorsChangePerRace());
assertEquals("read: regatta.competitorRegistrationType is different", CompetitorRegistrationType.CLOSED,
regatta.getCompetitorRegistrationType());
assertEquals("read: reagtta.series should have 1 entry", 1, series.size());
assertEquals("read: reagtta.fleets should have 1 entry", 1, fleets.size());
@@ -61,7 +62,7 @@ public class RegattaApiTest extends AbstractSeleniumTest {
EventApi eventApi = new EventApi();
RegattaApi regattaApi = new RegattaApi();
eventApi.createEvent(ctx, EVENT_NAME, BOAT_CLASS, "CLOSED", "default");
eventApi.createEvent(ctx, EVENT_NAME, BOAT_CLASS, CompetitorRegistrationType.CLOSED, "default");
JSONObject regattaRaces = regattaApi.getRegattaRaces(ctx, EVENT_NAME);
JSONArray races = (JSONArray) regattaRaces.get("races");
@@ -75,20 +76,20 @@ public class RegattaApiTest extends AbstractSeleniumTest {
EventApi eventApi = new EventApi();
RegattaApi regattaApi = new RegattaApi();
eventApi.createEvent(ctx, EVENT_NAME, BOAT_CLASS, "CLOSED", "default");
eventApi.createEvent(ctx, EVENT_NAME, BOAT_CLASS, CompetitorRegistrationType.CLOSED, "default");
JSONObject competitor = regattaApi.createAndAddCompetitor(ctx, EVENT_NAME, BOAT_CLASS, "test@de",
"Max Mustermann", "USA");
assertNotNull("competitor.id is missing", competitor.get("id"));
}
@Test
public void testAddRaceColumns() {
ApiContext ctx = ApiContext.createApiContext(getContextRoot(), ApiContext.SERVER_CONTEXT, "admin", "admin");
EventApi eventApi = new EventApi();
RegattaApi regattaApi = new RegattaApi();
eventApi.createEvent(ctx, EVENT_NAME, BOAT_CLASS, "CLOSED", "default");
@SuppressWarnings("unused") //TODO: check result
eventApi.createEvent(ctx, EVENT_NAME, BOAT_CLASS, CompetitorRegistrationType.CLOSED, "default");
@SuppressWarnings("unused") // TODO: check result
JSONArray result = regattaApi.addRaceColumn(ctx, EVENT_NAME, "T", 5);
}
}
@@ -3,12 +3,14 @@ package com.sap.sailing.selenium.api.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.json.simple.JSONObject;
import org.junit.Before;
import org.junit.Test;
import com.sap.sailing.selenium.api.core.ApiContext;
import com.sap.sailing.selenium.api.event.SecurityApi;
import com.sap.sailing.selenium.api.event.SecurityApi.AccessToken;
import com.sap.sailing.selenium.api.event.SecurityApi.Hello;
import com.sap.sailing.selenium.api.event.SecurityApi.User;
import com.sap.sailing.selenium.test.AbstractSeleniumTest;
public class SecurityApiTest extends AbstractSeleniumTest {
@@ -23,25 +25,27 @@ public class SecurityApiTest extends AbstractSeleniumTest {
@Test
public void testCreateAndGetUser() {
ApiContext adminCtx = ApiContext.createApiContext(getContextRoot(), ApiContext.SECURITY_CONTEXT, "admin", "admin");
ApiContext adminCtx = ApiContext.createApiContext(getContextRoot(), ApiContext.SECURITY_CONTEXT, "admin",
"admin");
SecurityApi securityApi = new SecurityApi();
JSONObject createUserResponse = securityApi.createUser(adminCtx, "max", USERNAME_FULL, null, "start123");
AccessToken createUserResponse = securityApi.createUser(adminCtx, "max", USERNAME_FULL, null, "start123");
assertEquals("Responded username of createUser is different!", USERNAME, createUserResponse.get("username"));
assertNotNull("Token is missing in reponse!", createUserResponse.get("access_token"));
assertEquals("Responded username of createUser is different!", USERNAME, createUserResponse.getUsername());
assertNotNull("Token is missing in reponse!", createUserResponse.getAccessToken());
JSONObject getUserResponse = securityApi.getUser(adminCtx, USERNAME);
assertEquals("Responded username of getUser is different!", USERNAME, getUserResponse.get("username"));
User getUserResponse = securityApi.getUser(adminCtx, USERNAME);
assertEquals("Responded username of getUser is different!", USERNAME, getUserResponse.getUsername());
}
@Test
public void testSayHello() {
ApiContext adminCtx = ApiContext.createApiContext(getContextRoot(), ApiContext.SECURITY_CONTEXT, "admin", "admin");
ApiContext adminCtx = ApiContext.createApiContext(getContextRoot(), ApiContext.SECURITY_CONTEXT, "admin",
"admin");
SecurityApi securityApi = new SecurityApi();
JSONObject helloResponse = securityApi.sayHello(adminCtx);
assertEquals("Responded principal of hello is different!", "admin", helloResponse.get("principal"));
assertEquals("Responded authenticated of hello is different!", true, helloResponse.get("authenticated"));
Hello hello = securityApi.sayHello(adminCtx);
assertEquals("Responded principal of hello is different!", "admin", hello.getPrincipal());
assertEquals("Responded authenticated of hello is different!", true, hello.isAuthenticated());
}
}