fixed SecurityResourceTest regarding StreamingOutput usage

This commit is contained in:
Axel Uhl
2020-04-28 18:21:31 +02:00
parent 8c8ef001b7
commit e1c745886c
8 changed files with 59 additions and 55 deletions
@@ -4,8 +4,6 @@ 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;
@@ -15,9 +13,7 @@ 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;
@@ -159,21 +155,4 @@ 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;
}
}
@@ -12,6 +12,7 @@ import com.sap.sailing.domain.base.impl.BoatClassImpl;
import com.sap.sailing.domain.base.impl.DynamicBoat;
import com.sap.sailing.server.gateway.deserialization.impl.BoatJsonDeserializer;
import com.sap.sailing.server.gateway.deserialization.impl.Helpers;
import com.sap.sse.rest.StreamingOutputUtil;
public class BoatResourceTest extends AbstractJaxRsApiTest {
private final String id = "af855a56-9726-4a9c-a77e-da955bd289be";
@@ -28,7 +29,7 @@ public class BoatResourceTest extends AbstractJaxRsApiTest {
@Test
public void testGetBoatAsJson() throws Exception {
String jsonString = getEntityAsString(boatsResource.getBoat(id, null, null).getEntity());
String jsonString = StreamingOutputUtil.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);
@@ -14,6 +14,7 @@ import com.sap.sailing.domain.base.impl.NationalityImpl;
import com.sap.sailing.domain.base.impl.PersonImpl;
import com.sap.sailing.domain.base.impl.TeamImpl;
import com.sap.sailing.server.gateway.deserialization.impl.Helpers;
import com.sap.sse.rest.StreamingOutputUtil;
public class CompetitorsResourceTest extends AbstractJaxRsApiTest {
private final String name = "Heiko KRÖGER";
@@ -31,7 +32,7 @@ public class CompetitorsResourceTest extends AbstractJaxRsApiTest {
@Test
public void testGetCompetitorAsJson() throws Exception {
String jsonString = getEntityAsString(competitorsResource.getCompetitor(id, null, null).getEntity());
String jsonString = StreamingOutputUtil.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));
@@ -31,6 +31,7 @@ import com.sap.sailing.domain.common.ScoringSchemeType;
import com.sap.sailing.domain.common.impl.DegreePosition;
import com.sap.sailing.server.gateway.jaxrs.api.AbstractLeaderboardsResource;
import com.sap.sse.InvalidDateException;
import com.sap.sse.rest.StreamingOutputUtil;
public class EventResourceTest extends AbstractJaxRsApiTest {
private String randomName;
@@ -226,7 +227,7 @@ public class EventResourceTest extends AbstractJaxRsApiTest {
private JSONObject getRegatta(String eventName) throws WebApplicationException, IOException {
Response regattasResponse = regattasResource.getRegatta(eventName, null);
return toJSONObject(getEntityAsString(regattasResponse.getEntity()));
return toJSONObject(StreamingOutputUtil.getEntityAsString(regattasResponse.getEntity()));
}
private boolean isValidCreateEventResponse(Response response) throws WebApplicationException, IOException {
@@ -254,14 +255,14 @@ public class EventResourceTest extends AbstractJaxRsApiTest {
}
private JSONObject getLeaderboardAsJsonObject(Response leaderboardResponse) throws WebApplicationException, IOException {
String strLeaderboardGroup = getEntityAsString(leaderboardResponse.getEntity());
String strLeaderboardGroup = StreamingOutputUtil.getEntityAsString(leaderboardResponse.getEntity());
JSONObject objLeaderboardGroup = toJSONObject(strLeaderboardGroup);
return objLeaderboardGroup;
}
private JSONObject getLeaderboardGroup(String strDefaultLeaderboardGroupName) throws WebApplicationException, IOException {
Response leaderboardGroupsResponse = leaderboardGroupsResource.getLeaderboardGroup(strDefaultLeaderboardGroupName);
return toJSONObject(getEntityAsString(leaderboardGroupsResponse.getEntity()));
return toJSONObject(StreamingOutputUtil.getEntityAsString(leaderboardGroupsResponse.getEntity()));
}
private JSONArray getLeaderboardGroups(JSONObject objEvent) {
@@ -289,11 +290,11 @@ public class EventResourceTest extends AbstractJaxRsApiTest {
private String getIdFromCreateEventResponse(Response createEventResponse) throws WebApplicationException, IOException {
return (String) toJSONObject(getEntityAsString(createEventResponse.getEntity())).get("eventid");
return (String) toJSONObject(StreamingOutputUtil.getEntityAsString(createEventResponse.getEntity())).get("eventid");
}
private String getEventAsString(String eventId) throws WebApplicationException, IOException {
return getEntityAsString(eventsResource.getEvent(eventId, null).getEntity());
return StreamingOutputUtil.getEntityAsString(eventsResource.getEvent(eventId, null).getEntity());
}
}
@@ -38,6 +38,7 @@ import com.sap.sailing.domain.tracking.TrackedRace;
import com.sap.sailing.server.gateway.jaxrs.api.AbstractLeaderboardsResource;
import com.sap.sse.common.TimePoint;
import com.sap.sse.common.impl.MillisecondsTimePoint;
import com.sap.sse.rest.StreamingOutputUtil;
public class LeaderboardsResourceTest extends AbstractJaxRsApiTest {
private Regatta regatta;
@@ -87,7 +88,7 @@ public class LeaderboardsResourceTest extends AbstractJaxRsApiTest {
Response leaderboardReponse = leaderboardsResource.getLeaderboard(regatta.getName(),
AbstractLeaderboardsResource.ResultStates.Final, null, null,
/* competitorAndBoatIdsOnly */ false);
String jsonString = getEntityAsString(leaderboardReponse.getEntity());
String jsonString = StreamingOutputUtil.getEntityAsString(leaderboardReponse.getEntity());
Object obj= JSONValue.parse(jsonString);
JSONObject jsonObject = (JSONObject) obj;
String jsonLeaderboardName = (String) jsonObject.get("name");
@@ -106,7 +107,7 @@ public class LeaderboardsResourceTest extends AbstractJaxRsApiTest {
Response leaderboardReponse = leaderboardsResource.getLeaderboard(regatta.getName(),
AbstractLeaderboardsResource.ResultStates.Final, null, null,
/* competitorAndBoatIdsOnly */ false);
String jsonString = getEntityAsString(leaderboardReponse.getEntity());
String jsonString = StreamingOutputUtil.getEntityAsString(leaderboardReponse.getEntity());
Object obj= JSONValue.parse(jsonString);
JSONObject jsonObject = (JSONObject) obj;
String jsonLeaderboardName = (String) jsonObject.get("name");
@@ -119,7 +120,7 @@ public class LeaderboardsResourceTest extends AbstractJaxRsApiTest {
Response leaderboardReponse2 = leaderboardsResource.getLeaderboard(regatta.getName(),
AbstractLeaderboardsResource.ResultStates.Final, null, null,
/* competitorAndBoatIdsOnly */ false);
String jsonString2 = getEntityAsString(leaderboardReponse2.getEntity());
String jsonString2 = StreamingOutputUtil.getEntityAsString(leaderboardReponse2.getEntity());
obj= JSONValue.parse(jsonString2);
jsonObject = (JSONObject) obj;
resultTimePoint = parseTimepointFromJsonNumber((Long) jsonObject.get("resultTimepoint"));
@@ -131,7 +132,7 @@ public class LeaderboardsResourceTest extends AbstractJaxRsApiTest {
Response leaderboardReponse = leaderboardsResource.getLeaderboard(regatta.getName(),
AbstractLeaderboardsResource.ResultStates.Live, null, null,
/* competitorAndBoatIdsOnly */ false);
String jsonString = getEntityAsString(leaderboardReponse.getEntity());
String jsonString = StreamingOutputUtil.getEntityAsString(leaderboardReponse.getEntity());
Object obj= JSONValue.parse(jsonString);
JSONObject jsonObject = (JSONObject) obj;
String jsonLeaderboardName = (String) jsonObject.get("name");
@@ -153,7 +154,7 @@ public class LeaderboardsResourceTest extends AbstractJaxRsApiTest {
Response leaderboardReponse = leaderboardsResource.getLeaderboard(regatta.getName(),
AbstractLeaderboardsResource.ResultStates.Final, null, null,
/* competitorAndBoatIdsOnly */ false);
String jsonString = getEntityAsString(leaderboardReponse.getEntity());
String jsonString = StreamingOutputUtil.getEntityAsString(leaderboardReponse.getEntity());
Object obj= JSONValue.parse(jsonString);
JSONObject jsonObject = (JSONObject) obj;
String jsonLeaderboardName = (String) jsonObject.get("name");
@@ -167,7 +168,7 @@ public class LeaderboardsResourceTest extends AbstractJaxRsApiTest {
Response leaderboardReponse2 = leaderboardsResource.getLeaderboard(regatta.getName(),
AbstractLeaderboardsResource.ResultStates.Final, null, null,
/* competitorAndBoatIdsOnly */ false);
String jsonString2 = getEntityAsString(leaderboardReponse2.getEntity());
String jsonString2 = StreamingOutputUtil.getEntityAsString(leaderboardReponse2.getEntity());
Object obj2= JSONValue.parse(jsonString2);
JSONObject jsonObject2 = (JSONObject) obj2;
String jsonLeaderboardName2 = (String) jsonObject2.get("name");
@@ -185,7 +186,7 @@ public class LeaderboardsResourceTest extends AbstractJaxRsApiTest {
Response leaderboardResponse = leaderboardsResource.getLeaderboard(regatta.getName(),
AbstractLeaderboardsResource.ResultStates.Final, null, null,
/* competitorAndBoatIdsOnly */ false);
String jsonString = getEntityAsString(leaderboardResponse.getEntity());
String jsonString = StreamingOutputUtil.getEntityAsString(leaderboardResponse.getEntity());
Object obj = JSONValue.parse(jsonString);
JSONObject jsonObject = (JSONObject) obj;
String jsonLeaderboardName = (String) jsonObject.get("name");
@@ -199,7 +200,7 @@ public class LeaderboardsResourceTest extends AbstractJaxRsApiTest {
Response leaderboardReponse2 = leaderboardsResource.getLeaderboard(regatta.getName(),
AbstractLeaderboardsResource.ResultStates.Final, null, null,
/* competitorAndBoatIdsOnly */ false);
jsonString = getEntityAsString(leaderboardReponse2.getEntity());
jsonString = StreamingOutputUtil.getEntityAsString(leaderboardReponse2.getEntity());
obj= JSONValue.parse(jsonString);
jsonObject = (JSONObject) obj;
JSONArray jsonCompetitors = (JSONArray) jsonObject.get("competitors");
@@ -209,7 +210,7 @@ public class LeaderboardsResourceTest extends AbstractJaxRsApiTest {
Response leaderboardReponse3 = leaderboardsResource.getLeaderboard(regatta.getName(),
AbstractLeaderboardsResource.ResultStates.Final, maxCompetitorsCount, null,
/* competitorAndBoatIdsOnly */ false);
jsonString = getEntityAsString(leaderboardReponse3.getEntity());
jsonString = StreamingOutputUtil.getEntityAsString(leaderboardReponse3.getEntity());
obj= JSONValue.parse(jsonString);
jsonObject = (JSONObject) obj;
jsonCompetitors = (JSONArray) jsonObject.get("competitors");
@@ -219,7 +220,7 @@ public class LeaderboardsResourceTest extends AbstractJaxRsApiTest {
Response leaderboardReponse4 = leaderboardsResource.getLeaderboard(regatta.getName(),
AbstractLeaderboardsResource.ResultStates.Final, null, null,
/* competitorAndBoatIdsOnly */ false);
jsonString = getEntityAsString(leaderboardReponse4.getEntity());
jsonString = StreamingOutputUtil.getEntityAsString(leaderboardReponse4.getEntity());
obj= JSONValue.parse(jsonString);
jsonObject = (JSONObject) obj;
jsonCompetitors = (JSONArray) jsonObject.get("competitors");
@@ -234,7 +235,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(getEntityAsString(leaderboardResponse.getEntity()));
final JSONObject jsonObject = (JSONObject) JSONValue.parse(StreamingOutputUtil.getEntityAsString(leaderboardResponse.getEntity()));
assertNotNull(jsonObject);
assertFalse((Boolean) jsonObject.get("complete"));
assertEquals(comment, regattaLeaderboard.getScoreCorrection().getComment());
@@ -252,7 +253,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(getEntityAsString(leaderboardResponse.getEntity()));
final JSONObject jsonObject = (JSONObject) JSONValue.parse(StreamingOutputUtil.getEntityAsString(leaderboardResponse.getEntity()));
assertNotNull(jsonObject);
assertFalse((Boolean) jsonObject.get("complete"));
assertEquals(null, regattaLeaderboard.getScoreCorrection().getComment());
@@ -48,6 +48,7 @@ import com.sap.sailing.domain.ranking.OneDesignRankingMetric;
import com.sap.sse.common.Color;
import com.sap.sse.common.TimePoint;
import com.sap.sse.common.impl.MillisecondsTimePoint;
import com.sap.sse.rest.StreamingOutputUtil;
import com.sap.sse.security.SecurityService;
import com.sap.sse.security.interfaces.UserImpl;
import com.sap.sse.security.shared.Account;
@@ -124,7 +125,7 @@ public class RegattasResourceTest extends AbstractJaxRsApiTest {
@Test
public void testGetRegattas() throws Exception {
Response regattasResponse = regattasResource.getRegattas();
String jsonString = getEntityAsString(regattasResponse.getEntity());
String jsonString = StreamingOutputUtil.getEntityAsString(regattasResponse.getEntity());
Object obj = JSONValue.parse(jsonString);
JSONArray array = (JSONArray) obj;
assertTrue(array.size() == 2);
@@ -145,7 +146,7 @@ public class RegattasResourceTest extends AbstractJaxRsApiTest {
@Test
public void testGetRegatta() throws Exception {
Response regattaResponse = regattasResource.getRegatta(closedRegattaName, null);
String jsonString = getEntityAsString(regattaResponse.getEntity());
String jsonString = StreamingOutputUtil.getEntityAsString(regattaResponse.getEntity());
assertNotNull(jsonString);
String readRegattaName = (String) ((JSONObject) JSONValue.parse(jsonString)).get("name");
assertEquals(closedRegattaName, readRegattaName);
@@ -161,13 +162,13 @@ public class RegattasResourceTest extends AbstractJaxRsApiTest {
Response response = regattasResource.createAndAddCompetitor(closedRegattaName, boatClassName, null, "GER",
"#F00", flagImageUri, teamImageUri, null, null, null, competitorName1, competitorShortName1, null,
deviceUuid, null);
assertTrue(response.getStatus() + ": " + getEntityAsString(response.getEntity()),
assertTrue(response.getStatus() + ": " + StreamingOutputUtil.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() + ": " + getEntityAsString(response.getEntity()),
assertTrue(response.getStatus() + ": " + StreamingOutputUtil.getEntityAsString(response.getEntity()),
response.getStatus() == Status.OK.getStatusCode());
Regatta regatta = racingEventService.getRegattaByName(closedRegattaName);
Iterator<Competitor> cit = regatta.getAllCompetitors().iterator();
@@ -191,7 +192,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() + ": " + getEntityAsString(response.getEntity()),
assertTrue(response.getStatus() + ": " + StreamingOutputUtil.getEntityAsString(response.getEntity()),
response.getStatus() == Status.OK.getStatusCode());
assertTrue(regattasResource.getService() == racingEventService);
@@ -206,7 +207,7 @@ public class RegattasResourceTest extends AbstractJaxRsApiTest {
Response response = regattasResource.createAndAddCompetitor(openRegattaName, boatClassName, null, "GER", "#F00",
flagImageUri, teamImageUri, null, null, null, competitorName1, competitorShortName1, null, deviceUuid,
"WRONGSECRET");
assertTrue(response.getStatus() + ": " + getEntityAsString(response.getEntity()),
assertTrue(response.getStatus() + ": " + StreamingOutputUtil.getEntityAsString(response.getEntity()),
response.getStatus() == Status.FORBIDDEN.getStatusCode());
}
@@ -219,7 +220,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() + ": " + getEntityAsString(response.getEntity()),
assertTrue(response.getStatus() + ": " + StreamingOutputUtil.getEntityAsString(response.getEntity()),
response.getStatus() == Status.OK.getStatusCode());
assertTrue(regattasResource.getService() == racingEventService);
regatta = racingEventService.getRegattaByName(openRegattaName);
@@ -7,6 +7,7 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.util.Locale;
import javax.ws.rs.core.Response;
@@ -24,6 +25,7 @@ import org.junit.Test;
import com.sap.sailing.domain.common.media.MediaTrack;
import com.sap.sse.common.mail.MailException;
import com.sap.sse.rest.StreamingOutputUtil;
import com.sap.sse.security.BearerAuthenticationToken;
import com.sap.sse.security.SecurityService;
import com.sap.sse.security.impl.Activator;
@@ -234,8 +236,8 @@ public class SecurityResourceTest {
}
}
private String getOrCreateAccessToken() throws ParseException {
String responseJsonString = (String) servlet.respondWithAccessTokenForUser(USERNAME).getEntity();
private String getOrCreateAccessToken() throws ParseException, IOException {
String responseJsonString = StreamingOutputUtil.getEntityAsString(servlet.respondWithAccessTokenForUser(USERNAME).getEntity());
JSONObject responseJson = (JSONObject) new JSONParser().parse(responseJsonString);
assertEquals(USERNAME, responseJson.get("username"));
String accessToken = (String) responseJson.get("access_token");
@@ -243,9 +245,9 @@ public class SecurityResourceTest {
return accessToken;
}
private String createAccessToken() throws ParseException {
private String createAccessToken() throws ParseException, IOException {
assertEquals(Response.Status.OK.getStatusCode(), servlet.respondToRemoveAccessTokenForUser(USERNAME).getStatus());
String responseJsonString = (String) servlet.respondWithAccessTokenForUser(USERNAME).getEntity();
String responseJsonString = StreamingOutputUtil.getEntityAsString(servlet.respondWithAccessTokenForUser(USERNAME).getEntity());
JSONObject responseJson = (JSONObject) new JSONParser().parse(responseJsonString);
assertEquals(USERNAME, responseJson.get("username"));
String accessToken = (String) responseJson.get("access_token");
@@ -258,7 +260,7 @@ public class SecurityResourceTest {
}
@Test
public void createAccessTokenAndAuthenticate() throws ParseException, UserManagementException {
public void createAccessTokenAndAuthenticate() throws ParseException, UserManagementException, IOException {
String accessToken = getOrCreateAccessToken();
User user = service.getUserByAccessToken(accessToken);
assertNotNull(user);
@@ -280,7 +282,7 @@ public class SecurityResourceTest {
}
@Test
public void ensureOldBearerTokenIsInvalidatedByObtainingNewOne() throws ParseException {
public void ensureOldBearerTokenIsInvalidatedByObtainingNewOne() throws ParseException, IOException {
String accessToken = getOrCreateAccessToken();
createAccessToken();
User user = service.getUserByAccessToken(accessToken);
@@ -288,7 +290,7 @@ public class SecurityResourceTest {
}
@Test
public void ensureOldBearerTokenIsInvalidatedByRequestingItsRemoval() throws ParseException {
public void ensureOldBearerTokenIsInvalidatedByRequestingItsRemoval() throws ParseException, IOException {
String accessToken = getOrCreateAccessToken();
removeAccessToken();
User user = service.getUserByAccessToken(accessToken);
@@ -1,6 +1,7 @@
package com.sap.sse.rest;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
@@ -10,7 +11,7 @@ import javax.ws.rs.core.StreamingOutput;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public abstract class StreamingOutputUtil {
public class StreamingOutputUtil {
protected StreamingOutput streamingOutput(JSONObject jsonObject) {
return new StreamingOutput() {
@Override
@@ -32,4 +33,21 @@ public abstract class StreamingOutputUtil {
}
};
}
/**
* 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.
*/
public static String getEntityAsString(Object entity) throws 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;
}
}