Skip to content

Commit

Permalink
feat: implementation of statistic trigger and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
daho4b committed Sep 10, 2021
1 parent b994f78 commit fb998de
Show file tree
Hide file tree
Showing 21 changed files with 880 additions and 64 deletions.
65 changes: 65 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
Expand Down Expand Up @@ -111,6 +115,13 @@
<version>3.11</version>
</dependency>

<!-- Apache Commons Collections -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>

<!-- Apache Commons Codec -->
<dependency>
<groupId>commons-codec</groupId>
Expand Down Expand Up @@ -169,8 +180,39 @@
<version>1.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.fridujo</groupId>
<artifactId>rabbitmq-mock</artifactId>
<version>1.1.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/net.javacrumbs.json-unit/json-unit -->
<dependency>
<groupId>net.javacrumbs.json-unit</groupId>
<artifactId>json-unit</artifactId>
<version>2.25.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<repositories>
<repository>
<id>1_maven.apache.org</id>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<build>
<finalName>${project.name}</finalName>
<resources>
Expand Down Expand Up @@ -232,6 +274,29 @@
<modelPackage>${project.groupId}.${project.artifactId}.userservice.generated.web.model</modelPackage>
</configuration>
</execution>
<!-- Generate client API for statistics service -->
<execution>
<id>statisticsservice-client-model</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<configOptions>
<sourceFolder>/</sourceFolder>
<library>resttemplate</library>
<dateLibrary>java8</dateLibrary>
</configOptions>
<inputSpec>${project.basedir}/services/statisticsservice.yaml</inputSpec>
<generatorName>java</generatorName>
<generateApis>true</generateApis>
<generateApiTests>false</generateApiTests>
<generateModelTests>false</generateModelTests>
<modelPackage>
${project.groupId}.${project.artifactId}.statisticsservice.generated.web.model
</modelPackage>
</configuration>
</execution>
</executions>
</plugin>
<!-- Code style check -->
Expand Down
110 changes: 110 additions & 0 deletions services/statisticsservice.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
openapi: 3.0.1
info:
title: will be replaced
description: This information will be replaced by the SpringFox config information
version: 0.0.1
paths:
/statistics/consultant:
get:
tags:
- statistics-controller
summary: 'Returns statistical data for a consultant. [Authorization: consultant]'
operationId: getConsultantStatistics
responses:
200:
description: OK - successfull operation
400:
description: BAD REQUEST - invalid/incomplete request or body object
403:
description: FORBIDDEN - no/invalid CSRF token
500:
description: INTERNAL SERVER ERROR - server encountered unexpected condition
security:
- Bearer: [ ]
/statistics/consultant/csv:
get:
tags:
- statistics-controller
summary: 'Returns statistical data for a consultant as csv file. [Authorization: consultant]'
operationId: getConsultantStatisticsCsv
responses:
200:
description: OK - successfull operation
400:
description: BAD REQUEST - invalid/incomplete request or body object
403:
description: FORBIDDEN - no/invalid CSRF token
500:
description: INTERNAL SERVER ERROR - server encountered unexpected condition
security:
- Bearer: [ ]

components:
schemas:

EventType:
type: string
enum:
- "ASSIGN_SESSION"
- "CREATE_MESSAGE"

StatisticsEventMessage:
type: object
required:
- eventType
properties:
eventType:
$ref: '#/components/schemas/EventType'

AssignSessionStatisticsEventMessage:
type: object
required:
- consultantId
- sessionId
- timestamp
allOf:
- $ref: '#/components/schemas/StatisticsEventMessage'
- type: object
properties:
consultantId:
type: string
description: The keycloak id of the consultant
example: d63f4cc0-215d-40e2-a866-2d3e910f0590
sessionId:
type: integer
format: int64
description: The id of the session
example: 12345
timestamp:
type: string
description: "Full qualified timestamp"
example: "2018-11-15T09:33:00.057Z"

CreateMessageStatisticsEventMessage:
type: object
required:
- rcUserId
- rcGroupId
- timestamp
allOf:
- $ref: '#/components/schemas/StatisticsEventMessage'
- type: object
properties:
rcUserId:
type: string
description: The Rocket.Chat Id of the consultant
example: yD764hjhz
rcGroupId:
type: string
description: The Rocket.Chat id of the group from the session
example: hzt766asas
timestamp:
type: string
description: "Full qualified timestamp"
example: "2018-11-15T09:33:00.057Z"

securitySchemes:
Bearer:
type: apiKey
name: Authorization
in: header
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
Expand All @@ -23,10 +24,11 @@

@SpringBootApplication
@EnableScheduling
@EnableAsync
public class MessageServiceApplication {

private final String claimNameUserId = "userId";
private final String claimNameUsername = "username";
private static final String CLAIM_NAME_USER_ID = "userId";
private static final String CLAIM_NAME_USERNAME = "username";

public static void main(String[] args) {
SpringApplication.run(MessageServiceApplication.class, args);
Expand All @@ -35,8 +37,8 @@ public static void main(String[] args) {
/**
* Returns the @KeycloakAuthenticationToken which represents the token for a Keycloak
* authentication.
*
*
*
*
* @return KeycloakAuthenticationToken
*/
@Bean
Expand All @@ -47,19 +49,19 @@ public KeycloakAuthenticationToken getAccessToken() {

/**
* Returns the @KeycloakSecurityContext
*
*
* @return KeycloakSecurityContext
*/
@Bean
@Scope(scopeName = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
public KeycloakSecurityContext getKeycloakSecurityContext() {
return (KeycloakSecurityContext) ((KeycloakAuthenticationToken) getRequest().getUserPrincipal())
return ((KeycloakAuthenticationToken) getRequest().getUserPrincipal())
.getAccount().getKeycloakSecurityContext();
}

/**
* Returns the Keycloak user id of the authenticated user
*
*
* @return {@link AuthenticatedUser}
*/
@Bean
Expand All @@ -75,21 +77,21 @@ public AuthenticatedUser getAuthenticatedUser() {

AuthenticatedUser authenticatedUser = new AuthenticatedUser();

if (claimMap.containsKey(claimNameUserId)) {
authenticatedUser.setUserId(claimMap.get(claimNameUserId).toString());
if (claimMap.containsKey(CLAIM_NAME_USER_ID)) {
authenticatedUser.setUserId(claimMap.get(CLAIM_NAME_USER_ID).toString());
} else {
throw new KeycloakException("Keycloak user attribute '" + claimNameUserId + "' not found.");
throw new KeycloakException("Keycloak user attribute '" + CLAIM_NAME_USER_ID + "' not found.");
}

if (claimMap.containsKey(claimNameUsername)) {
authenticatedUser.setUsername(claimMap.get(claimNameUsername).toString());
if (claimMap.containsKey(CLAIM_NAME_USERNAME)) {
authenticatedUser.setUsername(claimMap.get(CLAIM_NAME_USERNAME).toString());
}

// Set user roles
AccessToken.Access realmAccess = ((KeycloakAuthenticationToken) getRequest().getUserPrincipal())
.getAccount().getKeycloakSecurityContext().getToken().getRealmAccess();
Set<String> roles = realmAccess.getRoles();
if (roles != null && roles.size() > 0) {
if (roles != null && roles.isEmpty()) {
authenticatedUser.setRoles(roles);
} else {
throw new KeycloakException(
Expand All @@ -101,7 +103,7 @@ public AuthenticatedUser getAuthenticatedUser() {
// Set granted authorities
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
authenticatedUser.setGrantedAuthorities(authentication.getAuthorities().stream()
.map(authority -> authority.toString()).collect(Collectors.toSet()));
.map(Object::toString).collect(Collectors.toSet()));

// Set Keycloak token to authenticated user object
if (keycloakSecContext.getTokenString() != null) {
Expand Down
Loading

0 comments on commit fb998de

Please sign in to comment.