Repository navigation
COG-1145: W0 smoke suite — admin REST + Boot 1.5 golden JSON fixtures #346
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
Open
patrickbradley-cog
wants to merge
3
commits into
devin/cog-1141-w0-testcontainers-mysql-harness-for-springboottest-boot-154
Choose a base branch
from
devin/cog-1145-w0-smoke-suite-admin-rest-capture-golden-json-fixtures
base: devin/cog-1141-w0-testcontainers-mysql-harness-for-springboottest-boot-154
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
208be1b
COG-1145: admin REST smoke suite, Boot 1.5 golden JSON fixtures and p…
devin-ai-integration[bot] e7a51b6
COG-1145: compare admin fixture rows in date order
devin-ai-integration[bot] 3da5d40
COG-1145: document normalised fixture row order
devin-ai-integration[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
305 changes: 305 additions & 0 deletions
305
UserFront/src/test/java/com/userFront/AdminRestSmokeTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,305 @@ | ||
| package com.userFront; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertNotNull; | ||
| import static org.junit.Assert.assertTrue; | ||
| import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; | ||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.time.Instant; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Comparator; | ||
| import java.util.Date; | ||
| import java.util.List; | ||
|
|
||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; | ||
| import org.springframework.test.web.servlet.request.RequestPostProcessor; | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.node.ArrayNode; | ||
| import com.fasterxml.jackson.databind.node.JsonNodeFactory; | ||
| import com.userFront.dao.AppointmentDao; | ||
| import com.userFront.dao.PrimaryAccountDao; | ||
| import com.userFront.dao.PrimaryTransactionDao; | ||
| import com.userFront.dao.RecipientDao; | ||
| import com.userFront.dao.RoleDao; | ||
| import com.userFront.dao.SavingsAccountDao; | ||
| import com.userFront.dao.SavingsTransactionDao; | ||
| import com.userFront.dao.UserDao; | ||
| import com.userFront.domain.Appointment; | ||
| import com.userFront.domain.PrimaryAccount; | ||
| import com.userFront.domain.PrimaryTransaction; | ||
| import com.userFront.domain.Recipient; | ||
| import com.userFront.domain.SavingsAccount; | ||
| import com.userFront.domain.SavingsTransaction; | ||
| import com.userFront.domain.User; | ||
| import com.userFront.domain.security.Role; | ||
| import com.userFront.domain.security.UserRole; | ||
| import com.userFront.golden.GoldenFixtures; | ||
| import com.userFront.golden.JsonParseEquivalence; | ||
|
|
||
| /** | ||
| * Regression contract for the admin REST API consumed by the AdminPortal, captured on Boot 1.5.4. | ||
| * | ||
| * <p>Responses are compared with {@link JsonParseEquivalence} against the committed | ||
| * {@code golden/boot15/*.json} fixtures; regenerate them with {@code -Dgolden.update=true}. The | ||
| * fixtures hold the response bodies as served, restricted to the {@value #PREFIX}* rows this class | ||
| * seeds (other smoke tests share the JVM, context and database), with transaction and appointment | ||
| * rows sorted by date (the queries have no ORDER BY), and pretty-printed. Generated ids | ||
| * are compared for presence and type only; everything else, including account numbers, balances, | ||
| * dates and the exposed password hash, is seeded with fixed values. | ||
| * | ||
| * <p>Rows are written directly through the DAOs with fixed account numbers rather than through | ||
| * signup, whose static account-number counter restarts per JVM (COG-1179). | ||
| */ | ||
| public class AdminRestSmokeTest extends AbstractIntegrationTest { | ||
|
|
||
| private static final String PREFIX = "adminsmoke-"; | ||
|
|
||
| private static final String ALICE = PREFIX + "alice"; | ||
|
|
||
| private static final String BOB = PREFIX + "bob"; | ||
|
|
||
| /** Only used by the enable/disable test, so its {@code enabled} flag never leaks into a fixture. */ | ||
| private static final String TOGGLE = PREFIX + "toggle"; | ||
|
|
||
| private static final String PASSWORD_HASH = "adminsmoke-fixed-password-hash"; | ||
|
|
||
| /** | ||
| * {@code User.recipientList} has no {@code @JsonIgnore}, so {@code /api/user/all} (and the | ||
| * nested {@code user} of {@code /api/appointment/all}) currently serialises every user's | ||
| * recipients. COG-1150 adds the annotation: flip this to {@code false} and regenerate | ||
| * {@code user-all.json} and {@code appointment-all.json} in that PR. | ||
| */ | ||
| private static final boolean RECIPIENT_LIST_IN_USER_ALL = true; | ||
|
|
||
| private static final JsonParseEquivalence EQUIVALENCE = JsonParseEquivalence.create() | ||
| .ignoringValuesOf("id", "userId"); | ||
|
|
||
| private static final RequestPostProcessor ADMIN = user(PREFIX + "admin").roles("ADMIN"); | ||
|
|
||
| private static final RequestPostProcessor CUSTOMER = user(ALICE).roles("USER"); | ||
|
|
||
| @Autowired | ||
| private UserDao userDao; | ||
|
|
||
| @Autowired | ||
| private RoleDao roleDao; | ||
|
|
||
| @Autowired | ||
| private PrimaryAccountDao primaryAccountDao; | ||
|
|
||
| @Autowired | ||
| private SavingsAccountDao savingsAccountDao; | ||
|
|
||
| @Autowired | ||
| private PrimaryTransactionDao primaryTransactionDao; | ||
|
|
||
| @Autowired | ||
| private SavingsTransactionDao savingsTransactionDao; | ||
|
|
||
| @Autowired | ||
| private RecipientDao recipientDao; | ||
|
|
||
| @Autowired | ||
| private AppointmentDao appointmentDao; | ||
|
|
||
| /** | ||
| * The enable/disable request. Both endpoints are plain {@code @RequestMapping}s reached with GET | ||
| * today; COG-1154 moves them to POST, which only needs this method to change. | ||
| */ | ||
| private static MockHttpServletRequestBuilder toggleUser(String username, String action) { | ||
| return get("/api/user/{username}/{action}", username, action); | ||
| } | ||
|
|
||
| @Before | ||
| public void seed() { | ||
| if (userDao.findByUsername(ALICE) != null) { | ||
| return; | ||
| } | ||
| Role role = new Role(); | ||
| role.setRoleId(1); | ||
| role.setName("ROLE_USER"); | ||
| role = roleDao.save(role); | ||
|
|
||
| User alice = saveUser(ALICE, "Alice", "Admin-Smoke", "5550001001", 91145001, "1500.00", 91145002, "250.75", | ||
| role); | ||
| User bob = saveUser(BOB, "Bob", "Admin-Smoke", "5550001002", 91145003, "0.00", 91145004, "0.00", role); | ||
| saveUser(TOGGLE, "Toggle", "Admin-Smoke", "5550001003", 91145005, "0.00", 91145006, "0.00", role); | ||
|
|
||
| Recipient recipient = new Recipient(); | ||
| recipient.setName(PREFIX + "landlord"); | ||
| recipient.setEmail("adminsmoke-landlord@example.com"); | ||
| recipient.setPhone("5550002001"); | ||
| recipient.setAccountNumber("99887766"); | ||
| recipient.setDescription("Rent"); | ||
| recipient.setUser(alice); | ||
| recipientDao.save(recipient); | ||
|
|
||
| primaryTransactionDao.save(new PrimaryTransaction(at("2017-06-15T10:15:30Z"), "Deposit to Primary Account", | ||
| "Account", "Finished", 2000.0, new BigDecimal("2000.00"), alice.getPrimaryAccount())); | ||
| primaryTransactionDao.save(new PrimaryTransaction(at("2017-06-16T09:00:00Z"), | ||
| "Transfer to recipient " + recipient.getName(), "Transfer", "Finished", 500.0, | ||
| new BigDecimal("1500.00"), alice.getPrimaryAccount())); | ||
| savingsTransactionDao.save(new SavingsTransaction(at("2017-06-17T14:30:00Z"), "Deposit to savings Account", | ||
| "Account", "Finished", 250.75, new BigDecimal("250.75"), alice.getSavingsAccount())); | ||
|
|
||
| saveAppointment(alice, "2017-07-01T15:00:00Z", "Chicago branch", "Mortgage consultation", false); | ||
| saveAppointment(bob, "2017-07-02T16:30:00Z", "New York branch", "Open a savings account", true); | ||
| } | ||
|
|
||
| @Test | ||
| public void userAllMatchesBoot15Fixture() throws Exception { | ||
| JsonNode users = getJsonAsAdmin("/api/user/all"); | ||
| assertTrue(users.isArray()); | ||
|
|
||
| GoldenFixtures.assertMatches(GoldenFixtures.BOOT15, "user-all", onlyUsers(users, ALICE, BOB), EQUIVALENCE); | ||
| } | ||
|
|
||
| @Test | ||
| public void userAllSerialisesRecipientList() throws Exception { | ||
| JsonNode alice = onlyUsers(getJsonAsAdmin("/api/user/all"), ALICE).get(0); | ||
|
|
||
| assertEquals(RECIPIENT_LIST_IN_USER_ALL, alice.has("recipientList")); | ||
| if (RECIPIENT_LIST_IN_USER_ALL) { | ||
| assertEquals(1, alice.get("recipientList").size()); | ||
| assertEquals(PREFIX + "landlord", alice.get("recipientList").get(0).get("name").asText()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void primaryTransactionsMatchBoot15Fixture() throws Exception { | ||
| JsonNode transactions = getJsonAsAdmin("/api/user/primary/transaction?username=" + ALICE); | ||
|
|
||
| GoldenFixtures.assertMatches(GoldenFixtures.BOOT15, "user-primary-transaction", sortedByDate(transactions), | ||
| EQUIVALENCE); | ||
| } | ||
|
|
||
| @Test | ||
| public void savingsTransactionsMatchBoot15Fixture() throws Exception { | ||
| JsonNode transactions = getJsonAsAdmin("/api/user/savings/transaction?username=" + ALICE); | ||
|
|
||
| GoldenFixtures.assertMatches(GoldenFixtures.BOOT15, "user-savings-transaction", sortedByDate(transactions), | ||
| EQUIVALENCE); | ||
| } | ||
|
|
||
| @Test | ||
| public void appointmentAllMatchesBoot15Fixture() throws Exception { | ||
| JsonNode appointments = getJsonAsAdmin("/api/appointment/all"); | ||
| assertTrue(appointments.isArray()); | ||
|
|
||
| List<JsonNode> seeded = new ArrayList<>(); | ||
| for (JsonNode appointment : appointments) { | ||
| if (appointment.path("user").path("username").asText().startsWith(PREFIX)) { | ||
| seeded.add(appointment); | ||
| } | ||
| } | ||
| GoldenFixtures.assertMatches(GoldenFixtures.BOOT15, "appointment-all", sortedByDate(seeded), EQUIVALENCE); | ||
| } | ||
|
|
||
| @Test | ||
| public void disableAndEnableFlipTheEnabledFlag() throws Exception { | ||
| mockMvc.perform(toggleUser(TOGGLE, "disable").with(ADMIN)) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(content().string("")); | ||
| assertFalse(userDao.findByUsername(TOGGLE).isEnabled()); | ||
|
|
||
| mockMvc.perform(toggleUser(TOGGLE, "enable").with(ADMIN)) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(content().string("")); | ||
| assertTrue(userDao.findByUsername(TOGGLE).isEnabled()); | ||
| } | ||
|
|
||
| @Test | ||
| public void customerRoleIsForbiddenFromAdminEndpoints() throws Exception { | ||
| List<MockHttpServletRequestBuilder> adminRequests = Arrays.asList( | ||
| get("/api/user/all"), | ||
| get("/api/user/primary/transaction").param("username", ALICE), | ||
| get("/api/user/savings/transaction").param("username", ALICE), | ||
| get("/api/appointment/all"), | ||
| toggleUser(TOGGLE, "disable"), | ||
| toggleUser(TOGGLE, "enable")); | ||
|
|
||
| for (MockHttpServletRequestBuilder request : adminRequests) { | ||
| mockMvc.perform(request.with(CUSTOMER)).andExpect(status().isForbidden()); | ||
| } | ||
| assertTrue(userDao.findByUsername(TOGGLE).isEnabled()); | ||
| } | ||
|
|
||
| /** Reads and parses the whole body, so a lazy-loading failure anywhere in it fails the test. */ | ||
| private JsonNode getJsonAsAdmin(String url) throws Exception { | ||
| String body = mockMvc.perform(get(url).with(ADMIN)) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) | ||
| .andReturn().getResponse().getContentAsString(); | ||
| return JsonParseEquivalence.parse(body); | ||
| } | ||
|
|
||
| /** The endpoints query without ORDER BY, so rows are compared in date order rather than as returned. */ | ||
| private static ArrayNode sortedByDate(Iterable<JsonNode> rows) { | ||
| List<JsonNode> sorted = new ArrayList<>(); | ||
| rows.forEach(sorted::add); | ||
| sorted.sort(Comparator.comparing(row -> JsonParseEquivalence.parseInstant(row.get("date")))); | ||
| return JsonNodeFactory.instance.arrayNode().addAll(sorted); | ||
| } | ||
|
|
||
| private static ArrayNode onlyUsers(JsonNode users, String... usernames) { | ||
| ArrayNode selected = JsonNodeFactory.instance.arrayNode(); | ||
| for (String username : usernames) { | ||
| JsonNode match = null; | ||
| for (JsonNode user : users) { | ||
| if (username.equals(user.path("username").asText())) { | ||
| match = user; | ||
| } | ||
| } | ||
| assertNotNull("No " + username + " in " + users, match); | ||
| selected.add(match); | ||
| } | ||
| return selected; | ||
| } | ||
|
|
||
| private User saveUser(String username, String firstName, String lastName, String phone, int primaryNumber, | ||
| String primaryBalance, int savingsNumber, String savingsBalance, Role role) { | ||
| PrimaryAccount primary = new PrimaryAccount(); | ||
| primary.setAccountNumber(primaryNumber); | ||
| primary.setAccountBalance(new BigDecimal(primaryBalance)); | ||
| SavingsAccount savings = new SavingsAccount(); | ||
| savings.setAccountNumber(savingsNumber); | ||
| savings.setAccountBalance(new BigDecimal(savingsBalance)); | ||
|
|
||
| User user = new User(); | ||
| user.setUsername(username); | ||
| user.setPassword(PASSWORD_HASH); | ||
| user.setFirstName(firstName); | ||
| user.setLastName(lastName); | ||
| user.setEmail(username + "@example.com"); | ||
| user.setPhone(phone); | ||
| user.setPrimaryAccount(primaryAccountDao.save(primary)); | ||
| user.setSavingsAccount(savingsAccountDao.save(savings)); | ||
| user.getUserRoles().add(new UserRole(user, role)); | ||
| return userDao.save(user); | ||
| } | ||
|
|
||
| private void saveAppointment(User user, String date, String location, String description, boolean confirmed) { | ||
| Appointment appointment = new Appointment(); | ||
| appointment.setUser(user); | ||
| appointment.setDate(at(date)); | ||
| appointment.setLocation(location); | ||
| appointment.setDescription(description); | ||
| appointment.setConfirmed(confirmed); | ||
| appointmentDao.save(appointment); | ||
| } | ||
|
|
||
| private static Date at(String isoInstant) { | ||
| return Date.from(Instant.parse(isoInstant)); | ||
| } | ||
| } | ||
67 changes: 67 additions & 0 deletions
67
UserFront/src/test/java/com/userFront/golden/GoldenFixtures.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package com.userFront.golden; | ||
|
|
||
| import static org.junit.Assert.assertNotNull; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.UncheckedIOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.util.Scanner; | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode; | ||
|
|
||
| /** | ||
| * Golden JSON fixtures under {@code src/test/resources/golden/<set>/<name>.json}. | ||
| * | ||
| * <p>{@link #assertMatches} compares a response against the committed fixture with a | ||
| * {@link JsonParseEquivalence}. Run the tests with {@code -D}{@value #UPDATE_PROPERTY}{@code =true} | ||
| * to (re)write the fixtures from the current responses instead, then commit them. | ||
| */ | ||
| public final class GoldenFixtures { | ||
|
|
||
| /** Responses captured on Spring Boot 1.5.4 (Jackson 2.8, dates as epoch milliseconds). */ | ||
| public static final String BOOT15 = "boot15"; | ||
|
|
||
| public static final String UPDATE_PROPERTY = "golden.update"; | ||
|
|
||
| private static final Path SOURCE_ROOT = Paths.get("src", "test", "resources", "golden"); | ||
|
|
||
| private GoldenFixtures() { | ||
| } | ||
|
|
||
| public static void assertMatches(String set, String name, JsonNode actual, JsonParseEquivalence equivalence) { | ||
| if (Boolean.getBoolean(UPDATE_PROPERTY)) { | ||
| write(set, name, actual); | ||
| return; | ||
| } | ||
| equivalence.assertEquivalent(read(set, name), actual); | ||
| } | ||
|
|
||
| public static JsonNode read(String set, String name) { | ||
| String resource = "/golden/" + set + "/" + name + ".json"; | ||
| try (InputStream in = GoldenFixtures.class.getResourceAsStream(resource)) { | ||
| assertNotNull("Missing golden fixture " + resource + "; generate it with -D" + UPDATE_PROPERTY + "=true", | ||
| in); | ||
| try (Scanner scanner = new Scanner(in, StandardCharsets.UTF_8.name()).useDelimiter("\\A")) { | ||
| return JsonParseEquivalence.parse(scanner.hasNext() ? scanner.next() : ""); | ||
| } | ||
| } | ||
| catch (IOException e) { | ||
| throw new UncheckedIOException(e); | ||
| } | ||
| } | ||
|
|
||
| private static void write(String set, String name, JsonNode actual) { | ||
| Path file = SOURCE_ROOT.resolve(set).resolve(name + ".json"); | ||
| try { | ||
| Files.createDirectories(file.getParent()); | ||
| Files.write(file, JsonParseEquivalence.write(actual).getBytes(StandardCharsets.UTF_8)); | ||
| } | ||
| catch (IOException e) { | ||
| throw new UncheckedIOException(e); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.