bug5168: fixed some test issued

This commit is contained in:
Axel Uhl
2020-01-07 23:08:11 +01:00
parent 9169b00ca7
commit 7eb6802b8f
5 changed files with 75 additions and 44 deletions
@@ -56,7 +56,6 @@ public class MarkPropertiesApi {
}
formParams.put(PARAM_FIXED_POSITION_LATDEG, latDeg != null ? latDeg.toString() : null);
formParams.put(PARAM_FIXED_POSITION_LONDEG, latDeg != null ? lonDeg.toString() : null);
JSONObject result = ctx.put(MARK_PROPERTIES + "/" + id.toString() + POSITIONING, null, formParams);
return new MarkProperties(result);
}
@@ -80,7 +79,6 @@ public class MarkPropertiesApi {
for (String tag : tags) {
formParams.put(PARAM_TAG, tag);
}
JSONObject result = ctx.put(MARK_PROPERTIES + "/" + id.toString(), null, formParams);
return new MarkProperties(result);
}
@@ -91,8 +89,7 @@ public class MarkPropertiesApi {
}
public Iterable<MarkProperties> getAllMarkProperties(final ApiContext ctx, final Iterable<String> tags) {
// FIXME: multiple query parameters with the same key sould be passed but cannot be put into Map<String,
// String>. Should use Map<String, Iterator<String>>. Will be fixed in bug4942.
// FIXME: multiple query parameters with the same key sould be passed but cannot be put into Map<String, String>. Should use Map<String, Iterator<String>>. Will be fixed in bug4942.
final Map<String, String> queryParams = new TreeMap<>();
for (String tag : tags) {
queryParams.put(PARAM_TAG, tag);
@@ -93,7 +93,7 @@ public class CourseConfigurationJsonSerializer implements JsonSerializer<CourseC
markConfigurationsEntry.put(FIELD_MARK_CONFIGURATION_ASSOCIATED_ROLE_ID, ((MarkRole)associatedRole).getId().toString());
}
}
// TODO introduce MarkConfiguration visitor pattern?
// TODO use MarkConfiguration visitor pattern!
if (markConfiguration instanceof FreestyleMarkConfiguration) {
final FreestyleMarkConfiguration<MarkConfigurationResponseAnnotation> freeStyleMarkConfiguration =
(FreestyleMarkConfiguration<MarkConfigurationResponseAnnotation>) markConfiguration;
@@ -87,18 +87,14 @@ import com.sun.jersey.api.client.ClientResponse.Status;
public class CourseConfigurationResource extends AbstractSailingServerResource {
private static final Logger log = Logger.getLogger(CourseConfigurationResource.class.getName());
private final JsonSerializer<CourseConfiguration<MarkConfigurationResponseAnnotation>> courseConfigurationJsonSerializer;
private JsonSerializer<CourseConfiguration<MarkConfigurationResponseAnnotation>> courseConfigurationJsonSerializer;
private final BiFunction<Regatta, DeviceIdentifier, Position> positionResolver;
private final DeviceIdentifierJsonDeserializer deviceIdentifierDeserializer;
private DeviceIdentifierJsonDeserializer deviceIdentifierDeserializer;
private TypeBasedServiceFinder<DeviceIdentifierJsonHandler> deviceJsonServiceFinder;
public static final String FIELD_TAGS = "tags";
public CourseConfigurationResource() {
final TypeBasedServiceFinder<DeviceIdentifierJsonHandler> deviceJsonServiceFinder = getServiceFinderFactory()
.createServiceFinder(DeviceIdentifierJsonHandler.class);
deviceJsonServiceFinder.setFallbackService(new PlaceHolderDeviceIdentifierJsonHandler());
courseConfigurationJsonSerializer = new CourseConfigurationJsonSerializer(new DeviceIdentifierJsonSerializer(deviceJsonServiceFinder));
deviceIdentifierDeserializer = new DeviceIdentifierJsonDeserializer(deviceJsonServiceFinder);
positionResolver = (regatta, deviceIdentifier) -> {
Position lastPosition = null;
try {
@@ -116,7 +112,7 @@ public class CourseConfigurationResource extends AbstractSailingServerResource {
}
private JsonDeserializer<CourseConfiguration<MarkConfigurationRequestAnnotation>> getCourseConfigurationDeserializer(final Regatta regatta) {
return new CourseConfigurationJsonDeserializer(this.getSharedSailingData(), deviceIdentifierDeserializer, regatta);
return new CourseConfigurationJsonDeserializer(this.getSharedSailingData(), getDeviceIdentifierDeserializer(), regatta);
}
private Response getBadRegattaErrorResponse(String regattaName) {
@@ -180,7 +176,7 @@ public class CourseConfigurationResource extends AbstractSailingServerResource {
// An additional call to get the marks defined in the regatta isn't necessary with the described behavior of this API.
final CourseConfiguration<MarkConfigurationResponseAnnotation> courseConfiguration = getService().getCourseAndMarkConfigurationFactory()
.createCourseConfigurationFromRegatta(courseBase, regatta, tags);
final JSONObject jsonResult = courseConfigurationJsonSerializer.serialize(courseConfiguration);
final JSONObject jsonResult = getCourseConfigurationJsonSerializer().serialize(courseConfiguration);
return Response.ok(jsonResult.toJSONString()).build();
}
@@ -204,7 +200,7 @@ public class CourseConfigurationResource extends AbstractSailingServerResource {
}
final CourseConfiguration<MarkConfigurationResponseAnnotation> courseConfiguration = getService().getCourseAndMarkConfigurationFactory()
.createCourseConfigurationFromTemplate(courseTemplate, regatta, tags);
String jsonString = courseConfigurationJsonSerializer.serialize(courseConfiguration).toJSONString();
String jsonString = getCourseConfigurationJsonSerializer().serialize(courseConfiguration).toJSONString();
return Response.ok(jsonString).build();
}
@@ -241,7 +237,7 @@ public class CourseConfigurationResource extends AbstractSailingServerResource {
regatta, getService().getCourseAndMarkConfigurationFactory()
.createCourseTemplateAndUpdatedConfiguration(courseConfiguration, tags,
optionalUserGroupForNonDefaultMarkPropertiesOwnership));
final String jsonString = courseConfigurationJsonSerializer.serialize(courseTemplate).toJSONString();
final String jsonString = getCourseConfigurationJsonSerializer().serialize(courseTemplate).toJSONString();
return Response.ok(jsonString).build();
}
@@ -249,7 +245,7 @@ public class CourseConfigurationResource extends AbstractSailingServerResource {
Regatta regatta, CourseConfiguration<MarkConfigurationRequestAnnotation> courseConfiguration) {
final Map<MarkConfiguration<MarkConfigurationRequestAnnotation>, MarkConfiguration<MarkConfigurationResponseAnnotation>> markConfigMap = new HashMap<>();
for (final MarkConfiguration<MarkConfigurationRequestAnnotation> sourceMark : courseConfiguration.getAllMarks()) {
markConfigMap.put(sourceMark, annotateWithLastKnownPositionInformation(regatta, sourceMark));
annotateWithLastKnownPositionInformation(regatta, sourceMark, markConfigMap);
}
final Map<MarkConfiguration<MarkConfigurationResponseAnnotation>, IsMarkRole> targetAssociatedRoles = new HashMap<>();
for (final Entry<MarkConfiguration<MarkConfigurationRequestAnnotation>, IsMarkRole> e : courseConfiguration.getAssociatedRoles().entrySet()) {
@@ -258,7 +254,7 @@ public class CourseConfigurationResource extends AbstractSailingServerResource {
final List<WaypointWithMarkConfiguration<MarkConfigurationResponseAnnotation>> targetWaypoints = new ArrayList<>();
for (final WaypointWithMarkConfiguration<MarkConfigurationRequestAnnotation> sourceWaypoint : courseConfiguration.getWaypoints()) {
targetWaypoints.add(new WaypointWithMarkConfigurationImpl<MarkConfigurationResponseAnnotation>(
annotateWithLastKnownPositionInformation(sourceWaypoint.getControlPoint(), regatta), sourceWaypoint.getPassingInstruction()));
annotateWithLastKnownPositionInformation(sourceWaypoint.getControlPoint(), regatta, markConfigMap), sourceWaypoint.getPassingInstruction()));
}
return new CourseConfigurationImpl<MarkConfigurationResponseAnnotation>(
courseConfiguration.getOptionalCourseTemplate(),
@@ -268,25 +264,34 @@ public class CourseConfigurationResource extends AbstractSailingServerResource {
}
private ControlPointWithMarkConfiguration<MarkConfigurationResponseAnnotation> annotateWithLastKnownPositionInformation(
ControlPointWithMarkConfiguration<MarkConfigurationRequestAnnotation> controlPoint, Regatta regatta) {
ControlPointWithMarkConfiguration<MarkConfigurationRequestAnnotation> controlPoint, Regatta regatta,
Map<MarkConfiguration<MarkConfigurationRequestAnnotation>, MarkConfiguration<MarkConfigurationResponseAnnotation>> markConfigMap) {
final ControlPointWithMarkConfiguration<MarkConfigurationResponseAnnotation> result;
if (controlPoint instanceof ControlPointWithMarkConfiguration) {
result = annotateWithLastKnownPositionInformation(regatta, (MarkConfiguration<MarkConfigurationRequestAnnotation>) controlPoint);
} else if (controlPoint instanceof MarkPairWithConfiguration){
if (controlPoint instanceof MarkPairWithConfiguration) {
final MarkPairWithConfiguration<MarkConfigurationRequestAnnotation> sourceMarkPair = (MarkPairWithConfiguration<MarkConfigurationRequestAnnotation>) controlPoint;
result = new MarkPairWithConfigurationImpl<MarkConfigurationResponseAnnotation>(sourceMarkPair.getName(),
annotateWithLastKnownPositionInformation(regatta, sourceMarkPair.getLeft()),
annotateWithLastKnownPositionInformation(regatta, sourceMarkPair.getRight()),
annotateWithLastKnownPositionInformation(regatta, sourceMarkPair.getLeft(), markConfigMap),
annotateWithLastKnownPositionInformation(regatta, sourceMarkPair.getRight(), markConfigMap),
sourceMarkPair.getShortName());
} else if (controlPoint instanceof MarkConfiguration) {
result = annotateWithLastKnownPositionInformation(regatta,
((MarkConfiguration<MarkConfigurationRequestAnnotation>) controlPoint), markConfigMap);
} else {
throw new IllegalStateException("Unknown ControlPointWithMarkConfiguration subclass: "+controlPoint.getClass().getName());
}
return result;
}
/**
* @param markConfigMap
* used for look-up of {@code sourceMark}; if found, the value is returned; otherwise, a new value is
* computed, entered into {@code markConfigMap} and the value is returned.
*/
private MarkConfiguration<MarkConfigurationResponseAnnotation> annotateWithLastKnownPositionInformation(
Regatta regatta, MarkConfiguration<MarkConfigurationRequestAnnotation> sourceMark) {
return sourceMark.accept(new MarkConfigurationVisitor<MarkConfiguration<MarkConfigurationResponseAnnotation>, MarkConfigurationRequestAnnotation>() {
Regatta regatta, MarkConfiguration<MarkConfigurationRequestAnnotation> sourceMark,
Map<MarkConfiguration<MarkConfigurationRequestAnnotation>, MarkConfiguration<MarkConfigurationResponseAnnotation>> markConfigMap) {
return markConfigMap.computeIfAbsent(sourceMark,
sm->sm.accept(new MarkConfigurationVisitor<MarkConfiguration<MarkConfigurationResponseAnnotation>, MarkConfigurationRequestAnnotation>() {
@Override
public MarkConfiguration<MarkConfigurationResponseAnnotation> visit(
FreestyleMarkConfiguration<MarkConfigurationRequestAnnotation> markConfiguration) {
@@ -321,7 +326,7 @@ public class CourseConfigurationResource extends AbstractSailingServerResource {
markConfiguration.getMark(), positionResolver, regatta),
markConfiguration.getOptionalMarkTemplate(), markConfiguration.getOptionalMarkProperties());
}
});
}));
}
private MarkConfigurationResponseAnnotation getLastKnownPositionInformation(MarkConfigurationRequestAnnotation positioningAnnotation,
@@ -395,7 +400,29 @@ public class CourseConfigurationResource extends AbstractSailingServerResource {
raceLog.getCurrentPassId(), course, CourseDesignerMode.BY_MARKS));
final CourseConfiguration<MarkConfigurationResponseAnnotation> courseConfigurationResult = getService().getCourseAndMarkConfigurationFactory()
.createCourseConfigurationFromRegatta(course, regatta, /* tagsToFilterMarkProperties */ null);
final String jsonString = courseConfigurationJsonSerializer.serialize(courseConfigurationResult).toJSONString();
final String jsonString = getCourseConfigurationJsonSerializer().serialize(courseConfigurationResult).toJSONString();
return Response.ok(jsonString).build();
}
private synchronized JsonSerializer<CourseConfiguration<MarkConfigurationResponseAnnotation>> getCourseConfigurationJsonSerializer() {
if (courseConfigurationJsonSerializer == null) {
courseConfigurationJsonSerializer = new CourseConfigurationJsonSerializer(new DeviceIdentifierJsonSerializer(getDeviceJsonServiceFinder()));
}
return courseConfigurationJsonSerializer;
}
private DeviceIdentifierJsonDeserializer getDeviceIdentifierDeserializer() {
if (deviceIdentifierDeserializer == null) {
deviceIdentifierDeserializer = new DeviceIdentifierJsonDeserializer(getDeviceJsonServiceFinder());
}
return deviceIdentifierDeserializer;
}
private synchronized TypeBasedServiceFinder<DeviceIdentifierJsonHandler> getDeviceJsonServiceFinder() {
if (deviceJsonServiceFinder == null) {
deviceJsonServiceFinder = getServiceFinderFactory().createServiceFinder(DeviceIdentifierJsonHandler.class);
deviceJsonServiceFinder.setFallbackService(new PlaceHolderDeviceIdentifierJsonHandler());
}
return deviceJsonServiceFinder;
}
}
@@ -113,13 +113,14 @@ public class MarkResource extends AbstractSailingServerResource {
final String markShape = (String) requestObject.get(MARK_SHAPE);
final String markTypeStr = (String) requestObject.get(MARK_TYPE);
MarkType markType = null;
try {
markType = MarkType.valueOf(markTypeStr);
} catch (IllegalArgumentException | NullPointerException e) {
// ignore since mark type value is optional
LOG.warning("Invalid mark type '" + markTypeStr + "' received via REST endpoint was ignored.");
if (markTypeStr != null) {
try {
markType = MarkType.valueOf(markTypeStr);
} catch (IllegalArgumentException | NullPointerException e) {
// ignore since mark type value is optional
LOG.warning("Invalid mark type '" + markTypeStr + "' received via REST endpoint was ignored.");
}
}
Color markColor = null;
if (markColorStr != null && !markColorStr.isEmpty()) {
try {
@@ -43,13 +43,9 @@ import com.sun.jersey.api.client.ClientResponse.Status;
@Path("/v1/markproperties")
public class MarkPropertiesResource extends AbstractSailingServerResource {
private final JsonSerializer<MarkProperties> markPropertiesSerializer;
private JsonSerializer<MarkProperties> markPropertiesSerializer;
public MarkPropertiesResource() {
final TypeBasedServiceFinder<DeviceIdentifierJsonHandler> deviceJsonServiceFinder = getServiceFinderFactory()
.createServiceFinder(DeviceIdentifierJsonHandler.class);
deviceJsonServiceFinder.setFallbackService(new PlaceHolderDeviceIdentifierJsonHandler());
markPropertiesSerializer = new MarkPropertiesJsonSerializer(new DeviceIdentifierJsonSerializer(deviceJsonServiceFinder));
}
private Response getBadMarkPropertiesValidationErrorResponse(String errorText) {
@@ -67,7 +63,7 @@ public class MarkPropertiesResource extends AbstractSailingServerResource {
Iterable<MarkProperties> markPropertiesList = getSharedSailingData().getAllMarkProperties(tags);
JSONArray result = new JSONArray();
for (MarkProperties markProperties : markPropertiesList) {
result.add(markPropertiesSerializer.serialize(markProperties));
result.add(getMarkPropertiesSerializer().serialize(markProperties));
}
final String json = result.toJSONString();
return Response.ok(json).build();
@@ -81,7 +77,7 @@ public class MarkPropertiesResource extends AbstractSailingServerResource {
if (markProperties == null) {
return getMarkPropertiesNotFoundErrorResponse();
}
final JSONObject serializedMarkedProperties = markPropertiesSerializer.serialize(markProperties);
final JSONObject serializedMarkedProperties = getMarkPropertiesSerializer().serialize(markProperties);
final String json = serializedMarkedProperties.toJSONString();
return Response.ok(json).build();
}
@@ -133,7 +129,7 @@ public class MarkPropertiesResource extends AbstractSailingServerResource {
final Position fixedPosition = new DegreePosition(latDeg, lonDeg);
getSharedSailingData().setFixedPositionForMarkProperties(createdMarkProperties, fixedPosition);
}
final JSONObject serializedMarkedProperties = markPropertiesSerializer.serialize(createdMarkProperties);
final JSONObject serializedMarkedProperties = getMarkPropertiesSerializer().serialize(createdMarkProperties);
final String json = serializedMarkedProperties.toJSONString();
return Response.ok(json).build();
}
@@ -156,7 +152,7 @@ public class MarkPropertiesResource extends AbstractSailingServerResource {
final Position fixedPosition = new DegreePosition(latDeg, lonDeg);
getSharedSailingData().setFixedPositionForMarkProperties(markProperties, fixedPosition);
}
final JSONObject serializedMarkedProperties = markPropertiesSerializer.serialize(markProperties);
final JSONObject serializedMarkedProperties = getMarkPropertiesSerializer().serialize(markProperties);
final String json = serializedMarkedProperties.toJSONString();
return Response.ok(json).build();
}
@@ -211,7 +207,7 @@ public class MarkPropertiesResource extends AbstractSailingServerResource {
positioningInformation = null;
}
getSharedSailingData().updateMarkProperties(markPropertiesUUID, markPropertiesBuilder.build(), positioningInformation, tags);
final JSONObject serializedMarkedProperties = markPropertiesSerializer.serialize(markProperties);
final JSONObject serializedMarkedProperties = getMarkPropertiesSerializer().serialize(markProperties);
final String json = serializedMarkedProperties.toJSONString();
return Response.ok(json).build();
}
@@ -226,4 +222,14 @@ public class MarkPropertiesResource extends AbstractSailingServerResource {
getSharedSailingData().deleteMarkProperties(markProperties);
return Response.ok().build();
}
private synchronized JsonSerializer<MarkProperties> getMarkPropertiesSerializer() {
if (markPropertiesSerializer == null) {
final TypeBasedServiceFinder<DeviceIdentifierJsonHandler> deviceJsonServiceFinder = getServiceFinderFactory()
.createServiceFinder(DeviceIdentifierJsonHandler.class);
deviceJsonServiceFinder.setFallbackService(new PlaceHolderDeviceIdentifierJsonHandler());
markPropertiesSerializer = new MarkPropertiesJsonSerializer(new DeviceIdentifierJsonSerializer(deviceJsonServiceFinder));
}
return markPropertiesSerializer;
}
}