fixed plain REST tests so they cope with StreamingOutput responses

This commit is contained in:
Axel Uhl
2020-04-28 10:29:29 +02:00
parent 94a85ad8c7
commit bf9ade1a4d
6 changed files with 59 additions and 54 deletions
@@ -4,6 +4,8 @@ import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
@@ -13,7 +15,9 @@ import java.util.Date;
import java.util.List;
import java.util.Optional;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
@@ -155,4 +159,21 @@ public abstract class AbstractJaxRsApiTest {
}
return result;
}
/**
* If the {@code entity} is a {@link StreamingOutput}, the stream will first be read and then converted to a
* {@link String}. Otherwise, the {@link Object#toString()} method is used to convert the {@code entity} into a
* string.
*/
protected String getEntityAsString(Object entity) throws WebApplicationException, IOException {
final String result;
if (entity instanceof StreamingOutput) {
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
((StreamingOutput) entity).write(bos);
return bos.toString("UTF8");
} else {
result = entity.toString();
}
return result;
}
}
@@ -28,12 +28,10 @@ public class BoatResourceTest extends AbstractJaxRsApiTest {
@Test
public void testGetBoatAsJson() throws Exception {
String jsonString = boatsResource.getBoat(id, null, null).getEntity().toString();
String jsonString = getEntityAsString(boatsResource.getBoat(id, null, null).getEntity());
BoatJsonDeserializer boatJsonDeserializer = BoatJsonDeserializer.create(racingEventService.getBaseDomainFactory());
JSONObject jsonObject = Helpers.toJSONObjectSafe(JSONValue.parse(jsonString));
DynamicBoat boat = boatJsonDeserializer.deserialize(jsonObject);
assertTrue(boat.getId().equals(id));
assertTrue(boat.getName().equals(boatName));
assertTrue(boat.getSailID().equals(sailId));
@@ -31,12 +31,11 @@ public class CompetitorsResourceTest extends AbstractJaxRsApiTest {
@Test
public void testGetCompetitorAsJson() throws Exception {
String jsonString = competitorsResource.getCompetitor(id, null, null).getEntity().toString();
String jsonString = getEntityAsString(competitorsResource.getCompetitor(id, null, null).getEntity());
JSONObject json = Helpers.toJSONObjectSafe(JSONValue.parse(jsonString));
assertTrue(json.get("id").equals(id));
assertTrue(json.get("name").equals(name));
assertTrue(json.get("nationality").equals(nationality));
assertTrue(json.get("countryCode").equals(countryCode));
}
}
@@ -12,6 +12,7 @@ import java.text.ParseException;
import java.util.Arrays;
import java.util.UUID;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
@@ -223,44 +224,44 @@ public class EventResourceTest extends AbstractJaxRsApiTest {
return array.stream().filter(o -> ((JSONObject) o).get(attributeName).equals(value)).findFirst().isPresent();
}
private JSONObject getRegatta(String eventName) {
private JSONObject getRegatta(String eventName) throws WebApplicationException, IOException {
Response regattasResponse = regattasResource.getRegatta(eventName, null);
return toJSONObject((String) regattasResponse.getEntity());
return toJSONObject(getEntityAsString(regattasResponse.getEntity()));
}
private boolean isValidCreateEventResponse(Response response) {
private boolean isValidCreateEventResponse(Response response) throws WebApplicationException, IOException {
String id = getIdFromCreateEventResponse(response);
return validateUUID(id);
}
private JSONArray getCourseAreasOfEvent(String strEventId) {
private JSONArray getCourseAreasOfEvent(String strEventId) throws WebApplicationException, IOException {
JSONObject objEvent = getEvent(strEventId);
JSONArray arrCourseAreas = getCourseAreas(objEvent);
return arrCourseAreas;
}
private JSONObject getEvent(String strEventId) {
private JSONObject getEvent(String strEventId) throws WebApplicationException, IOException {
String jsonEvent = getEventAsString(strEventId);
JSONObject objEvent = toJSONObject(jsonEvent);
return objEvent;
}
private boolean leaderBoardWithNameExists(String name) {
private boolean leaderBoardWithNameExists(String name) throws WebApplicationException, IOException {
Response leaderboardResponse = getLeaderboard(name);
JSONObject objLeaderboard = getLeaderboardAsJsonObject(leaderboardResponse);
String strLeaderboardName = (String) objLeaderboard.get("name");
return strLeaderboardName.equals(name);
}
private JSONObject getLeaderboardAsJsonObject(Response leaderboardResponse) {
String strLeaderboardGroup = (String) leaderboardResponse.getEntity();
private JSONObject getLeaderboardAsJsonObject(Response leaderboardResponse) throws WebApplicationException, IOException {
String strLeaderboardGroup = getEntityAsString(leaderboardResponse.getEntity());
JSONObject objLeaderboardGroup = toJSONObject(strLeaderboardGroup);
return objLeaderboardGroup;
}
private JSONObject getLeaderboardGroup(String strDefaultLeaderboardGroupName) {
private JSONObject getLeaderboardGroup(String strDefaultLeaderboardGroupName) throws WebApplicationException, IOException {
Response leaderboardGroupsResponse = leaderboardGroupsResource.getLeaderboardGroup(strDefaultLeaderboardGroupName);
return toJSONObject(leaderboardGroupsResponse.getEntity().toString());
return toJSONObject(getEntityAsString(leaderboardGroupsResponse.getEntity()));
}
private JSONArray getLeaderboardGroups(JSONObject objEvent) {
@@ -287,12 +288,12 @@ public class EventResourceTest extends AbstractJaxRsApiTest {
}
private String getIdFromCreateEventResponse(Response createEventResponse) {
return (String) toJSONObject((String) createEventResponse.getEntity()).get("eventid");
private String getIdFromCreateEventResponse(Response createEventResponse) throws WebApplicationException, IOException {
return (String) toJSONObject(getEntityAsString(createEventResponse.getEntity())).get("eventid");
}
private String getEventAsString(String eventId) {
return (String) eventsResource.getEvent(eventId, null).getEntity();
private String getEventAsString(String eventId) throws WebApplicationException, IOException {
return getEntityAsString(eventsResource.getEvent(eventId, null).getEntity());
}
}
@@ -87,7 +87,7 @@ public class LeaderboardsResourceTest extends AbstractJaxRsApiTest {
Response leaderboardReponse = leaderboardsResource.getLeaderboard(regatta.getName(),
AbstractLeaderboardsResource.ResultStates.Final, null, null,
/* competitorAndBoatIdsOnly */ false);
String jsonString = (String) leaderboardReponse.getEntity();
String jsonString = getEntityAsString(leaderboardReponse.getEntity());
Object obj= JSONValue.parse(jsonString);
JSONObject jsonObject = (JSONObject) obj;
String jsonLeaderboardName = (String) jsonObject.get("name");
@@ -106,7 +106,7 @@ public class LeaderboardsResourceTest extends AbstractJaxRsApiTest {
Response leaderboardReponse = leaderboardsResource.getLeaderboard(regatta.getName(),
AbstractLeaderboardsResource.ResultStates.Final, null, null,
/* competitorAndBoatIdsOnly */ false);
String jsonString = (String) leaderboardReponse.getEntity();
String jsonString = getEntityAsString(leaderboardReponse.getEntity());
Object obj= JSONValue.parse(jsonString);
JSONObject jsonObject = (JSONObject) obj;
String jsonLeaderboardName = (String) jsonObject.get("name");
@@ -119,7 +119,7 @@ public class LeaderboardsResourceTest extends AbstractJaxRsApiTest {
Response leaderboardReponse2 = leaderboardsResource.getLeaderboard(regatta.getName(),
AbstractLeaderboardsResource.ResultStates.Final, null, null,
/* competitorAndBoatIdsOnly */ false);
String jsonString2 = (String) leaderboardReponse2.getEntity();
String jsonString2 = getEntityAsString(leaderboardReponse2.getEntity());
obj= JSONValue.parse(jsonString2);
jsonObject = (JSONObject) obj;
resultTimePoint = parseTimepointFromJsonNumber((Long) jsonObject.get("resultTimepoint"));
@@ -131,7 +131,7 @@ public class LeaderboardsResourceTest extends AbstractJaxRsApiTest {
Response leaderboardReponse = leaderboardsResource.getLeaderboard(regatta.getName(),
AbstractLeaderboardsResource.ResultStates.Live, null, null,
/* competitorAndBoatIdsOnly */ false);
String jsonString = (String) leaderboardReponse.getEntity();
String jsonString = getEntityAsString(leaderboardReponse.getEntity());
Object obj= JSONValue.parse(jsonString);
JSONObject jsonObject = (JSONObject) obj;
String jsonLeaderboardName = (String) jsonObject.get("name");
@@ -153,7 +153,7 @@ public class LeaderboardsResourceTest extends AbstractJaxRsApiTest {
Response leaderboardReponse = leaderboardsResource.getLeaderboard(regatta.getName(),
AbstractLeaderboardsResource.ResultStates.Final, null, null,
/* competitorAndBoatIdsOnly */ false);
String jsonString = (String) leaderboardReponse.getEntity();
String jsonString = getEntityAsString(leaderboardReponse.getEntity());
Object obj= JSONValue.parse(jsonString);
JSONObject jsonObject = (JSONObject) obj;
String jsonLeaderboardName = (String) jsonObject.get("name");
@@ -167,7 +167,7 @@ public class LeaderboardsResourceTest extends AbstractJaxRsApiTest {
Response leaderboardReponse2 = leaderboardsResource.getLeaderboard(regatta.getName(),
AbstractLeaderboardsResource.ResultStates.Final, null, null,
/* competitorAndBoatIdsOnly */ false);
String jsonString2 = (String) leaderboardReponse2.getEntity();
String jsonString2 = getEntityAsString(leaderboardReponse2.getEntity());
Object obj2= JSONValue.parse(jsonString2);
JSONObject jsonObject2 = (JSONObject) obj2;
String jsonLeaderboardName2 = (String) jsonObject2.get("name");
@@ -185,7 +185,7 @@ public class LeaderboardsResourceTest extends AbstractJaxRsApiTest {
Response leaderboardResponse = leaderboardsResource.getLeaderboard(regatta.getName(),
AbstractLeaderboardsResource.ResultStates.Final, null, null,
/* competitorAndBoatIdsOnly */ false);
String jsonString = (String) leaderboardResponse.getEntity();
String jsonString = getEntityAsString(leaderboardResponse.getEntity());
Object obj = JSONValue.parse(jsonString);
JSONObject jsonObject = (JSONObject) obj;
String jsonLeaderboardName = (String) jsonObject.get("name");
@@ -199,7 +199,7 @@ public class LeaderboardsResourceTest extends AbstractJaxRsApiTest {
Response leaderboardReponse2 = leaderboardsResource.getLeaderboard(regatta.getName(),
AbstractLeaderboardsResource.ResultStates.Final, null, null,
/* competitorAndBoatIdsOnly */ false);
jsonString = (String) leaderboardReponse2.getEntity();
jsonString = getEntityAsString(leaderboardReponse2.getEntity());
obj= JSONValue.parse(jsonString);
jsonObject = (JSONObject) obj;
JSONArray jsonCompetitors = (JSONArray) jsonObject.get("competitors");
@@ -209,7 +209,7 @@ public class LeaderboardsResourceTest extends AbstractJaxRsApiTest {
Response leaderboardReponse3 = leaderboardsResource.getLeaderboard(regatta.getName(),
AbstractLeaderboardsResource.ResultStates.Final, maxCompetitorsCount, null,
/* competitorAndBoatIdsOnly */ false);
jsonString = (String) leaderboardReponse3.getEntity();
jsonString = getEntityAsString(leaderboardReponse3.getEntity());
obj= JSONValue.parse(jsonString);
jsonObject = (JSONObject) obj;
jsonCompetitors = (JSONArray) jsonObject.get("competitors");
@@ -219,7 +219,7 @@ public class LeaderboardsResourceTest extends AbstractJaxRsApiTest {
Response leaderboardReponse4 = leaderboardsResource.getLeaderboard(regatta.getName(),
AbstractLeaderboardsResource.ResultStates.Final, null, null,
/* competitorAndBoatIdsOnly */ false);
jsonString = (String) leaderboardReponse4.getEntity();
jsonString = getEntityAsString(leaderboardReponse4.getEntity());
obj= JSONValue.parse(jsonString);
jsonObject = (JSONObject) obj;
jsonCompetitors = (JSONArray) jsonObject.get("competitors");
@@ -234,7 +234,7 @@ public class LeaderboardsResourceTest extends AbstractJaxRsApiTest {
final boolean allowPartialImport = true;
Response leaderboardResponse = importScores(validityTimePoint, comment, allowRaceDefaultsByOrder, allowPartialImport);
assertEquals(Status.OK.getStatusCode(), leaderboardResponse.getStatus());
final JSONObject jsonObject = (JSONObject) JSONValue.parse((String) leaderboardResponse.getEntity());
final JSONObject jsonObject = (JSONObject) JSONValue.parse(getEntityAsString(leaderboardResponse.getEntity()));
assertNotNull(jsonObject);
assertFalse((Boolean) jsonObject.get("complete"));
assertEquals(comment, regattaLeaderboard.getScoreCorrection().getComment());
@@ -252,7 +252,7 @@ public class LeaderboardsResourceTest extends AbstractJaxRsApiTest {
final boolean allowPartialImport = false;
Response leaderboardResponse = importScores(validityTimePoint, comment, allowRaceDefaultsByOrder, allowPartialImport);
assertEquals(Status.CONFLICT.getStatusCode(), leaderboardResponse.getStatus());
final JSONObject jsonObject = (JSONObject) JSONValue.parse((String) leaderboardResponse.getEntity());
final JSONObject jsonObject = (JSONObject) JSONValue.parse(getEntityAsString(leaderboardResponse.getEntity()));
assertNotNull(jsonObject);
assertFalse((Boolean) jsonObject.get("complete"));
assertEquals(null, regattaLeaderboard.getScoreCorrection().getComment());
@@ -124,17 +124,13 @@ public class RegattasResourceTest extends AbstractJaxRsApiTest {
@Test
public void testGetRegattas() throws Exception {
Response regattasResponse = regattasResource.getRegattas();
String jsonString = (String) regattasResponse.getEntity();
String jsonString = getEntityAsString(regattasResponse.getEntity());
Object obj = JSONValue.parse(jsonString);
JSONArray array = (JSONArray) obj;
assertTrue(array.size() == 2);
JSONObject firstElement = (JSONObject) array.get(0);
String jsonName = (String) firstElement.get("name");
String jsonBoatClass = (String) firstElement.get("boatclass");
assertTrue(closedRegattaName.equals(jsonName) || openRegattaName.equals(jsonName));
assertTrue(boatClassName.equals(jsonBoatClass));
}
@@ -149,7 +145,7 @@ public class RegattasResourceTest extends AbstractJaxRsApiTest {
@Test
public void testGetRegatta() throws Exception {
Response regattaResponse = regattasResource.getRegatta(closedRegattaName, null);
String jsonString = (String) regattaResponse.getEntity();
String jsonString = getEntityAsString(regattaResponse.getEntity());
assertNotNull(jsonString);
String readRegattaName = (String) ((JSONObject) JSONValue.parse(jsonString)).get("name");
assertEquals(closedRegattaName, readRegattaName);
@@ -162,31 +158,25 @@ public class RegattasResourceTest extends AbstractJaxRsApiTest {
User user = new UserImpl("admin", "noreply@sapsailing.com", null, new ArrayList<Account>(0), null);
setUser(user);
when(securityService.getCurrentUser()).thenReturn(user);
Response response = regattasResource.createAndAddCompetitor(closedRegattaName, boatClassName, null, "GER",
"#F00", flagImageUri, teamImageUri, null, null, null, competitorName1, competitorShortName1, null,
deviceUuid, null);
assertTrue(response.getStatus() + ": " + response.getEntity().toString(),
assertTrue(response.getStatus() + ": " + getEntityAsString(response.getEntity()),
response.getStatus() == Status.OK.getStatusCode());
assertTrue(regattasResource.getService() == racingEventService);
response = regattasResource.createAndAddCompetitor(closedRegattaName, boatClassName, null, "GER", "#0F0",
flagImageUri, teamImageUri, null, null, null, competitorName2, competitorShortName2, null, deviceUuid,
null);
assertTrue(response.getStatus() + ": " + response.getEntity().toString(),
assertTrue(response.getStatus() + ": " + getEntityAsString(response.getEntity()),
response.getStatus() == Status.OK.getStatusCode());
Regatta regatta = racingEventService.getRegattaByName(closedRegattaName);
Iterator<Competitor> cit = regatta.getAllCompetitors().iterator();
Competitor readCompetitor = cit.next();
assertNotNull(readCompetitor);
assertTrue(
competitorName1.equals(readCompetitor.getName()) || competitorName2.equals(readCompetitor.getName()));
assertTrue(competitorName1.equals(readCompetitor.getName()) || competitorName2.equals(readCompetitor.getName()));
readCompetitor = cit.next();
assertNotNull(readCompetitor);
assertTrue(
competitorName1.equals(readCompetitor.getName()) || competitorName2.equals(readCompetitor.getName()));
assertTrue(competitorName1.equals(readCompetitor.getName()) || competitorName2.equals(readCompetitor.getName()));
Iterator<Competitor> citForRemove = regatta.getAllCompetitors().iterator();
citForRemove.forEachRemaining(competitor -> regattasResource.removeCompetitor(closedRegattaName,
competitor.getId().toString(), null));
@@ -201,7 +191,7 @@ public class RegattasResourceTest extends AbstractJaxRsApiTest {
Response response = regattasResource.createAndAddCompetitor(openRegattaName, boatClassName, null, "GER", "#F00",
flagImageUri, teamImageUri, null, null, null, competitorName1, competitorShortName1, null, deviceUuid,
secret);
assertTrue(response.getStatus() + ": " + response.getEntity().toString(),
assertTrue(response.getStatus() + ": " + getEntityAsString(response.getEntity()),
response.getStatus() == Status.OK.getStatusCode());
assertTrue(regattasResource.getService() == racingEventService);
@@ -213,29 +203,25 @@ public class RegattasResourceTest extends AbstractJaxRsApiTest {
public void testCompetitorRegistrationAnonymousOnOpenRegattaWrongSecret() throws Exception {
doReturn(securityService).when(regattasResource).getService(SecurityService.class);
setUser(null);
Response response = regattasResource.createAndAddCompetitor(openRegattaName, boatClassName, null, "GER", "#F00",
flagImageUri, teamImageUri, null, null, null, competitorName1, competitorShortName1, null, deviceUuid,
"WRONGSECRET");
assertTrue(response.getStatus() + ": " + response.getEntity().toString(),
assertTrue(response.getStatus() + ": " + getEntityAsString(response.getEntity()),
response.getStatus() == Status.FORBIDDEN.getStatusCode());
}
@Test
public void testCompetitorRegistrationAuthenticatedOnOpenRegatta() throws Exception {
doReturn(securityService).when(regattasResource).getService(SecurityService.class);
User user = new UserImpl("max", "noreply@sapsailing.com", null, new ArrayList<Account>(0), null);
setUser(user);
Regatta regatta = racingEventService.getRegattaByName(openRegattaName);
Response response = regattasResource.createAndAddCompetitor(openRegattaName, boatClassName, null, "GER", "#F00",
flagImageUri, teamImageUri, null, null, null, competitorName1, competitorShortName1, null, deviceUuid,
secret);
assertTrue(response.getStatus() + ": " + response.getEntity().toString(),
assertTrue(response.getStatus() + ": " + getEntityAsString(response.getEntity()),
response.getStatus() == Status.OK.getStatusCode());
assertTrue(regattasResource.getService() == racingEventService);
regatta = racingEventService.getRegattaByName(openRegattaName);
testResponseOfOpenRegattaCompetitorRegistration(regatta);
}