Skip to content

Commit

Permalink
Merge pull request #384 from CaritasDeutschland/refactor-use-rocketch…
Browse files Browse the repository at this point in the history
…at-config

Refactor use rocketchat config
  • Loading branch information
webatspeed authored May 9, 2022
2 parents 1b77380 + 04236a9 commit 2a287eb
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 292 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static java.util.Objects.nonNull;
import static java.util.Objects.requireNonNull;

import de.caritas.cob.userservice.api.adapters.rocketchat.config.RocketChatConfig;
import de.caritas.cob.userservice.api.container.RocketChatCredentials;
import de.caritas.cob.userservice.api.exception.rocketchat.RocketChatLoginException;
import de.caritas.cob.userservice.api.exception.rocketchat.RocketChatUserNotInitializedException;
Expand Down Expand Up @@ -44,19 +45,14 @@ public class RocketChatCredentialsProvider {
@Value("${rocket.systemuser.password}")
private String systemPassword;

@Value("${rocket.chat.api.user.login}")
private String rocketChatApiUserLogin;

@Value("${rocket.chat.api.user.logout}")
private String rocketChatApiUserLogout;

@Value("${rocket.chat.header.auth.token}")
private String rocketChatHeaderAuthToken;
private final @NonNull RestTemplate restTemplate;

@Value("${rocket.chat.header.user.id}")
private String rocketChatHeaderUserId;
private final RocketChatConfig rocketChatConfig;

private final @NonNull RestTemplate restTemplate;
private static final String HEADER_AUTH_TOKEN = "X-Auth-Token";
private static final String HEADER_USER_ID = "X-User-Id";
private static final String ENDPOINT_USER_LOGIN = "/login";
private static final String ENDPOINT_USER_LOGOUT = "/logout";

// Tokens
private final AtomicReference<RocketChatCredentials> techUserA = new AtomicReference<>();
Expand Down Expand Up @@ -207,7 +203,8 @@ public ResponseEntity<LoginResponseDTO> loginUser(String username, String passwo
HttpEntity<MultiValueMap<String, String>> request =
new HttpEntity<>(map, headers);

return restTemplate.postForEntity(rocketChatApiUserLogin, request, LoginResponseDTO.class);
var url = rocketChatConfig.getApiUrl(ENDPOINT_USER_LOGIN);
return restTemplate.postForEntity(url, request, LoginResponseDTO.class);
} catch (Exception ex) {
throw new RocketChatLoginException(
String.format("Could not login user (%s) in Rocket.Chat", username));
Expand All @@ -227,8 +224,8 @@ public boolean logoutUser(String rcUserId, String rcAuthToken) {

HttpEntity<Void> request = new HttpEntity<>(headers);

ResponseEntity<LogoutResponseDTO> response =
restTemplate.postForEntity(rocketChatApiUserLogout, request, LogoutResponseDTO.class);
var url = rocketChatConfig.getApiUrl(ENDPOINT_USER_LOGOUT);
var response = restTemplate.postForEntity(url, request, LogoutResponseDTO.class);

return response.getStatusCode() == HttpStatus.OK;

Expand All @@ -254,11 +251,11 @@ private void logoutUser(AtomicReference<RocketChatCredentials> user) {
* @return a HttpHeaders instance with the standard settings
*/
private HttpHeaders getStandardHttpHeaders(String rcToken, String rcUserId) {

var httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
httpHeaders.add(rocketChatHeaderAuthToken, rcToken);
httpHeaders.add(rocketChatHeaderUserId, rcUserId);
httpHeaders.add(HEADER_AUTH_TOKEN, rcToken);
httpHeaders.add(HEADER_USER_ID, rcUserId);

return httpHeaders;
}

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import static org.springframework.web.util.UriComponentsBuilder.fromUriString;

import java.util.Arrays;
import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.NotBlank;
import lombok.Data;
import org.apache.logging.log4j.core.util.CronExpression;
import org.hibernate.validator.constraints.URL;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
Expand All @@ -27,13 +30,22 @@ public class RocketChatConfig {
@URL
private String baseUrl;

@NotBlank
private String credentialCron;

@Bean("rocketChatRestTemplate")
public RestTemplate rocketChatRestTemplate(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder
.defaultHeader(CONTENT_TYPE, MediaType.APPLICATION_JSON.toString())
.build();
}

@AssertTrue
@SuppressWarnings("unused")
private boolean isCronExpression() {
return CronExpression.isValidExpression(credentialCron);
}

public String getApiUrl(String path) {
return getApiUrl(path, "");
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/application-local.properties
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ rocket.technical.password=technical
rocket.systemuser.id=hjvBcfWH5vS3KLv27
rocket.systemuser.username=system1
rocket.systemuser.password=system
rocket.credentialscheduler.cron=*/10 * * * * ?
rocket-chat.credential-cron=*/10 * * * * ?

# Liquibase
spring.liquibase.change-log=classpath:db/changelog/userservice-local-master.xml
Expand All @@ -44,4 +44,4 @@ spring.liquibase.password=liquibase
app.base.url=http://onlineberatung.local
multitenancy.enabled=false
tenant.service.api.url=http://localhost:8081
rocket.chat.api.url=http://localhost:8082/api/v1
rocket-chat.base-url=http://localhost:8082/api/v1
2 changes: 2 additions & 0 deletions src/main/resources/application-testing.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
app.base.url=https://testing.com

# Testing profile for unit tests
spring.main.allow-bean-definition-overriding=true

Expand Down
24 changes: 1 addition & 23 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -80,35 +80,13 @@ springfox.docuContactEmail=
springfox.docuLicense=Apache 2.0
springfox.docuLicenseUrl=http://www.apache.org/licenses/LICENSE-2.0.html
springfox.docuPath=/users/docs

# Rocket.Chat
rocket.chat.header.auth.token=X-Auth-Token
rocket.chat.header.user.id=X-User-Id
rocket.chat.api.url=${app.base.url}/api/v1
rocket.chat.api.group.create.url=${rocket.chat.api.url}/groups.create
rocket.chat.api.group.delete.url=${rocket.chat.api.url}/groups.delete
rocket.chat.api.group.add.user=${rocket.chat.api.url}/groups.invite
rocket.chat.api.group.remove.user=${rocket.chat.api.url}/groups.kick
rocket.chat.api.group.get.member=${rocket.chat.api.url}/groups.members
rocket.chat.api.group.set.readOnly=${rocket.chat.api.url}/groups.setReadOnly
rocket.chat.api.group.list.all=${rocket.chat.api.url}/groups.listAll
rocket.chat.api.user.login=${rocket.chat.api.url}/login
rocket.chat.api.user.logout=${rocket.chat.api.url}/logout
rocket.chat.api.user.info=${rocket.chat.api.url}/users.info
rocket.chat.api.user.update=${rocket.chat.api.url}/users.update
rocket.chat.api.user.delete=${rocket.chat.api.url}/users.delete
rocket.chat.api.user.list=${rocket.chat.api.url}/users.list
rocket.chat.api.rooms.clean.history=${rocket.chat.api.url}/rooms.cleanHistory
rocket.chat.api.subscriptions.get=${rocket.chat.api.url}/subscriptions.get
rocket.chat.api.rooms.get=${rocket.chat.api.url}/rooms.get
rocket.credentialscheduler.cron=0 0 * * * ?
rocket-chat.credential-cron=0 0 * * * ?
rocket-chat.base-url=${app.base.url}/api/v1

# AgencyService API
agency.service.api.url=${app.base.url}/service
agency.service.api.get.agencies=${agency.service.api.url}/
agency.admin.service.api.url=${app.base.url}

# ConsultingTypeService API
consulting.type.service.api.url=

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package de.caritas.cob.userservice.api.adapters.rocketchat.config;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.ActiveProfiles;

@SpringBootTest
@ActiveProfiles("testing")
class RocketChatConfigIT {

@SpyBean
private RocketChatConfig underTest;

@Test
void configurationShouldLoadProperties() {
assertEquals("https://testing.com/api/v1", underTest.getBaseUrl());
assertEquals("0 0 * * * ?", underTest.getCredentialCron());
}

@Test
void getApiUrlShouldReturnUrlWithPath() {
var path = "/this/is/a/path";
var url = underTest.getApiUrl(path);

assertEquals("https://testing.com/api/v1" + path, url);
}

@Test
void getApiUrlShouldReplacePathParams() {
var value = "a-value";
var path = "/this/is/a/path/{a-variable}/suffix";
var url = underTest.getApiUrl(path, value);

assertEquals("https://testing.com/api/v1/this/is/a/path/a-value/suffix", url);
}

@Test
void getApiUrlShouldRemoveAdditionalSlashes() {
var path = "//this/is//a/path/";
var url = underTest.getApiUrl(path);

assertEquals("https://testing.com/api/v1/this/is/a/path", url);
}

@Test
void getApiUrlShouldReturnUrlWithQueryParams() {
var path = "/this/is/a/path?a=1&b=2";
var url = underTest.getApiUrl(path);

assertEquals("https://testing.com/api/v1" + path, url);
}
}
Loading

0 comments on commit 2a287eb

Please sign in to comment.