Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x1129 Mutation::renameEquipment #320

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/main/java/uk/ac/sanger/sccp/stan/GraphQLMutation.java
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,17 @@ public DataFetcher<Equipment> setEquipmentEnabled() {
};
}

public DataFetcher<Equipment> renameEquipment() {
return dfe -> {
User user = checkUser(dfe, User.Role.admin);
Integer equipmentId = dfe.getArgument("equipmentId");
requireNonNull(equipmentId, "equipmentId not specified");
String name = dfe.getArgument("name");
logRequest("RenameEquipment", user, String.format("(equipmentId=%s, name=%s)", equipmentId, repr(name)));
return equipmentAdminService.renameEquipment(equipmentId, name);
};
}

public DataFetcher<DestructionReason> addDestructionReason() {
return adminAdd(destructionReasonAdminService::addNew, "AddDestructionReason", "text");
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/uk/ac/sanger/sccp/stan/GraphQLProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ private RuntimeWiring buildWiring() {
.dataFetcher("setCommentEnabled", transact(graphQLMutation.setCommentEnabled()))
.dataFetcher("addEquipment", transact(graphQLMutation.addEquipment()))
.dataFetcher("setEquipmentEnabled", transact(graphQLMutation.setEquipmentEnabled()))
.dataFetcher("renameEquipment", transact(graphQLMutation.renameEquipment()))
.dataFetcher("addHmdmc", transact(graphQLMutation.addHmdmc()))
.dataFetcher("setHmdmcEnabled", transact(graphQLMutation.setHmdmcEnabled()))
.dataFetcher("addDestructionReason", transact(graphQLMutation.addDestructionReason()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ public Equipment addEquipment(String category, String name) {
return equipmentRepo.save(new Equipment(name, category.toLowerCase()));
}

public Equipment renameEquipment(int equipmentId, String newName) {
newName = trimAndRequire(newName, "Name not supplied.");
equipmentNameValidator.checkArgument(newName);
Equipment eq = equipmentRepo.getById(equipmentId);
if (newName.equals(eq.getName())) {
return eq;
}
eq.setName(newName);
return equipmentRepo.save(eq);
}

public Equipment setEquipmentEnabled(int equipmentId, boolean enabled) {
Equipment eq = equipmentRepo.getById(equipmentId);
if (eq.isEnabled() != enabled) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -1913,6 +1913,8 @@ type Mutation {
setCommentEnabled(commentId: Int!, enabled: Boolean!): Comment!
"""Create a new piece of equipment that users can record using in future operations."""
addEquipment(category: String!, name: String!): Equipment!
"""Rename existing equipment."""
renameEquipment(equipmentId: Int!, name: String!): Equipment!
"""Enable or disable a piece of equipment."""
setEquipmentEnabled(equipmentId: Int!, enabled: Boolean!): Equipment!
"""Create a new reason that users can record when destroying labware."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.*;
import org.mockito.stubbing.OngoingStubbing;
import uk.ac.sanger.sccp.stan.Matchers;
import uk.ac.sanger.sccp.stan.model.Equipment;
import uk.ac.sanger.sccp.stan.repo.EquipmentRepo;

Expand All @@ -17,6 +16,7 @@
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static uk.ac.sanger.sccp.stan.Matchers.returnArgument;

/**
* Tests {@link EquipmentAdminService}
Expand All @@ -31,8 +31,8 @@ public class TestEquipmentAdminService {
void setup() {
mockEquipmentRepo = mock(EquipmentRepo.class);
service = new EquipmentAdminService(mockEquipmentRepo,
new StringValidator("Equipment name", 2, 8, StringValidator.CharacterType.ALPHA),
new StringValidator("Equipment category", 2, 8, StringValidator.CharacterType.ALPHA));
new StringValidator("Equipment category", 2, 8, StringValidator.CharacterType.ALPHA),
new StringValidator("Equipment name", 2, 8, StringValidator.CharacterType.ALPHA));
}

@ParameterizedTest
Expand Down Expand Up @@ -63,14 +63,14 @@ public void testGetEquipment(String category, boolean includeDisabled) {
@CsvSource(value={
",Bananas,Category not supplied.",
"scanner,,Name not supplied.",
"sc@nner,Bananas,Equipment name \"sc@nner\" contains invalid characters \"@\".",
"scanner,B@nanas,Equipment category \"B@nanas\" contains invalid characters \"@\".",
"sc@nner,Bananas,Equipment category \"sc@nner\" contains invalid characters \"@\".",
"scanner,B@nanas,Equipment name \"B@nanas\" contains invalid characters \"@\".",
"scanner,Bananas,",
"' SCANNER ', ' Bananas ',",
})
public void testAddEquipment(String category, String name, String expectedErrorMessage) {
if (expectedErrorMessage==null) {
when(mockEquipmentRepo.save(any())).then(Matchers.returnArgument());
when(mockEquipmentRepo.save(any())).then(returnArgument());
Equipment equipment = service.addEquipment(category, name);
assertNotNull(equipment);
verify(mockEquipmentRepo).save(equipment);
Expand Down Expand Up @@ -99,7 +99,7 @@ public void testAddEquipmentDupe() {
public void testSetEquipmentEnabled(boolean wasEnabled, boolean enabled) {
Equipment equipment = new Equipment(10, "scanner", "Bananas", wasEnabled);
when(mockEquipmentRepo.getById(equipment.getId())).thenReturn(equipment);
when(mockEquipmentRepo.save(any())).then(Matchers.returnArgument());
when(mockEquipmentRepo.save(any())).then(returnArgument());

assertSame(equipment, service.setEquipmentEnabled(equipment.getId(), enabled));

Expand All @@ -110,4 +110,38 @@ public void testSetEquipmentEnabled(boolean wasEnabled, boolean enabled) {
verify(mockEquipmentRepo).save(equipment);
}
}

@ParameterizedTest
@NullSource
@ValueSource(strings={"", " "})
public void testRenameEquipment_noName(String name) {
assertThat(assertThrows(IllegalArgumentException.class, () -> service.renameEquipment(17, name)))
.hasMessage("Name not supplied.");
verifyNoInteractions(mockEquipmentRepo);
}

@Test
public void testRename_invalidName() {
assertThat(assertThrows(IllegalArgumentException.class, () -> service.renameEquipment(17, "Name!")))
.hasMessageMatching(".*name.*invalid.*");
verifyNoInteractions(mockEquipmentRepo);
}

@ParameterizedTest
@ValueSource(booleans={false,true})
public void testRename(boolean same) {
Equipment eq = new Equipment(17, "Alpha", "bookcase", true);
String newName = (same ? eq.getName() : "Beta");
int id = eq.getId();
when(mockEquipmentRepo.getById(id)).thenReturn(eq);
when(mockEquipmentRepo.save(any())).then(returnArgument());

assertSame(eq, service.renameEquipment(id, newName));
assertEquals(newName, eq.getName());
if (same) {
verify(mockEquipmentRepo, never()).save(any());
} else {
verify(mockEquipmentRepo).save(eq);
}
}
}
Loading