Search in sources :

Example 1 with ClientConsentLogEntity

use of com.gw2auth.oauth2.server.repository.client.consent.ClientConsentLogEntity in project oauth2-server by gw2auth.

the class ClientConsentControllerTest method getClientConsentLogPage.

@WithGw2AuthLogin
public void getClientConsentLogPage(MockHttpSession session) throws Exception {
    final long accountId = AuthenticationHelper.getUser(session).orElseThrow().getAccountId();
    final ClientRegistrationEntity clientRegistration = this.testHelper.createClientRegistration(accountId, "Name");
    final ClientConsentEntity clientAuthorization = this.testHelper.createClientConsent(accountId, clientRegistration.id(), Set.of(Gw2ApiPermission.ACCOUNT.oauth2()));
    final Queue<ClientConsentLogEntity> insertedLogs = new PriorityQueue<>(Comparator.comparing(ClientConsentLogEntity::timestamp).reversed());
    for (int i = 0; i < 143; i++) {
        final int generateMessageCount = ThreadLocalRandom.current().nextInt(20);
        final List<String> messages = new ArrayList<>(generateMessageCount);
        for (int j = 0; j < generateMessageCount; j++) {
            messages.add(UUID.randomUUID().toString());
        }
        insertedLogs.offer(this.testHelper.createClientLog(accountId, clientAuthorization.clientRegistrationId(), UUID.randomUUID().toString(), messages));
    }
    final ObjectMapper mapper = new ObjectMapper();
    int page = 0;
    do {
        final String responseJson = this.mockMvc.perform(get("/api/client/consent/{clientId}/logs", clientRegistration.clientId()).session(session).queryParam("page", Integer.toString(page))).andExpect(status().isOk()).andExpect(jsonPath("$.page").exists()).andExpect(jsonPath("$.nextPage").exists()).andExpect(jsonPath("$.logs").exists()).andReturn().getResponse().getContentAsString();
        final JsonNode responseNode = mapper.readTree(responseJson);
        final int nextPage = responseNode.get("nextPage").intValue();
        assertEquals(page, responseNode.get("page").intValue());
        assertTrue(nextPage == page + 1 || nextPage == -1);
        final JsonNode logsNode = responseNode.get("logs");
        assertTrue(logsNode.isArray());
        for (int i = 0; i < logsNode.size(); i++) {
            final ClientConsentLogEntity expectedLog = insertedLogs.poll();
            assertNotNull(expectedLog);
            final JsonNode logNode = logsNode.get(i);
            assertInstantEquals(expectedLog.timestamp(), logNode.get("timestamp").textValue());
            assertEquals(expectedLog.type(), logNode.get("type").textValue());
            final JsonNode messagesNode = logNode.get("messages");
            assertTrue(messagesNode.isArray());
            for (int j = 0; j < messagesNode.size(); j++) {
                assertEquals(expectedLog.messages().get(j), messagesNode.get(j).textValue());
            }
        }
        page = nextPage;
    } while (page != -1);
    assertTrue(insertedLogs.isEmpty());
}
Also used : ClientConsentLogEntity(com.gw2auth.oauth2.server.repository.client.consent.ClientConsentLogEntity) ClientRegistrationEntity(com.gw2auth.oauth2.server.repository.client.registration.ClientRegistrationEntity) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ClientConsentEntity(com.gw2auth.oauth2.server.repository.client.consent.ClientConsentEntity)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ClientConsentEntity (com.gw2auth.oauth2.server.repository.client.consent.ClientConsentEntity)1 ClientConsentLogEntity (com.gw2auth.oauth2.server.repository.client.consent.ClientConsentLogEntity)1 ClientRegistrationEntity (com.gw2auth.oauth2.server.repository.client.registration.ClientRegistrationEntity)1