Skip to content

Commit

Permalink
fix mutateInfoSpot API and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Leitsi committed Jul 5, 2024
1 parent 3f8d07f commit 5867649
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ public class GraphQLNames {
public static final String INPUT_TYPE_INFO_SPOT = OUTPUT_TYPE_INFO_SPOT + INPUT_TYPE_POSTFIX;

public static final String INFO_SPOTS = "infoSpots";
public static final String MUTATE_INFO_SPOT = "mutateInfoSpots";
public static final String MUTATE_INFO_SPOT = "mutateInfoSpot";

public static final String POSTER = "poster";
public static final String PURPOSE = "purpose";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ public void init() {
.description("Hard delete group of stop places by ID")
.dataFetcher(groupOfStopPlacesDeleterFetcher))
.field(newFieldDefinition()
.type(new GraphQLList(infoSpotObjectType))
.type(infoSpotObjectType)
.name(MUTATE_INFO_SPOT)
.description("Create new or update existing InfoSpots")
.argument(GraphQLArgument.newArgument()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package org.rutebanken.tiamat.rest.graphql.fetchers;

import com.google.api.client.util.Preconditions;
import graphql.language.Field;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

import java.util.List;
import java.util.Map;

import org.rutebanken.tiamat.lock.MutateLock;
import org.rutebanken.tiamat.model.DisplayTypeEnumeration;
import org.rutebanken.tiamat.model.InfoSpot;
import org.rutebanken.tiamat.model.PosterPlaceTypeEnumeration;
import org.rutebanken.tiamat.model.PosterSizeEnumeration;
import org.rutebanken.tiamat.repository.InfoSpotRepository;
import org.rutebanken.tiamat.rest.graphql.GraphQLNames;
import org.rutebanken.tiamat.versioning.VersionCreator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -25,6 +28,8 @@
import static org.rutebanken.tiamat.rest.graphql.GraphQLNames.ID;
import static org.rutebanken.tiamat.rest.graphql.GraphQLNames.LABEL;
import static org.rutebanken.tiamat.rest.graphql.GraphQLNames.MAINTENANCE;
import static org.rutebanken.tiamat.rest.graphql.GraphQLNames.MUTATE_INFO_SPOT;
import static org.rutebanken.tiamat.rest.graphql.GraphQLNames.OUTPUT_TYPE_INFO_SPOT;
import static org.rutebanken.tiamat.rest.graphql.GraphQLNames.POSTER_PLACE_SIZE;
import static org.rutebanken.tiamat.rest.graphql.GraphQLNames.POSTER_PLACE_TYPE;
import static org.rutebanken.tiamat.rest.graphql.GraphQLNames.PURPOSE;
Expand All @@ -45,18 +50,40 @@ public class InfoSpotsUpdater implements DataFetcher {
private InfoSpotRepository infoSpotRepository;

@Override
public Object get(DataFetchingEnvironment environment) throws Exception {
public InfoSpot get(DataFetchingEnvironment environment) throws Exception {
return mutateLock.executeInLock(() -> {
final List<Field> fields = environment.getFields();

logger.info("Got fields {}", fields);

return "Moi";
InfoSpot infoSpot = null;
for (Field field : fields) {
if (field.getName().equals(MUTATE_INFO_SPOT)) {
infoSpot = createOrUpdateInfoSpotInLock(environment);
}
}

return infoSpot;
});
}

private InfoSpot createOrUpdateInfoSpot(Map input) {
InfoSpot target = input.containsKey(ID) ? infoSpotRepository.findFirstByNetexIdOrderByVersionDesc((String) input.get(ID)) : new InfoSpot();
private InfoSpot createOrUpdateInfoSpotInLock(DataFetchingEnvironment environment) {
return mutateLock.executeInLock(() -> createOrUpdateInfoSpot(environment));
}

private InfoSpot createOrUpdateInfoSpot(DataFetchingEnvironment environment) {
Map input = environment.getArgument(OUTPUT_TYPE_INFO_SPOT);
if (input == null) {
return null;
}

InfoSpot target;
String netexId = (String) input.get(ID);
if (netexId != null) {
target = findAndVerify(netexId);
} else {
target = new InfoSpot();
}

if (input.containsKey(LABEL)) {
target.setLabel((String) input.get(LABEL));
Expand Down Expand Up @@ -99,6 +126,17 @@ private InfoSpot createOrUpdateInfoSpot(Map input) {

// TODO: Set associated stops

return infoSpotRepository.save(target);
InfoSpot saved = infoSpotRepository.save(target);
return saved;
}

private InfoSpot findAndVerify(String netexId) {
InfoSpot existingInfoSpot = infoSpotRepository.findFirstByNetexIdOrderByVersionDesc(netexId);
verifyInfoSpotNotNull(existingInfoSpot, netexId);
return existingInfoSpot;
}

private void verifyInfoSpotNotNull(InfoSpot existingInfoSpot, String netexId) {
Preconditions.checkArgument(existingInfoSpot != null, "Attempting to update InfoSpot [id = %s], but InfoSpot does not exist.", netexId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,10 @@ public GraphQLObjectType createObjectType(GraphQLInterfaceType stopPlaceInterfac
.build();
}

public GraphQLInputObjectType infoSpotInputType(GraphQLInputObjectType validBetweenInputObjectType) {

public GraphQLInputObjectType createInputObjectType(GraphQLInputObjectType validBetweenInputObjectType) {
return newInputObject()
.name(INFO_SPOTS)
.name(INPUT_TYPE_INFO_SPOT)
.field(newInputObjectField()
.name(ID)
.type(GraphQLString))
Expand Down Expand Up @@ -192,15 +193,6 @@ public GraphQLInputObjectType infoSpotInputType(GraphQLInputObjectType validBetw
.name(ON_STOP_PLACE)
.type(new GraphQLList(GraphQLString)))
.build();
}

public GraphQLInputObjectType createInputObjectType(GraphQLInputObjectType validBetweenInputObjectType) {
return newInputObject()
.name(INPUT_TYPE_INFO_SPOT)
.field(newInputObjectField()
.name(INFO_SPOTS)
.type(new GraphQLList(infoSpotInputType(validBetweenInputObjectType))))
.build();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.in;
import static org.hamcrest.Matchers.notNullValue;

public class GraphQLResourceInfoSpotIntegrationTest extends AbstractGraphQLResourceIntegrationTest {

Expand Down Expand Up @@ -70,6 +71,7 @@ public void listInfoSpots() throws Exception {
"\"variables\":\"\"}";

executeGraphQL(graphQlJsonQuery)
.body("data.infoSpots[0].id", equalTo(infoSpot.getNetexId()))
.body("data.infoSpots[0].label", equalTo(testLabel))
.body("data.infoSpots[0].backlight", equalTo(infoSpot.getBacklight()))
.body("data.infoSpots[0].floor", equalTo(infoSpot.getFloor()))
Expand All @@ -82,9 +84,63 @@ public void listInfoSpots() throws Exception {
.body("data.infoSpots[0].zoneLabel", equalTo(infoSpot.getZoneLabel()));
}

@Test
public void createInfoSpot() throws Exception {
String graphQlJsonQuery = "{" +
"\"query\": \"mutation { " +
" mutateInfoSpot( InfoSpot: {"+
" label: \\\"new label\\\"" +
" backlight: false" +
" floor: \\\"5\\\"" +
" description: \\\"new description\\\"" +
" maintenance: \\\"new maintainer\\\"" +
" posterPlaceSize: %s".formatted(PosterSizeEnumeration.A4.value()) +
" posterPlaceType: %s".formatted(PosterPlaceTypeEnumeration.SOUND_BEACON.value()) +
" purpose: \\\"new purpose\\\"" +
" railInformation: \\\"new rail info\\\"" +
" zoneLabel: \\\"N\\\"" +
// TODO: poster
// TODO: onStopPlace
" }) { " +
" id " +
" label " +
" backlight " +
" floor " +
" description " +
" maintenance " +
" posterPlaceSize " +
" posterPlaceType " +
" purpose " +
" railInformation " +
" zoneLabel " +
" poster { " +
" label " +
" posterSize " +
" posterType " +
" lines " +
" } " +
" onStopPlace " +
" } " +
"}\"," +
"\"variables\":\"\"}";

executeGraphQL(graphQlJsonQuery)
.body("data.mutateInfoSpot.id", notNullValue())
// .body("data.mutateInfoSpot.version", equalTo(1)) // TODO
.body("data.mutateInfoSpot.label", equalTo("new label"))
.body("data.mutateInfoSpot.backlight", equalTo(false))
.body("data.mutateInfoSpot.floor", equalTo("5"))
.body("data.mutateInfoSpot.description", equalTo("new description"))
.body("data.mutateInfoSpot.maintenance", equalTo("new maintainer"))
.body("data.mutateInfoSpot.posterPlaceSize", equalTo(PosterSizeEnumeration.A4.value()))
.body("data.mutateInfoSpot.posterPlaceType", equalTo(PosterPlaceTypeEnumeration.SOUND_BEACON.value()))
.body("data.mutateInfoSpot.purpose", equalTo("new purpose"))
.body("data.mutateInfoSpot.railInformation", equalTo("new rail info"))
.body("data.mutateInfoSpot.zoneLabel", equalTo("N"));
}

@Test
public void saveInfoSpot() throws Exception {
public void updateInfoSpot() throws Exception {
String testLabel = "I9876";

InfoSpot infoSpot = new InfoSpot();
Expand Down Expand Up @@ -116,7 +172,21 @@ public void saveInfoSpot() throws Exception {

String graphQlJsonQuery = "{" +
"\"query\": \"mutation { " +
" mutateInfoSpots( InfoSpot: { label: 'keke' }) { " +
" mutateInfoSpot( InfoSpot: {"+
" id: \\\"%s\\\"".formatted(infoSpot.getNetexId()) +
" label: \\\"new label\\\"" +
" backlight: false" +
" floor: \\\"5\\\"" +
" description: \\\"new description\\\"" +
" maintenance: \\\"new maintainer\\\"" +
" posterPlaceSize: %s".formatted(PosterSizeEnumeration.A4.value()) +
" posterPlaceType: %s".formatted(PosterPlaceTypeEnumeration.SOUND_BEACON.value()) +
" purpose: \\\"new purpose\\\"" +
" railInformation: \\\"new rail info\\\"" +
" zoneLabel: \\\"N\\\"" +
// TODO: poster
// TODO: onStopPlace
" }) { " +
" id " +
" label " +
" backlight " +
Expand All @@ -140,15 +210,16 @@ public void saveInfoSpot() throws Exception {
"\"variables\":\"\"}";

executeGraphQL(graphQlJsonQuery)
.body("data.infoSpots[0].label", equalTo(testLabel))
.body("data.infoSpots[0].backlight", equalTo(infoSpot.getBacklight()))
.body("data.infoSpots[0].floor", equalTo(infoSpot.getFloor()))
.body("data.infoSpots[0].description", equalTo(infoSpot.getDescription()))
.body("data.infoSpots[0].maintenance", equalTo(infoSpot.getMaintenance()))
.body("data.infoSpots[0].posterPlaceSize", equalTo(infoSpot.getPosterPlaceSize().value()))
.body("data.infoSpots[0].posterPlaceType", equalTo(infoSpot.getPosterPlaceType().value()))
.body("data.infoSpots[0].purpose", equalTo(infoSpot.getPurpose()))
.body("data.infoSpots[0].railInformation", equalTo(infoSpot.getRailInformation()))
.body("data.infoSpots[0].zoneLabel", equalTo(infoSpot.getZoneLabel()));
.body("data.mutateInfoSpot.id", equalTo(infoSpot.getNetexId()))
.body("data.mutateInfoSpot.label", equalTo("new label"))
.body("data.mutateInfoSpot.backlight", equalTo(false))
.body("data.mutateInfoSpot.floor", equalTo("5"))
.body("data.mutateInfoSpot.description", equalTo("new description"))
.body("data.mutateInfoSpot.maintenance", equalTo("new maintainer"))
.body("data.mutateInfoSpot.posterPlaceSize", equalTo(PosterSizeEnumeration.A4.value()))
.body("data.mutateInfoSpot.posterPlaceType", equalTo(PosterPlaceTypeEnumeration.SOUND_BEACON.value()))
.body("data.mutateInfoSpot.purpose", equalTo("new purpose"))
.body("data.mutateInfoSpot.railInformation", equalTo("new rail info"))
.body("data.mutateInfoSpot.zoneLabel", equalTo("N"));
}
}

0 comments on commit 5867649

Please sign in to comment.