Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c461aa81cc | |||
| 856fa9e166 | |||
| 7ce01dff0b |
@@ -8,13 +8,18 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|||||||
@Configuration
|
@Configuration
|
||||||
public class CorsConfig implements WebMvcConfigurer {
|
public class CorsConfig implements WebMvcConfigurer {
|
||||||
|
|
||||||
@Value("${vaessl.allowed-origins}")
|
@Value("${vaessl.frontend-local-url}")
|
||||||
private String[] allowedOrigins;
|
private String frontendLocalUrl;
|
||||||
|
|
||||||
|
@Value("${vaessl.frontend-public-url}")
|
||||||
|
private String frontendPublicUrl;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addCorsMappings(CorsRegistry registry) {
|
public void addCorsMappings(CorsRegistry registry) {
|
||||||
registry.addMapping("/**").allowedOrigins(allowedOrigins)
|
registry.addMapping("/**")
|
||||||
|
.allowedOrigins(frontendLocalUrl, frontendPublicUrl)
|
||||||
.allowedMethods("GET", "POST", "DELETE", "OPTIONS")
|
.allowedMethods("GET", "POST", "DELETE", "OPTIONS")
|
||||||
.allowedHeaders("Content-Type", "Accept").allowCredentials(true);
|
.allowedHeaders("Content-Type", "Accept")
|
||||||
|
.allowCredentials(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,11 @@ import org.springframework.web.bind.annotation.PostMapping;
|
|||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import com.vaessl.app.dto.AuthResponse;
|
||||||
|
import com.vaessl.app.dto.ConnectionRequest;
|
||||||
|
import com.vaessl.app.dto.ConnectionStatusResponse;
|
||||||
|
import com.vaessl.app.dto.LoginResult;
|
||||||
|
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpSession;
|
import jakarta.servlet.http.HttpSession;
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
@@ -28,7 +33,8 @@ public class ConnectionController {
|
|||||||
private final ConnectionService connectionService;
|
private final ConnectionService connectionService;
|
||||||
|
|
||||||
@PostMapping("/login")
|
@PostMapping("/login")
|
||||||
public ResponseEntity<AuthResponse> login(@Valid @RequestBody ConnectionRequest request,
|
public ResponseEntity<AuthResponse> login(
|
||||||
|
@Valid @RequestBody ConnectionRequest request,
|
||||||
HttpServletRequest httpReq) {
|
HttpServletRequest httpReq) {
|
||||||
|
|
||||||
LoginResult result = connectionService.login(request);
|
LoginResult result = connectionService.login(request);
|
||||||
@@ -52,21 +58,21 @@ public class ConnectionController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
List<ConnectionStatusResponse> statuses = new ArrayList<>();
|
List<ConnectionStatusResponse> statuses = new ArrayList<>();
|
||||||
Collections.list(session.getAttributeNames()).stream().filter(k -> k.endsWith(SUFFIX))
|
Collections.list(session.getAttributeNames()).stream()
|
||||||
|
.filter(k -> k.endsWith(SUFFIX))
|
||||||
.forEach(k -> {
|
.forEach(k -> {
|
||||||
String serviceType = k.replace(SUFFIX, "");
|
String serviceType = k.replace(SUFFIX, "");
|
||||||
Long id = (Long) session.getAttribute(k);
|
Long id = (Long) session.getAttribute(k);
|
||||||
ConnectionStatusResponse status =
|
ConnectionStatusResponse status = connectionService.getConnectionStatus(serviceType, id);
|
||||||
connectionService.getConnectionStatus(serviceType, id);
|
if (status != null) statuses.add(status);
|
||||||
if (status != null)
|
|
||||||
statuses.add(status);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return ResponseEntity.ok(statuses);
|
return ResponseEntity.ok(statuses);
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/connections/{serviceType}")
|
@DeleteMapping("/connections/{serviceType}")
|
||||||
public ResponseEntity<Void> logout(@PathVariable("serviceType") String serviceType,
|
public ResponseEntity<Void> logout(
|
||||||
|
@PathVariable("serviceType") String serviceType,
|
||||||
HttpServletRequest httpReq) {
|
HttpServletRequest httpReq) {
|
||||||
|
|
||||||
HttpSession session = httpReq.getSession(false);
|
HttpSession session = httpReq.getSession(false);
|
||||||
|
|||||||
@@ -14,8 +14,7 @@ import lombok.NoArgsConstructor;
|
|||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "connections",
|
@Table(name = "connections", uniqueConstraints = { @UniqueConstraint(columnNames = { "appUrl", "username" }) })
|
||||||
uniqueConstraints = {@UniqueConstraint(columnNames = {"appUrl", "username"})})
|
|
||||||
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
|
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
|
||||||
@DiscriminatorColumn(name = "service_type")
|
@DiscriminatorColumn(name = "service_type")
|
||||||
@Getter
|
@Getter
|
||||||
|
|||||||
@@ -2,10 +2,15 @@ package com.vaessl.app.connection;
|
|||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
|
||||||
public interface ConnectionProvider extends ServiceProvider {
|
import com.vaessl.app.dto.ConnectionRequest;
|
||||||
|
import com.vaessl.app.dto.ConnectionResponse;
|
||||||
|
|
||||||
|
public interface ConnectionProvider {
|
||||||
|
|
||||||
void checkCredentials(ConnectionRequest request);
|
void checkCredentials(ConnectionRequest request);
|
||||||
|
|
||||||
|
String getServiceType();
|
||||||
|
|
||||||
ConnectionResponse authenticate(ConnectionRequest request);
|
ConnectionResponse authenticate(ConnectionRequest request);
|
||||||
|
|
||||||
ConnectionEntity findUniqueConnectionEntry(ConnectionRequest request);
|
ConnectionEntity findUniqueConnectionEntry(ConnectionRequest request);
|
||||||
|
|||||||
@@ -7,6 +7,10 @@ import java.util.stream.Collectors;
|
|||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import com.vaessl.app.dto.ConnectionRequest;
|
||||||
|
import com.vaessl.app.dto.ConnectionResponse;
|
||||||
|
import com.vaessl.app.dto.ConnectionStatusResponse;
|
||||||
|
import com.vaessl.app.dto.LoginResult;
|
||||||
import com.vaessl.app.exception.WrongServiceTypeException;
|
import com.vaessl.app.exception.WrongServiceTypeException;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@@ -50,14 +54,13 @@ public class ConnectionService {
|
|||||||
|
|
||||||
public ConnectionStatusResponse getConnectionStatus(String serviceType, Long connectionId) {
|
public ConnectionStatusResponse getConnectionStatus(String serviceType, Long connectionId) {
|
||||||
ConnectionEntity entity = cRepository.findById(connectionId).orElse(null);
|
ConnectionEntity entity = cRepository.findById(connectionId).orElse(null);
|
||||||
if (entity == null)
|
if (entity == null) return null;
|
||||||
return null;
|
|
||||||
|
|
||||||
ConnectionProvider provider = providerRegistry.get(serviceType);
|
ConnectionProvider provider = providerRegistry.get(serviceType);
|
||||||
Instant expiresAt = (provider != null) ? provider.getTokenExpiry(entity) : null;
|
Instant expiresAt = (provider != null) ? provider.getTokenExpiry(entity) : null;
|
||||||
boolean connected = expiresAt == null || expiresAt.isAfter(Instant.now());
|
boolean connected = expiresAt == null || expiresAt.isAfter(Instant.now());
|
||||||
|
|
||||||
return new ConnectionStatusResponse(serviceType, entity.getAppUrl(), entity.getUsername(),
|
return new ConnectionStatusResponse(serviceType, entity.getAppUrl(),
|
||||||
expiresAt, connected);
|
entity.getUsername(), expiresAt, connected);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
package com.vaessl.app.connection;
|
|
||||||
|
|
||||||
import java.time.Instant;
|
|
||||||
|
|
||||||
public record ConnectionStatusResponse(String serviceType, String appUrl, String username,
|
|
||||||
Instant expiresAt, boolean connected) {
|
|
||||||
}
|
|
||||||
@@ -1,8 +1,9 @@
|
|||||||
package com.vaessl.app.connection;
|
package com.vaessl.app.connection;
|
||||||
|
|
||||||
public enum Endpoint {
|
public enum Endpoint {
|
||||||
HOMEBOX_LOGIN("/api/v1/users/login"), LOGIN("/login"), CONNECTION_STATUS(
|
HOMEBOX_LOGIN("/api/v1/users/login"),
|
||||||
"/connections/status"), HOMEBOX_QUERY_ALL_ITEMS("/api/v1/items"), SEARCH("/search");
|
LOGIN("/login"),
|
||||||
|
CONNECTION_STATUS("/connections/status");
|
||||||
|
|
||||||
private final String value;
|
private final String value;
|
||||||
|
|
||||||
|
|||||||
+16
-17
@@ -9,20 +9,20 @@ import java.util.Map;
|
|||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.client.RestClient;
|
import org.springframework.web.client.RestClient;
|
||||||
|
|
||||||
|
import com.vaessl.app.dto.ConnectionRequest;
|
||||||
|
import com.vaessl.app.dto.ConnectionResponse;
|
||||||
import com.vaessl.app.exception.EmptyCredentialsException;
|
import com.vaessl.app.exception.EmptyCredentialsException;
|
||||||
import com.vaessl.app.exception.RemoteApiException;
|
|
||||||
|
|
||||||
import static com.vaessl.app.connection.Endpoint.*;
|
import static com.vaessl.app.connection.Endpoint.*;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class HomeboxConnectionProvider implements ConnectionProvider {
|
public class HomeBoxConnectionProvider implements ConnectionProvider {
|
||||||
|
|
||||||
private final RestClient.Builder restClientBuilder;
|
private final RestClient.Builder restClientBuilder;
|
||||||
|
|
||||||
private final ConnectionRepository cRepository;
|
private final ConnectionRepository cRepository;
|
||||||
|
|
||||||
public HomeboxConnectionProvider(RestClient.Builder restClientBuilder,
|
public HomeBoxConnectionProvider(RestClient.Builder restClientBuilder, ConnectionRepository cRepository) {
|
||||||
ConnectionRepository cRepository) {
|
|
||||||
this.restClientBuilder = restClientBuilder;
|
this.restClientBuilder = restClientBuilder;
|
||||||
this.cRepository = cRepository;
|
this.cRepository = cRepository;
|
||||||
}
|
}
|
||||||
@@ -50,26 +50,27 @@ public class HomeboxConnectionProvider implements ConnectionProvider {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ConnectionResponse authenticate(ConnectionRequest request) {
|
public ConnectionResponse authenticate(ConnectionRequest request) {
|
||||||
Map<String, Object> homeboxPayload = Map.of("username", request.username(), "password",
|
Map<String, Object> homeboxPayload = Map.of("username", request.username(),
|
||||||
request.password(), "stayLoggedIn", request.stayLoggedIn());
|
"password", request.password(), "stayLoggedIn",
|
||||||
|
request.stayLoggedIn());
|
||||||
|
|
||||||
HomeboxLoginResponse hbResponse = restClientBuilder.baseUrl(request.appUrl()).build().post()
|
HomeboxLoginResponse hbResponse = restClientBuilder.baseUrl(request.appUrl())
|
||||||
.uri(HOMEBOX_LOGIN.getValue()).body(homeboxPayload).retrieve()
|
.build()
|
||||||
|
.post()
|
||||||
|
.uri(HOMEBOX_LOGIN.getValue())
|
||||||
|
.body(homeboxPayload)
|
||||||
|
.retrieve()
|
||||||
.body(HomeboxLoginResponse.class);
|
.body(HomeboxLoginResponse.class);
|
||||||
|
|
||||||
if (hbResponse == null) {
|
if (hbResponse == null) {
|
||||||
throw new RemoteApiException(request.appUrl());
|
throw new IllegalStateException("Remote API returned an empty body for " + request.appUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, Object> attachmentToken = new HashMap<>();
|
Map<String, Object> attachmentToken = new HashMap<>();
|
||||||
|
|
||||||
attachmentToken.put("attachmentToken", hbResponse.attachmentToken());
|
attachmentToken.put("attachmentToken", hbResponse.attachmentToken());
|
||||||
|
|
||||||
String hbRawToken = hbResponse.token();
|
return new ConnectionResponse(hbResponse.token(), hbResponse.expiresAt(), attachmentToken);
|
||||||
|
|
||||||
String token = hbRawToken.startsWith("Bearer ") ? hbRawToken.substring(7) : hbRawToken;
|
|
||||||
|
|
||||||
return new ConnectionResponse(token, hbResponse.expiresAt(), attachmentToken);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -79,8 +80,7 @@ public class HomeboxConnectionProvider implements ConnectionProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ConnectionEntity connectionToEntity(ConnectionRequest request,
|
public ConnectionEntity connectionToEntity(ConnectionRequest request, ConnectionResponse response) {
|
||||||
ConnectionResponse response) {
|
|
||||||
return HomeboxEntity.from(request, response);
|
return HomeboxEntity.from(request, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,5 +105,4 @@ public class HomeboxConnectionProvider implements ConnectionProvider {
|
|||||||
private record HomeboxLoginResponse(String token, String attachmentToken, Instant expiresAt) {
|
private record HomeboxLoginResponse(String token, String attachmentToken, Instant expiresAt) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,9 @@ package com.vaessl.app.connection;
|
|||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
|
||||||
|
import com.vaessl.app.dto.ConnectionRequest;
|
||||||
|
import com.vaessl.app.dto.ConnectionResponse;
|
||||||
|
|
||||||
import jakarta.persistence.DiscriminatorValue;
|
import jakarta.persistence.DiscriminatorValue;
|
||||||
import jakarta.persistence.Entity;
|
import jakarta.persistence.Entity;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
package com.vaessl.app.connection;
|
|
||||||
|
|
||||||
public interface ServiceProvider {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the service type key used to look up this provider in a registry, e.g.
|
|
||||||
* {@code "HOMEBOX"}.
|
|
||||||
*/
|
|
||||||
String getServiceType();
|
|
||||||
}
|
|
||||||
@@ -8,7 +8,7 @@ public enum ServiceType {
|
|||||||
|
|
||||||
private final String value;
|
private final String value;
|
||||||
|
|
||||||
private ServiceType(String value) {
|
private ServiceType(String value){
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-3
@@ -1,6 +1,5 @@
|
|||||||
package com.vaessl.app.connection;
|
package com.vaessl.app.dto;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
|
||||||
public record AuthResponse(String serviceType, Instant expiresAt) {
|
public record AuthResponse(String serviceType, Instant expiresAt) {}
|
||||||
}
|
|
||||||
+8
-5
@@ -1,12 +1,15 @@
|
|||||||
package com.vaessl.app.connection;
|
package com.vaessl.app.dto;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
import jakarta.validation.constraints.NotBlank;
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
|
||||||
public record ConnectionRequest(@NotBlank(message = "App URL is mandatory") String appUrl,
|
public record ConnectionRequest(
|
||||||
|
@NotBlank(message = "App URL is mandatory") String appUrl,
|
||||||
@NotBlank(message = "Service type is mandatory") String serviceType,
|
@NotBlank(message = "Service type is mandatory") String serviceType,
|
||||||
String username, String password, String apiKey,
|
String username,
|
||||||
|
String password,
|
||||||
|
String apiKey,
|
||||||
@JsonProperty(defaultValue = "false") Boolean stayLoggedIn) {
|
@JsonProperty(defaultValue = "false") Boolean stayLoggedIn) {
|
||||||
|
|
||||||
public ConnectionRequest {
|
public ConnectionRequest {
|
||||||
@@ -15,8 +18,8 @@ public record ConnectionRequest(@NotBlank(message = "App URL is mandatory") Stri
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ConnectionRequest(String appUrl, String serviceType, String username,
|
public ConnectionRequest(String appUrl, String serviceType, String username, String password,
|
||||||
String password, Boolean stayLoggedIn) {
|
Boolean stayLoggedIn) {
|
||||||
this(appUrl, serviceType, username, password, null, stayLoggedIn);
|
this(appUrl, serviceType, username, password, null, stayLoggedIn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+3
-4
@@ -1,13 +1,12 @@
|
|||||||
package com.vaessl.app.connection;
|
package com.vaessl.app.dto;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public record ConnectionResponse(String token, Instant expiresAt,
|
public record ConnectionResponse(String token, Instant expiresAt, Map<String, Object> extraResponseData) {
|
||||||
Map<String, Object> extraResponseData) {
|
|
||||||
|
|
||||||
public String getExtraVar(String key) {
|
public String getExtraVar(String key) {
|
||||||
if (extraResponseData == null) {
|
if(extraResponseData == null) {
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
Object value = extraResponseData.get(key);
|
Object value = extraResponseData.get(key);
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.vaessl.app.dto;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
|
||||||
|
public record ConnectionStatusResponse(
|
||||||
|
String serviceType,
|
||||||
|
String appUrl,
|
||||||
|
String username,
|
||||||
|
Instant expiresAt,
|
||||||
|
boolean connected) {}
|
||||||
+2
-3
@@ -1,6 +1,5 @@
|
|||||||
package com.vaessl.app.connection;
|
package com.vaessl.app.dto;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
|
||||||
public record LoginResult(Long connectionId, Instant expiresAt) {
|
public record LoginResult(Long connectionId, Instant expiresAt) {}
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
package com.vaessl.app.exception;
|
|
||||||
|
|
||||||
public class ConnectionNotFoundException extends RuntimeException {
|
|
||||||
}
|
|
||||||
@@ -5,18 +5,11 @@ import org.springframework.http.HttpStatus;
|
|||||||
import com.fasterxml.jackson.annotation.JsonValue;
|
import com.fasterxml.jackson.annotation.JsonValue;
|
||||||
|
|
||||||
public enum ErrorMessage {
|
public enum ErrorMessage {
|
||||||
BAD_REQUEST_EMPTY_FIELDS(HttpStatus.BAD_REQUEST,
|
BAD_REQUEST_EMPTY_FIELDS(HttpStatus.BAD_REQUEST, "Fields must not be empty."), UNAUTHORIZED_WRONG_LOGIN(
|
||||||
"Fields must not be empty."), UNAUTHORIZED_WRONG_LOGIN(HttpStatus.UNAUTHORIZED,
|
HttpStatus.UNAUTHORIZED, "Invalid username or password."), SERVICE_UNAVAILABLE_UNREACHABLE_URL(
|
||||||
"Invalid username or password."), SERVICE_UNAVAILABLE_UNREACHABLE_URL(
|
HttpStatus.SERVICE_UNAVAILABLE, "The target URL is unreachable."), SERVER_ERROR_GENERAL(
|
||||||
HttpStatus.SERVICE_UNAVAILABLE,
|
"The external app returned a server error: "), WRONG_SERVICE_TYPE(HttpStatus.NOT_FOUND,
|
||||||
"The target URL is unreachable."), SERVER_ERROR_GENERAL(
|
"No such service type.");
|
||||||
"The external app returned a server error: "), WRONG_SERVICE_TYPE(
|
|
||||||
HttpStatus.NOT_FOUND,
|
|
||||||
"No such service type."), CONNECTION_NOT_FOUND(
|
|
||||||
HttpStatus.NOT_FOUND,
|
|
||||||
"No active connection found for this service."), REMOTE_API_EMPTY_RESPONSE(
|
|
||||||
HttpStatus.BAD_GATEWAY,
|
|
||||||
"Remote API returned empty response for ");
|
|
||||||
|
|
||||||
private final HttpStatus status;
|
private final HttpStatus status;
|
||||||
private final String message;
|
private final String message;
|
||||||
|
|||||||
@@ -19,8 +19,8 @@ public class GlobalExceptionHandler {
|
|||||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||||
public ProblemDetail handleEmptyCredentialInput(MethodArgumentNotValidException e) {
|
public ProblemDetail handleEmptyCredentialInput(MethodArgumentNotValidException e) {
|
||||||
|
|
||||||
String defaultMessages = e.getBindingResult().getFieldErrors().stream()
|
String defaultMessages = e.getBindingResult().getFieldErrors().stream().map(FieldError::getDefaultMessage)
|
||||||
.map(FieldError::getDefaultMessage).collect(Collectors.joining(", "));
|
.collect(Collectors.joining(", "));
|
||||||
|
|
||||||
return ProblemDetail.forStatusAndDetail(BAD_REQUEST_EMPTY_FIELDS.getStatus(),
|
return ProblemDetail.forStatusAndDetail(BAD_REQUEST_EMPTY_FIELDS.getStatus(),
|
||||||
BAD_REQUEST_EMPTY_FIELDS.getMessage() + " [" + defaultMessages + "]");
|
BAD_REQUEST_EMPTY_FIELDS.getMessage() + " [" + defaultMessages + "]");
|
||||||
@@ -43,14 +43,14 @@ public class GlobalExceptionHandler {
|
|||||||
@ExceptionHandler(HttpServerErrorException.class)
|
@ExceptionHandler(HttpServerErrorException.class)
|
||||||
public ProblemDetail handleTimeoutOrNotFound(HttpServerErrorException e) {
|
public ProblemDetail handleTimeoutOrNotFound(HttpServerErrorException e) {
|
||||||
|
|
||||||
return ProblemDetail.forStatusAndDetail(e.getStatusCode(),
|
return ProblemDetail
|
||||||
SERVER_ERROR_GENERAL.getMessage() + e.getStatusText());
|
.forStatusAndDetail(e.getStatusCode(),
|
||||||
|
SERVER_ERROR_GENERAL.getMessage() + e.getStatusText());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(WrongServiceTypeException.class)
|
@ExceptionHandler(WrongServiceTypeException.class)
|
||||||
public ProblemDetail handleWrongServiceType(WrongServiceTypeException e) {
|
public ProblemDetail handleWrongServiceType(WrongServiceTypeException e) {
|
||||||
return ProblemDetail.forStatusAndDetail(WRONG_SERVICE_TYPE.getStatus(),
|
return ProblemDetail.forStatusAndDetail(WRONG_SERVICE_TYPE.getStatus(), WRONG_SERVICE_TYPE.getMessage());
|
||||||
WRONG_SERVICE_TYPE.getMessage());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(EmptyCredentialsException.class)
|
@ExceptionHandler(EmptyCredentialsException.class)
|
||||||
@@ -58,15 +58,4 @@ public class GlobalExceptionHandler {
|
|||||||
return ProblemDetail.forStatusAndDetail(BAD_REQUEST_EMPTY_FIELDS.getStatus(),
|
return ProblemDetail.forStatusAndDetail(BAD_REQUEST_EMPTY_FIELDS.getStatus(),
|
||||||
BAD_REQUEST_EMPTY_FIELDS.getMessage() + " " + e.getMissingFields());
|
BAD_REQUEST_EMPTY_FIELDS.getMessage() + " " + e.getMissingFields());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(ConnectionNotFoundException.class)
|
|
||||||
public ProblemDetail handleConnectionNotFound(ConnectionNotFoundException e) {
|
|
||||||
return ProblemDetail.forStatusAndDetail(CONNECTION_NOT_FOUND.getStatus(), CONNECTION_NOT_FOUND.getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
@ExceptionHandler(RemoteApiException.class)
|
|
||||||
public ProblemDetail handleRemoteApiException(RemoteApiException e) {
|
|
||||||
return ProblemDetail.forStatusAndDetail(REMOTE_API_EMPTY_RESPONSE.getStatus(),
|
|
||||||
REMOTE_API_EMPTY_RESPONSE.getMessage() + e.getAppUrl());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +0,0 @@
|
|||||||
package com.vaessl.app.exception;
|
|
||||||
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class RemoteApiException extends RuntimeException {
|
|
||||||
|
|
||||||
private final String appUrl;
|
|
||||||
}
|
|
||||||
@@ -1,79 +0,0 @@
|
|||||||
package com.vaessl.app.search;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.springframework.data.domain.Page;
|
|
||||||
import org.springframework.data.domain.PageImpl;
|
|
||||||
import org.springframework.data.domain.Pageable;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
import org.springframework.web.client.RestClient;
|
|
||||||
|
|
||||||
import com.vaessl.app.connection.ConnectionEntity;
|
|
||||||
import com.vaessl.app.connection.ConnectionRepository;
|
|
||||||
import com.vaessl.app.connection.HomeboxEntity;
|
|
||||||
import com.vaessl.app.exception.ConnectionNotFoundException;
|
|
||||||
import com.vaessl.app.exception.RemoteApiException;
|
|
||||||
|
|
||||||
import static com.vaessl.app.connection.Endpoint.*;
|
|
||||||
|
|
||||||
@Component
|
|
||||||
public class HomeboxSearchProvider implements SearchProvider {
|
|
||||||
|
|
||||||
private final RestClient.Builder restClientBuilder;
|
|
||||||
|
|
||||||
private final ConnectionRepository cRepository;
|
|
||||||
|
|
||||||
public HomeboxSearchProvider(RestClient.Builder restClientBuilder,
|
|
||||||
ConnectionRepository cRepository) {
|
|
||||||
this.restClientBuilder = restClientBuilder;
|
|
||||||
this.cRepository = cRepository;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getServiceType() {
|
|
||||||
return "HOMEBOX";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Page<SearchResponse> getSearchResults(SearchRequest request, Pageable pageable) {
|
|
||||||
|
|
||||||
ConnectionEntity entity =
|
|
||||||
cRepository.findByAppUrlAndUsername(request.appUrl(), request.username());
|
|
||||||
|
|
||||||
if (!(entity instanceof HomeboxEntity hbEntity)) {
|
|
||||||
throw new ConnectionNotFoundException();
|
|
||||||
}
|
|
||||||
|
|
||||||
HomeboxSearchResponse hbResponse = restClientBuilder.baseUrl(request.appUrl()).build().get()
|
|
||||||
.uri(u -> u.path(HOMEBOX_QUERY_ALL_ITEMS.getValue())
|
|
||||||
.queryParam("q", request.query())
|
|
||||||
.queryParam("page", pageable.getPageNumber() + 1)
|
|
||||||
.queryParam("pageSize", pageable.getPageSize()).build())
|
|
||||||
.headers(h -> h.setBearerAuth(
|
|
||||||
hbEntity.getToken())).retrieve().body(HomeboxSearchResponse.class);
|
|
||||||
|
|
||||||
if (hbResponse == null) {
|
|
||||||
throw new RemoteApiException(request.appUrl());
|
|
||||||
}
|
|
||||||
|
|
||||||
List<SearchResponse> items = hbResponse.items().stream().map(i -> {
|
|
||||||
String title = i.name();
|
|
||||||
String description = i.description();
|
|
||||||
Map<String, Object> extraSearchResponseData = Map.of("location", i.location());
|
|
||||||
return new SearchResponse(title, description, extraSearchResponseData);
|
|
||||||
}).toList();
|
|
||||||
|
|
||||||
return new PageImpl<>(items, pageable, hbResponse.total());
|
|
||||||
}
|
|
||||||
|
|
||||||
private record HomeboxSearchResponse(int page, int pageSize, int total,
|
|
||||||
List<HomeboxItem> items) {
|
|
||||||
}
|
|
||||||
|
|
||||||
private record HomeboxItem(String name, String description, HomeboxLocation location) {
|
|
||||||
}
|
|
||||||
|
|
||||||
private record HomeboxLocation(String name, String description) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
package com.vaessl.app.search;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import org.springframework.data.domain.Page;
|
|
||||||
|
|
||||||
public record PagedSearchResponse<T>(List<T> content, int page, int pageSize, long totalElements,
|
|
||||||
boolean first, boolean last, String sort) {
|
|
||||||
|
|
||||||
public static <T> PagedSearchResponse<T> from(Page<T> pageResult) {
|
|
||||||
return new PagedSearchResponse<>(pageResult.getContent(), pageResult.getNumber(),
|
|
||||||
pageResult.getSize(), pageResult.getTotalElements(), pageResult.isFirst(),
|
|
||||||
pageResult.isLast(), pageResult.getSort().toString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
package com.vaessl.app.search;
|
|
||||||
|
|
||||||
import org.springframework.data.domain.Page;
|
|
||||||
import org.springframework.data.domain.Pageable;
|
|
||||||
import org.springframework.data.web.PageableDefault;
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
|
||||||
import jakarta.servlet.http.HttpSession;
|
|
||||||
import jakarta.validation.Valid;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class SearchController {
|
|
||||||
|
|
||||||
private final SearchService searchService;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Executes a paged search against the requested service. Returns {@code 401 Unauthorized} if
|
|
||||||
* there is no active session.
|
|
||||||
*/
|
|
||||||
@PostMapping("/search")
|
|
||||||
public ResponseEntity<PagedSearchResponse<SearchResponse>> search(
|
|
||||||
@Valid @RequestBody SearchRequest request,
|
|
||||||
@PageableDefault(size = 20) Pageable pageable, HttpServletRequest httpReq) {
|
|
||||||
HttpSession session = httpReq.getSession(false);
|
|
||||||
if (session == null
|
|
||||||
|| session.getAttribute(request.serviceType() + "_CONNECTION_ID") == null) {
|
|
||||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
Page<SearchResponse> result = searchService.search(request, pageable);
|
|
||||||
|
|
||||||
return ResponseEntity.ok(PagedSearchResponse.from(result));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
package com.vaessl.app.search;
|
|
||||||
|
|
||||||
import org.springframework.data.domain.Page;
|
|
||||||
import org.springframework.data.domain.Pageable;
|
|
||||||
|
|
||||||
import com.vaessl.app.connection.ServiceProvider;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Implemented by any service that supports querying its remote API for items.
|
|
||||||
*/
|
|
||||||
public interface SearchProvider extends ServiceProvider {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Executes a search query against the remote service and returns matching results.
|
|
||||||
*
|
|
||||||
* @param request the search request containing the query string, app URL, and user credentials
|
|
||||||
* @return a list of Page<SearchResponse> items matching the query
|
|
||||||
*/
|
|
||||||
Page<SearchResponse> getSearchResults(SearchRequest request, Pageable pageable);
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
package com.vaessl.app.search;
|
|
||||||
|
|
||||||
import jakarta.validation.constraints.NotBlank;
|
|
||||||
|
|
||||||
public record SearchRequest(@NotBlank String appUrl, @NotBlank String username, String query,
|
|
||||||
@NotBlank String serviceType) {
|
|
||||||
}
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
package com.vaessl.app.search;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public record SearchResponse(String title, String description, Map<String, Object> extraData) {
|
|
||||||
|
|
||||||
public String getExtra(String key) {
|
|
||||||
if (extraData == null) {
|
|
||||||
return null;
|
|
||||||
} else {
|
|
||||||
Object value = extraData.get(key);
|
|
||||||
|
|
||||||
return value != null ? String.valueOf(value) : null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
package com.vaessl.app.search;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
import org.springframework.data.domain.Page;
|
|
||||||
import org.springframework.data.domain.Pageable;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import com.vaessl.app.exception.WrongServiceTypeException;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class SearchService {
|
|
||||||
|
|
||||||
private final Map<String, SearchProvider> providerRegistry;
|
|
||||||
|
|
||||||
public SearchService(List<SearchProvider> providers) {
|
|
||||||
this.providerRegistry = Map.copyOf(providers.stream()
|
|
||||||
.collect(Collectors.toMap(SearchProvider::getServiceType, p -> p)));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Dispatches the paged search request to the provider registered for
|
|
||||||
* {@link SearchRequest#serviceType()}.
|
|
||||||
*
|
|
||||||
* @param request the search request
|
|
||||||
* @param pageable the Pageable interface
|
|
||||||
* @return results returned by the matching provider
|
|
||||||
* @throws WrongServiceTypeException if no provider is registered for the given service type
|
|
||||||
*/
|
|
||||||
public Page<SearchResponse> search(SearchRequest request, Pageable pageable) {
|
|
||||||
|
|
||||||
SearchProvider provider = providerRegistry.get(request.serviceType());
|
|
||||||
|
|
||||||
if (provider == null) {
|
|
||||||
throw new WrongServiceTypeException();
|
|
||||||
}
|
|
||||||
|
|
||||||
return provider.getSearchResults(request, pageable);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -5,8 +5,13 @@
|
|||||||
"description": "A description for 'spring.session.store-type'"
|
"description": "A description for 'spring.session.store-type'"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "vaessl.allowed-origins",
|
"name": "vaessl.frontend-local-url",
|
||||||
"type": "java.lang.String",
|
"type": "java.lang.String",
|
||||||
"description": "Comma-separated list of allowed CORS origins"
|
"description": "A description for 'vaessl.frontend-local-url'"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "vaessl.frontend-public-url",
|
||||||
|
"type": "java.lang.String",
|
||||||
|
"description": "A description for 'vaessl.frontend-public-url'"
|
||||||
}
|
}
|
||||||
]}
|
]}
|
||||||
@@ -13,7 +13,7 @@ spring:
|
|||||||
driver-class-name: ${PG_DRIVER_CLASS_NAME}
|
driver-class-name: ${PG_DRIVER_CLASS_NAME}
|
||||||
jpa:
|
jpa:
|
||||||
hibernate:
|
hibernate:
|
||||||
ddl-auto: update
|
ddl-auto: create-drop
|
||||||
show-sql: true
|
show-sql: true
|
||||||
ai:
|
ai:
|
||||||
openai:
|
openai:
|
||||||
@@ -30,4 +30,5 @@ server:
|
|||||||
servlet:
|
servlet:
|
||||||
context-path: /api
|
context-path: /api
|
||||||
vaessl:
|
vaessl:
|
||||||
allowed-origins: ${ALLOWED_ORIGINS}
|
frontend-local-url: ${FRONTEND_LOCAL_URL}
|
||||||
|
frontend-public-url: ${FRONTEND_PUBLIC_URL}
|
||||||
|
|||||||
@@ -20,7 +20,8 @@ class ApplicationTests {
|
|||||||
private DataSource dataSource;
|
private DataSource dataSource;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void contextLoads() {}
|
void contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void connectionToTestDbWorks() throws SQLException {
|
void connectionToTestDbWorks() throws SQLException {
|
||||||
|
|||||||
@@ -1,13 +0,0 @@
|
|||||||
package com.vaessl.app;
|
|
||||||
|
|
||||||
public final class Mockdata {
|
|
||||||
|
|
||||||
private Mockdata() {}
|
|
||||||
|
|
||||||
public static final String MOCK_URL = "http://localhost:1234";
|
|
||||||
public static final String MOCK_SERVICE_TYPE = "SERVICE_TYPE";
|
|
||||||
public static final String MOCK_USER = "user";
|
|
||||||
public static final String MOCK_PASS = "pw";
|
|
||||||
public static final String MOCK_TITLE = "title";
|
|
||||||
public static final String MOCK_DESCRIPTION = "desc";
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,5 @@
|
|||||||
package com.vaessl.app.connection;
|
package com.vaessl.app.connection;
|
||||||
|
|
||||||
import static com.vaessl.app.Mockdata.*;
|
|
||||||
|
|
||||||
import static com.vaessl.app.connection.Endpoint.*;
|
import static com.vaessl.app.connection.Endpoint.*;
|
||||||
import static com.vaessl.app.connection.ServiceType.*;
|
import static com.vaessl.app.connection.ServiceType.*;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||||
@@ -26,129 +24,134 @@ import org.springframework.test.web.servlet.MvcResult;
|
|||||||
@WireMockTest
|
@WireMockTest
|
||||||
class ConnectionControllerTest {
|
class ConnectionControllerTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
MockMvc mockMvc;
|
MockMvc mockMvc;
|
||||||
|
|
||||||
private static final String LOGIN_PATH = LOGIN.getValue();
|
private static final String TEST_USER = "admin";
|
||||||
private static final String STATUS_PATH = CONNECTION_STATUS.getValue();
|
private static final String TEST_PASS = "pw";
|
||||||
private static final String LOGOUT_PATH = "/connections/HOMEBOX";
|
private static final String LOGIN_PATH = LOGIN.getValue();
|
||||||
|
private static final String STATUS_PATH = CONNECTION_STATUS.getValue();
|
||||||
|
private static final String LOGOUT_PATH = "/connections/HOMEBOX";
|
||||||
|
|
||||||
private static final String VALID_HOMEBOX_RESPONSE = """
|
private static final String VALID_HOMEBOX_RESPONSE = """
|
||||||
{
|
{
|
||||||
"token": "fake-jwt-token",
|
"token": "fake-jwt-token",
|
||||||
"attachmentToken": "fake-attach",
|
"attachmentToken": "fake-attach",
|
||||||
"expiresAt": "2099-01-01T00:00:00Z"
|
"expiresAt": "2099-01-01T00:00:00Z"
|
||||||
}
|
}
|
||||||
""";
|
""";
|
||||||
|
|
||||||
private static final String EXPIRED_HOMEBOX_RESPONSE = """
|
private static final String EXPIRED_HOMEBOX_RESPONSE = """
|
||||||
{
|
{
|
||||||
"token": "expired-token",
|
"token": "expired-token",
|
||||||
"attachmentToken": "fake-attach",
|
"attachmentToken": "fake-attach",
|
||||||
"expiresAt": "2000-01-01T00:00:00Z"
|
"expiresAt": "2000-01-01T00:00:00Z"
|
||||||
}
|
}
|
||||||
""";
|
""";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldReturnEmptyListWhenNoActiveSession() throws Exception {
|
void shouldReturnEmptyListWhenNoActiveSession() throws Exception {
|
||||||
mockMvc.perform(get(STATUS_PATH)).andExpect(status().isOk())
|
mockMvc.perform(get(STATUS_PATH))
|
||||||
.andExpect(content().json("[]"));
|
.andExpect(status().isOk())
|
||||||
}
|
.andExpect(content().json("[]"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldReturnConnectionStatusWithConnectedTrueAfterLogin(WireMockRuntimeInfo wm)
|
void shouldReturnConnectionStatusWithConnectedTrueAfterLogin(WireMockRuntimeInfo wm) throws Exception {
|
||||||
throws Exception {
|
WireMock.stubFor(WireMock.post(HOMEBOX_LOGIN.getValue()).willReturn(WireMock.okJson(VALID_HOMEBOX_RESPONSE)));
|
||||||
WireMock.stubFor(WireMock.post(HOMEBOX_LOGIN.getValue())
|
|
||||||
.willReturn(WireMock.okJson(VALID_HOMEBOX_RESPONSE)));
|
|
||||||
|
|
||||||
MvcResult loginResult = mockMvc
|
MvcResult loginResult = mockMvc.perform(post(LOGIN_PATH)
|
||||||
.perform(post(LOGIN_PATH).contentType(MediaType.APPLICATION_JSON)
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
.content(connectionRequestBody(wm)))
|
.content(connectionRequestBody(wm)))
|
||||||
.andExpect(status().isOk()).andReturn();
|
.andExpect(status().isOk())
|
||||||
|
.andReturn();
|
||||||
|
|
||||||
Cookie sessionCookie = loginResult.getResponse().getCookie("SESSION");
|
Cookie sessionCookie = loginResult.getResponse().getCookie("SESSION");
|
||||||
|
|
||||||
mockMvc.perform(get(STATUS_PATH).cookie(sessionCookie)).andExpect(status().isOk())
|
mockMvc.perform(get(STATUS_PATH).cookie(sessionCookie))
|
||||||
.andExpect(jsonPath("$.length()").value(1))
|
.andExpect(status().isOk())
|
||||||
.andExpect(jsonPath("$[0].serviceType").value(HOMEBOX.getValue()))
|
.andExpect(jsonPath("$.length()").value(1))
|
||||||
.andExpect(jsonPath("$[0].username").value(MOCK_USER))
|
.andExpect(jsonPath("$[0].serviceType").value(HOMEBOX.getValue()))
|
||||||
.andExpect(jsonPath("$[0].appUrl").value(wm.getHttpBaseUrl()))
|
.andExpect(jsonPath("$[0].username").value(TEST_USER))
|
||||||
.andExpect(jsonPath("$[0].connected").value(true));
|
.andExpect(jsonPath("$[0].appUrl").value(wm.getHttpBaseUrl()))
|
||||||
}
|
.andExpect(jsonPath("$[0].connected").value(true));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldReturnConnectedFalseWhenStoredTokenIsExpired(WireMockRuntimeInfo wm)
|
void shouldReturnConnectedFalseWhenStoredTokenIsExpired(WireMockRuntimeInfo wm) throws Exception {
|
||||||
throws Exception {
|
WireMock.stubFor(WireMock.post(HOMEBOX_LOGIN.getValue()).willReturn(WireMock.okJson(EXPIRED_HOMEBOX_RESPONSE)));
|
||||||
WireMock.stubFor(WireMock.post(HOMEBOX_LOGIN.getValue())
|
|
||||||
.willReturn(WireMock.okJson(EXPIRED_HOMEBOX_RESPONSE)));
|
|
||||||
|
|
||||||
MvcResult loginResult = mockMvc
|
MvcResult loginResult = mockMvc.perform(post(LOGIN_PATH)
|
||||||
.perform(post(LOGIN_PATH).contentType(MediaType.APPLICATION_JSON)
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
.content(connectionRequestBody(wm)))
|
.content(connectionRequestBody(wm)))
|
||||||
.andExpect(status().isOk()).andReturn();
|
.andExpect(status().isOk())
|
||||||
|
.andReturn();
|
||||||
|
|
||||||
Cookie sessionCookie = loginResult.getResponse().getCookie("SESSION");
|
Cookie sessionCookie = loginResult.getResponse().getCookie("SESSION");
|
||||||
|
|
||||||
mockMvc.perform(get(STATUS_PATH).cookie(sessionCookie)).andExpect(status().isOk())
|
mockMvc.perform(get(STATUS_PATH).cookie(sessionCookie))
|
||||||
.andExpect(jsonPath("$[0].serviceType").value(HOMEBOX.getValue()))
|
.andExpect(status().isOk())
|
||||||
.andExpect(jsonPath("$[0].connected").value(false))
|
.andExpect(jsonPath("$[0].serviceType").value(HOMEBOX.getValue()))
|
||||||
.andExpect(jsonPath("$[0].expiresAt")
|
.andExpect(jsonPath("$[0].connected").value(false))
|
||||||
.value("2000-01-01T00:00:00Z"));
|
.andExpect(jsonPath("$[0].expiresAt").value("2000-01-01T00:00:00Z"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldReturn204NoContentOnLogout(WireMockRuntimeInfo wm) throws Exception {
|
void shouldReturn204NoContentOnLogout(WireMockRuntimeInfo wm) throws Exception {
|
||||||
WireMock.stubFor(WireMock.post(HOMEBOX_LOGIN.getValue())
|
WireMock.stubFor(WireMock.post(HOMEBOX_LOGIN.getValue()).willReturn(WireMock.okJson(VALID_HOMEBOX_RESPONSE)));
|
||||||
.willReturn(WireMock.okJson(VALID_HOMEBOX_RESPONSE)));
|
|
||||||
|
|
||||||
MvcResult loginResult = mockMvc
|
MvcResult loginResult = mockMvc.perform(post(LOGIN_PATH)
|
||||||
.perform(post(LOGIN_PATH).contentType(MediaType.APPLICATION_JSON)
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
.content(connectionRequestBody(wm)))
|
.content(connectionRequestBody(wm)))
|
||||||
.andExpect(status().isOk()).andReturn();
|
.andExpect(status().isOk())
|
||||||
|
.andReturn();
|
||||||
|
|
||||||
Cookie sessionCookie = loginResult.getResponse().getCookie("SESSION");
|
Cookie sessionCookie = loginResult.getResponse().getCookie("SESSION");
|
||||||
|
|
||||||
mockMvc.perform(delete(LOGOUT_PATH).cookie(sessionCookie))
|
mockMvc.perform(delete(LOGOUT_PATH).cookie(sessionCookie))
|
||||||
.andExpect(status().isNoContent());
|
.andExpect(status().isNoContent());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldReturnEmptyStatusListAfterLogout(WireMockRuntimeInfo wm) throws Exception {
|
void shouldReturnEmptyStatusListAfterLogout(WireMockRuntimeInfo wm) throws Exception {
|
||||||
WireMock.stubFor(WireMock.post(HOMEBOX_LOGIN.getValue())
|
WireMock.stubFor(WireMock.post(HOMEBOX_LOGIN.getValue()).willReturn(WireMock.okJson(VALID_HOMEBOX_RESPONSE)));
|
||||||
.willReturn(WireMock.okJson(VALID_HOMEBOX_RESPONSE)));
|
|
||||||
|
|
||||||
MvcResult loginResult = mockMvc
|
MvcResult loginResult = mockMvc.perform(post(LOGIN_PATH)
|
||||||
.perform(post(LOGIN_PATH).contentType(MediaType.APPLICATION_JSON)
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
.content(connectionRequestBody(wm)))
|
.content(connectionRequestBody(wm)))
|
||||||
.andExpect(status().isOk()).andReturn();
|
.andExpect(status().isOk())
|
||||||
|
.andReturn();
|
||||||
|
|
||||||
Cookie sessionCookie = loginResult.getResponse().getCookie("SESSION");
|
Cookie sessionCookie = loginResult.getResponse().getCookie("SESSION");
|
||||||
|
|
||||||
// Verify connected before logout
|
// Verify connected before logout
|
||||||
mockMvc.perform(get(STATUS_PATH).cookie(sessionCookie)).andExpect(status().isOk())
|
mockMvc.perform(get(STATUS_PATH).cookie(sessionCookie))
|
||||||
.andExpect(jsonPath("$.length()").value(1));
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(jsonPath("$.length()").value(1));
|
||||||
|
|
||||||
mockMvc.perform(delete(LOGOUT_PATH).cookie(sessionCookie))
|
mockMvc.perform(delete(LOGOUT_PATH).cookie(sessionCookie))
|
||||||
.andExpect(status().isNoContent());
|
.andExpect(status().isNoContent());
|
||||||
|
|
||||||
// A new request (no session cookie, as in a fresh browser) returns no connections
|
// A new request (no session cookie, as in a fresh browser) returns no connections
|
||||||
mockMvc.perform(get(STATUS_PATH)).andExpect(status().isOk())
|
mockMvc.perform(get(STATUS_PATH))
|
||||||
.andExpect(content().json("[]"));
|
.andExpect(status().isOk())
|
||||||
}
|
.andExpect(content().json("[]"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldReturn204WhenLogoutCalledWithNoActiveSession() throws Exception {
|
void shouldReturn204WhenLogoutCalledWithNoActiveSession() throws Exception {
|
||||||
mockMvc.perform(delete(LOGOUT_PATH)).andExpect(status().isNoContent());
|
mockMvc.perform(delete(LOGOUT_PATH))
|
||||||
}
|
.andExpect(status().isNoContent());
|
||||||
|
}
|
||||||
|
|
||||||
private String connectionRequestBody(WireMockRuntimeInfo wm) {
|
private String connectionRequestBody(WireMockRuntimeInfo wm) {
|
||||||
return """
|
return """
|
||||||
{
|
{
|
||||||
"appUrl": "%s",
|
"appUrl": "%s",
|
||||||
"serviceType": "HOMEBOX",
|
"serviceType": "HOMEBOX",
|
||||||
"username": "%s",
|
"username": "%s",
|
||||||
"password": "%s"
|
"password": "%s"
|
||||||
}
|
}
|
||||||
""".formatted(wm.getHttpBaseUrl(), MOCK_USER, MOCK_PASS);
|
""".formatted(wm.getHttpBaseUrl(), TEST_USER, TEST_PASS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package com.vaessl.app.connection;
|
package com.vaessl.app.connection;
|
||||||
|
|
||||||
import static com.vaessl.app.Mockdata.*;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.Mockito.doThrow;
|
import static org.mockito.Mockito.doThrow;
|
||||||
@@ -16,8 +15,11 @@ import org.junit.jupiter.api.extension.ExtendWith;
|
|||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.junit.jupiter.MockitoExtension;
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
|
||||||
|
import com.vaessl.app.dto.ConnectionRequest;
|
||||||
import com.vaessl.app.exception.EmptyCredentialsException;
|
import com.vaessl.app.exception.EmptyCredentialsException;
|
||||||
|
|
||||||
|
import static com.vaessl.app.connection.Mockdata.*;
|
||||||
|
|
||||||
@ExtendWith(MockitoExtension.class)
|
@ExtendWith(MockitoExtension.class)
|
||||||
class ConnectionServiceTest {
|
class ConnectionServiceTest {
|
||||||
|
|
||||||
@@ -36,11 +38,10 @@ class ConnectionServiceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void login_ShouldAbort_WhenCheckCredentialsThrowsException() {
|
void login_ShouldAbort_WhenCheckCredentialsThrowsException() {
|
||||||
ConnectionRequest request =
|
ConnectionRequest request = new ConnectionRequest(MOCK_URL, MOCK_SERVICE_TYPE, null, null, false);
|
||||||
new ConnectionRequest(MOCK_URL, MOCK_SERVICE_TYPE, null, null, false);
|
|
||||||
|
|
||||||
doThrow(new EmptyCredentialsException(List.of("username"))).when(mockProvider)
|
doThrow(new EmptyCredentialsException(List.of("username")))
|
||||||
.checkCredentials(request);
|
.when(mockProvider).checkCredentials(request);
|
||||||
|
|
||||||
assertThrows(EmptyCredentialsException.class, () -> connectionService.login(request));
|
assertThrows(EmptyCredentialsException.class, () -> connectionService.login(request));
|
||||||
|
|
||||||
|
|||||||
+5
-3
@@ -4,13 +4,15 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
|||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import com.vaessl.app.dto.ConnectionRequest;
|
||||||
import com.vaessl.app.exception.EmptyCredentialsException;
|
import com.vaessl.app.exception.EmptyCredentialsException;
|
||||||
import static com.vaessl.app.Mockdata.*;
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static com.vaessl.app.connection.Mockdata.*;
|
||||||
|
|
||||||
class HomeboxConnectionProviderTest {
|
class HomeBoxConnectionProviderTest {
|
||||||
|
|
||||||
private final HomeboxConnectionProvider provider = new HomeboxConnectionProvider(null, null);
|
private final HomeBoxConnectionProvider provider = new HomeBoxConnectionProvider(null, null);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void checkCredentials_ShouldThrowException_WhenFieldsAreMissing() {
|
void checkCredentials_ShouldThrowException_WhenFieldsAreMissing() {
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
package com.vaessl.app.connection;
|
package com.vaessl.app.connection;
|
||||||
|
|
||||||
import static com.vaessl.app.Mockdata.*;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.resttestclient.TestRestTemplate;
|
import org.springframework.boot.resttestclient.TestRestTemplate;
|
||||||
@@ -15,6 +14,7 @@ import com.github.tomakehurst.wiremock.junit5.WireMockTest;
|
|||||||
|
|
||||||
import com.jayway.jsonpath.DocumentContext;
|
import com.jayway.jsonpath.DocumentContext;
|
||||||
import com.jayway.jsonpath.JsonPath;
|
import com.jayway.jsonpath.JsonPath;
|
||||||
|
import com.vaessl.app.dto.ConnectionRequest;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
@@ -42,6 +42,9 @@ class HomeboxIntegrationTest {
|
|||||||
}
|
}
|
||||||
""";
|
""";
|
||||||
|
|
||||||
|
private static final String TEST_USER = "admin";
|
||||||
|
private static final String TEST_PASS = "pw";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns Token and status code OK when login is successful.
|
* Returns Token and status code OK when login is successful.
|
||||||
*
|
*
|
||||||
@@ -50,10 +53,11 @@ class HomeboxIntegrationTest {
|
|||||||
@Test
|
@Test
|
||||||
void shouldReturnStatusOkWhenHomeboxCredentialsAreValid(WireMockRuntimeInfo wm) {
|
void shouldReturnStatusOkWhenHomeboxCredentialsAreValid(WireMockRuntimeInfo wm) {
|
||||||
|
|
||||||
stubFor(post(HOMEBOX_LOGIN.getValue()).willReturn(okJson(okJsonHomeboxResponse)));
|
stubFor(post(HOMEBOX_LOGIN.getValue())
|
||||||
|
.willReturn(okJson(okJsonHomeboxResponse)));
|
||||||
|
|
||||||
ResponseEntity<String> response =
|
ResponseEntity<String> response = restTemplate.postForEntity(LOGIN.getValue(), connectionRequest(wm),
|
||||||
restTemplate.postForEntity(LOGIN.getValue(), connectionRequest(wm), String.class);
|
String.class);
|
||||||
|
|
||||||
DocumentContext documentContext = JsonPath.parse(response.getBody());
|
DocumentContext documentContext = JsonPath.parse(response.getBody());
|
||||||
|
|
||||||
@@ -75,8 +79,8 @@ class HomeboxIntegrationTest {
|
|||||||
|
|
||||||
stubFor(post(HOMEBOX_LOGIN.getValue()).willReturn(unauthorized()));
|
stubFor(post(HOMEBOX_LOGIN.getValue()).willReturn(unauthorized()));
|
||||||
|
|
||||||
ResponseEntity<String> response =
|
ResponseEntity<String> response = restTemplate.postForEntity(LOGIN.getValue(), connectionRequest(wm),
|
||||||
restTemplate.postForEntity(LOGIN.getValue(), connectionRequest(wm), String.class);
|
String.class);
|
||||||
|
|
||||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
|
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
|
||||||
assertThat(response.getBody()).contains(UNAUTHORIZED_WRONG_LOGIN.getMessage());
|
assertThat(response.getBody()).contains(UNAUTHORIZED_WRONG_LOGIN.getMessage());
|
||||||
@@ -92,8 +96,8 @@ class HomeboxIntegrationTest {
|
|||||||
|
|
||||||
stubFor(post(HOMEBOX_LOGIN.getValue()).willReturn(serviceUnavailable()));
|
stubFor(post(HOMEBOX_LOGIN.getValue()).willReturn(serviceUnavailable()));
|
||||||
|
|
||||||
ResponseEntity<String> response =
|
ResponseEntity<String> response = restTemplate.postForEntity(LOGIN.getValue(), connectionRequest(wm),
|
||||||
restTemplate.postForEntity(LOGIN.getValue(), connectionRequest(wm), String.class);
|
String.class);
|
||||||
|
|
||||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.SERVICE_UNAVAILABLE);
|
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.SERVICE_UNAVAILABLE);
|
||||||
assertThat(response.getBody()).contains(SERVER_ERROR_GENERAL.getMessage());
|
assertThat(response.getBody()).contains(SERVER_ERROR_GENERAL.getMessage());
|
||||||
@@ -109,8 +113,7 @@ class HomeboxIntegrationTest {
|
|||||||
.willReturn(aResponse().withFault(Fault.CONNECTION_RESET_BY_PEER)));
|
.willReturn(aResponse().withFault(Fault.CONNECTION_RESET_BY_PEER)));
|
||||||
|
|
||||||
ConnectionRequest badRequest = connectionRequest(wm);
|
ConnectionRequest badRequest = connectionRequest(wm);
|
||||||
ResponseEntity<String> response =
|
ResponseEntity<String> response = restTemplate.postForEntity(LOGIN.getValue(), badRequest, String.class);
|
||||||
restTemplate.postForEntity(LOGIN.getValue(), badRequest, String.class);
|
|
||||||
|
|
||||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.SERVICE_UNAVAILABLE);
|
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.SERVICE_UNAVAILABLE);
|
||||||
assertThat(response.getBody()).contains(SERVICE_UNAVAILABLE_UNREACHABLE_URL.getMessage());
|
assertThat(response.getBody()).contains(SERVICE_UNAVAILABLE_UNREACHABLE_URL.getMessage());
|
||||||
@@ -124,30 +127,34 @@ class HomeboxIntegrationTest {
|
|||||||
|
|
||||||
ConnectionRequest emtpyRequest = new ConnectionRequest("", "", "", "", false);
|
ConnectionRequest emtpyRequest = new ConnectionRequest("", "", "", "", false);
|
||||||
|
|
||||||
ResponseEntity<String> response =
|
ResponseEntity<String> response = restTemplate.postForEntity(LOGIN.getValue(), emtpyRequest, String.class);
|
||||||
restTemplate.postForEntity(LOGIN.getValue(), emtpyRequest, String.class);
|
|
||||||
|
|
||||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
|
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
|
||||||
assertThat(response.getBody()).contains(BAD_REQUEST_EMPTY_FIELDS.getMessage());
|
assertThat(response.getBody()).contains(BAD_REQUEST_EMPTY_FIELDS.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test the exception when there is an input for serviceType but it's unsupported.
|
* Test the exception when there is an input for serviceType but it's
|
||||||
|
* unsupported.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
void shouldReturnWrongServiceTypeException(WireMockRuntimeInfo wm) {
|
void shouldReturnWrongServiceTypeException(WireMockRuntimeInfo wm) {
|
||||||
ConnectionRequest wrongServiceTypeReq = new ConnectionRequest(wm.getHttpBaseUrl(),
|
ConnectionRequest wrongServiceTypeReq = new ConnectionRequest(
|
||||||
"wrong-service-type", MOCK_USER, MOCK_PASS, false);
|
wm.getHttpBaseUrl(),
|
||||||
|
"wrong-service-type",
|
||||||
|
TEST_USER, TEST_PASS,
|
||||||
|
false);
|
||||||
|
|
||||||
ResponseEntity<String> response =
|
ResponseEntity<String> response = restTemplate.postForEntity(LOGIN.getValue(), wrongServiceTypeReq,
|
||||||
restTemplate.postForEntity(LOGIN.getValue(), wrongServiceTypeReq, String.class);
|
String.class);
|
||||||
|
|
||||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
|
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
|
||||||
assertThat(response.getBody()).contains(WRONG_SERVICE_TYPE.getMessage());
|
assertThat(response.getBody()).contains(WRONG_SERVICE_TYPE.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests the succesfull persistance of Homebox credential response to the database.
|
* Tests the succesfull persistance of Homebox credential response to the
|
||||||
|
* database.
|
||||||
*
|
*
|
||||||
* @param wm the WiremockRuntimeInfo object
|
* @param wm the WiremockRuntimeInfo object
|
||||||
*/
|
*/
|
||||||
@@ -159,12 +166,10 @@ class HomeboxIntegrationTest {
|
|||||||
|
|
||||||
ConnectionRequest request = connectionRequest(wm);
|
ConnectionRequest request = connectionRequest(wm);
|
||||||
|
|
||||||
ResponseEntity<String> response =
|
ResponseEntity<String> response = restTemplate.postForEntity(LOGIN.getValue(), request, String.class);
|
||||||
restTemplate.postForEntity(LOGIN.getValue(), request, String.class);
|
|
||||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||||
|
|
||||||
ConnectionEntity dbEntry =
|
ConnectionEntity dbEntry = cRepository.findByAppUrlAndUsername(request.appUrl(), request.username());
|
||||||
cRepository.findByAppUrlAndUsername(request.appUrl(), request.username());
|
|
||||||
|
|
||||||
assertThat(dbEntry).isNotNull();
|
assertThat(dbEntry).isNotNull();
|
||||||
assertThat(dbEntry.getAppUrl()).isEqualTo(request.appUrl());
|
assertThat(dbEntry.getAppUrl()).isEqualTo(request.appUrl());
|
||||||
@@ -179,24 +184,26 @@ class HomeboxIntegrationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldReturnEmptyCredentialsExceptionWhenCredsAreMissing(WireMockRuntimeInfo wm) {
|
void shouldReturnEmptyCredentialsExceptionWhenCredsAreMissing(WireMockRuntimeInfo wm) {
|
||||||
ConnectionRequest missingCredentials = new ConnectionRequest(wm.getHttpBaseUrl(),
|
ConnectionRequest missingCredentials = new ConnectionRequest(wm.getHttpBaseUrl(), HOMEBOX.getValue(), TEST_USER,
|
||||||
HOMEBOX.getValue(), MOCK_USER, null, false);
|
null,
|
||||||
|
false);
|
||||||
|
|
||||||
ResponseEntity<String> response =
|
ResponseEntity<String> response = restTemplate.postForEntity(LOGIN.getValue(), missingCredentials,
|
||||||
restTemplate.postForEntity(LOGIN.getValue(), missingCredentials, String.class);
|
String.class);
|
||||||
|
|
||||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
|
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
|
||||||
assertThat(response.getBody()).contains(BAD_REQUEST_EMPTY_FIELDS.getMessage());
|
assertThat(response.getBody()).contains(BAD_REQUEST_EMPTY_FIELDS.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a valid connection request with a mock Api through WireMockRuntimeInfo.
|
* Creates a valid connection request with a mock Api through
|
||||||
|
* WireMockRuntimeInfo.
|
||||||
*
|
*
|
||||||
* @param wm the WiremockRuntimeInfo object
|
* @param wm the WiremockRuntimeInfo object
|
||||||
* @return a mock api connection request.
|
* @return a mock api connection request.
|
||||||
*/
|
*/
|
||||||
private ConnectionRequest connectionRequest(WireMockRuntimeInfo wm) {
|
private ConnectionRequest connectionRequest(WireMockRuntimeInfo wm) {
|
||||||
return new ConnectionRequest(wm.getHttpBaseUrl(), HOMEBOX.getValue(), MOCK_USER, MOCK_PASS,
|
return new ConnectionRequest(wm.getHttpBaseUrl(), HOMEBOX.getValue(), TEST_USER, TEST_PASS,
|
||||||
null);
|
null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.vaessl.app.connection;
|
||||||
|
|
||||||
|
public final class Mockdata {
|
||||||
|
|
||||||
|
private Mockdata() {}
|
||||||
|
|
||||||
|
public static final String MOCK_URL = "http://localhost:1234";
|
||||||
|
public static final String MOCK_SERVICE_TYPE = "SERVICE_TYPE";
|
||||||
|
}
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
package com.vaessl.app.search;
|
|
||||||
|
|
||||||
import static com.vaessl.app.Mockdata.*;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
|
||||||
import org.mockito.InjectMocks;
|
|
||||||
import org.mockito.Mock;
|
|
||||||
import org.mockito.junit.jupiter.MockitoExtension;
|
|
||||||
import org.springframework.data.domain.PageRequest;
|
|
||||||
import org.springframework.data.domain.Pageable;
|
|
||||||
import com.vaessl.app.connection.ConnectionRepository;
|
|
||||||
import com.vaessl.app.exception.ConnectionNotFoundException;
|
|
||||||
|
|
||||||
@ExtendWith(MockitoExtension.class)
|
|
||||||
class HomeboxSearchProviderTest {
|
|
||||||
|
|
||||||
@Mock
|
|
||||||
private ConnectionRepository mockRepo;
|
|
||||||
|
|
||||||
@InjectMocks
|
|
||||||
private HomeboxSearchProvider provider;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void shouldReturnConnectionNotFoundException() {
|
|
||||||
|
|
||||||
when(mockRepo.findByAppUrlAndUsername(MOCK_URL, MOCK_USER)).thenReturn(null);
|
|
||||||
|
|
||||||
SearchRequest request = new SearchRequest(MOCK_URL, MOCK_USER, "test query", "HOMEBOX");
|
|
||||||
Pageable pageable = PageRequest.of(0, 10);
|
|
||||||
assertThrows(ConnectionNotFoundException.class,
|
|
||||||
() -> provider.getSearchResults(request, pageable));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,152 +0,0 @@
|
|||||||
package com.vaessl.app.search;
|
|
||||||
|
|
||||||
import static com.vaessl.app.Mockdata.MOCK_PASS;
|
|
||||||
import static com.vaessl.app.Mockdata.MOCK_USER;
|
|
||||||
import static com.vaessl.app.connection.Endpoint.*;
|
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
|
|
||||||
import org.springframework.http.MediaType;
|
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
|
||||||
import org.springframework.test.web.servlet.MvcResult;
|
|
||||||
import com.github.tomakehurst.wiremock.client.WireMock;
|
|
||||||
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
|
|
||||||
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
|
|
||||||
import jakarta.servlet.http.Cookie;
|
|
||||||
|
|
||||||
@SpringBootTest
|
|
||||||
@AutoConfigureMockMvc
|
|
||||||
@WireMockTest
|
|
||||||
class SearchControllerTest {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
MockMvc mockMvc;
|
|
||||||
|
|
||||||
private static final String QUERY_ALL_ITEMS = HOMEBOX_QUERY_ALL_ITEMS.getValue();
|
|
||||||
|
|
||||||
private static final String LOGIN_PATH = LOGIN.getValue();
|
|
||||||
|
|
||||||
private static final String SEARCH_REQUEST = SEARCH.getValue();
|
|
||||||
|
|
||||||
private static final String VALID_HOMEBOX_LOGIN_RESPONSE = """
|
|
||||||
{
|
|
||||||
"token": "fake-bearer-token",
|
|
||||||
"attachmentToken": "fake-attach-token",
|
|
||||||
"expiresAt": "2099-01-01T00:00:00Z"
|
|
||||||
}
|
|
||||||
""";
|
|
||||||
|
|
||||||
private static final String VALID_HOMEBOX_ALL_ITEMS_QUERY_RESPONSE = """
|
|
||||||
{
|
|
||||||
"page": -1,
|
|
||||||
"pageSize": -1,
|
|
||||||
"total": 1,
|
|
||||||
"items": [
|
|
||||||
{
|
|
||||||
"id": "c643e7f9-93d0-4b5f-ae4d-e1c2d90389e0",
|
|
||||||
"assetId": "000-001",
|
|
||||||
"name": "MacBook Pro A1398",
|
|
||||||
"description": "Running Linux (Fedora)",
|
|
||||||
"quantity": 1,
|
|
||||||
"insured": false,
|
|
||||||
"archived": false,
|
|
||||||
"createdAt": "2026-05-13T19:52:20.016176Z",
|
|
||||||
"updatedAt": "2026-05-14T12:39:11.836403Z",
|
|
||||||
"purchasePrice": 0,
|
|
||||||
"location": {
|
|
||||||
"id": "b6f60ab8-3a2a-4a8d-a4bf-897d0555f636",
|
|
||||||
"name": "Server Schrank Ikea weiß",
|
|
||||||
"description": "Weißer Ikea Schrank, wo sich der Server befindet.",
|
|
||||||
"createdAt": "2026-05-13T19:55:55.817576Z",
|
|
||||||
"updatedAt": "2026-05-14T12:37:24.396651Z"
|
|
||||||
},
|
|
||||||
"tags": [],
|
|
||||||
"imageId": "cb3e44d5-ccd4-421e-9f5a-f52cd5f40ca6",
|
|
||||||
"thumbnailId": "2bfd53fa-1bf1-483c-8d76-7720464532fa",
|
|
||||||
"soldTime": "0001-01-01T00:00:00Z"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
""";
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void shouldReturnListOfQueriedHomeboxItems(WireMockRuntimeInfo wm) throws Exception {
|
|
||||||
|
|
||||||
WireMock.stubFor(WireMock.post(HOMEBOX_LOGIN.getValue())
|
|
||||||
.willReturn(WireMock.okJson(VALID_HOMEBOX_LOGIN_RESPONSE)));
|
|
||||||
|
|
||||||
WireMock.stubFor(WireMock.get(WireMock.urlPathEqualTo(QUERY_ALL_ITEMS))
|
|
||||||
.willReturn(WireMock.okJson(VALID_HOMEBOX_ALL_ITEMS_QUERY_RESPONSE)));
|
|
||||||
|
|
||||||
MvcResult loginResult =
|
|
||||||
mockMvc.perform(post(LOGIN_PATH).contentType(MediaType.APPLICATION_JSON)
|
|
||||||
.content(connectionRequestBody(wm))).andExpect(status().isOk()).andReturn();
|
|
||||||
|
|
||||||
Cookie sessionCookie = loginResult.getResponse().getCookie("SESSION");
|
|
||||||
|
|
||||||
mockMvc.perform(post(SEARCH_REQUEST).cookie(sessionCookie)
|
|
||||||
.contentType(MediaType.APPLICATION_JSON).content(searchRequestBody(wm, "HOMEBOX")))
|
|
||||||
.andExpect(status().isOk())
|
|
||||||
.andExpect(jsonPath("$.content[0].title").value("MacBook Pro A1398"))
|
|
||||||
.andExpect(jsonPath("$.totalElements").value(1))
|
|
||||||
.andExpect(jsonPath("$.content[0].extraData.location.name")
|
|
||||||
.value("Server Schrank Ikea weiß"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void shouldReturnUnauthorizedWhenNoSession() throws Exception {
|
|
||||||
mockMvc.perform(post(SEARCH_REQUEST).contentType(MediaType.APPLICATION_JSON).content("""
|
|
||||||
{
|
|
||||||
"appUrl": "http://irrelevant",
|
|
||||||
"query": "Item",
|
|
||||||
"serviceType": "HOMEBOX",
|
|
||||||
"username": "irrelevant"
|
|
||||||
}
|
|
||||||
""")).andExpect(status().isUnauthorized());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void shouldReturnUnauthorizedWhenSessionHasNoConnectionForRequestedServiceType(
|
|
||||||
WireMockRuntimeInfo wm) throws Exception {
|
|
||||||
|
|
||||||
WireMock.stubFor(WireMock.post(HOMEBOX_LOGIN.getValue())
|
|
||||||
.willReturn(WireMock.okJson(VALID_HOMEBOX_LOGIN_RESPONSE)));
|
|
||||||
|
|
||||||
MvcResult loginResult =
|
|
||||||
mockMvc.perform(post(LOGIN_PATH).contentType(MediaType.APPLICATION_JSON)
|
|
||||||
.content(connectionRequestBody(wm))).andExpect(status().isOk()).andReturn();
|
|
||||||
|
|
||||||
Cookie sessionCookie = loginResult.getResponse().getCookie("SESSION");
|
|
||||||
|
|
||||||
mockMvc.perform(
|
|
||||||
post(SEARCH_REQUEST).cookie(sessionCookie).contentType(MediaType.APPLICATION_JSON)
|
|
||||||
.content(searchRequestBody(wm, "OTHER_SERVICETYPE")))
|
|
||||||
.andExpect(status().isUnauthorized());
|
|
||||||
}
|
|
||||||
|
|
||||||
private String searchRequestBody(WireMockRuntimeInfo wm, String serviceType) {
|
|
||||||
return """
|
|
||||||
{
|
|
||||||
"appUrl": "%s",
|
|
||||||
"query": "Item",
|
|
||||||
"serviceType": "%s",
|
|
||||||
"username": "%s"
|
|
||||||
}
|
|
||||||
""".formatted(wm.getHttpBaseUrl(), serviceType, MOCK_USER);
|
|
||||||
}
|
|
||||||
|
|
||||||
private String connectionRequestBody(WireMockRuntimeInfo wm) {
|
|
||||||
return """
|
|
||||||
{
|
|
||||||
"appUrl": "%s",
|
|
||||||
"serviceType": "HOMEBOX",
|
|
||||||
"username": "%s",
|
|
||||||
"password": "%s"
|
|
||||||
}
|
|
||||||
""".formatted(wm.getHttpBaseUrl(), MOCK_USER, MOCK_PASS);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
package com.vaessl.app.search;
|
|
||||||
|
|
||||||
import static com.vaessl.app.Mockdata.*;
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
import java.util.Map;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
class SearchResponseTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void shouldReturnNullWhenExtraDataIsNull() {
|
|
||||||
SearchResponse response = new SearchResponse(MOCK_TITLE, MOCK_DESCRIPTION, null);
|
|
||||||
|
|
||||||
assertThat(response.getExtra(null)).isNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void shouldReturnNullWhenExtraDataKeyIsMissing() {
|
|
||||||
|
|
||||||
SearchResponse response = new SearchResponse(MOCK_TITLE, MOCK_DESCRIPTION, Map.of("key", "value"));
|
|
||||||
|
|
||||||
assertThat(response.getExtra("missing")).isNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void shouldReturnExtraDataValue() {
|
|
||||||
|
|
||||||
SearchResponse response = new SearchResponse(MOCK_TITLE, MOCK_DESCRIPTION, Map.of("key", "value"));
|
|
||||||
|
|
||||||
assertThat(response.getExtra("key")).contains("value");
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
spring:
|
spring:
|
||||||
datasource:
|
datasource:
|
||||||
url : ${DB_TEST_URL}
|
url: ${DB_TEST_URL}
|
||||||
username: ${DB_USERNAME}
|
username: ${DB_USERNAME}
|
||||||
password: ${DB_PASSWORD}
|
password: ${DB_PASSWORD}
|
||||||
driver-class-name: ${PG_DRIVER_CLASS_NAME}
|
driver-class-name: ${PG_DRIVER_CLASS_NAME}
|
||||||
jpa:
|
jpa:
|
||||||
hibernate:
|
hibernate:
|
||||||
ddl-auto: create-drop
|
ddl-auto: update
|
||||||
|
|||||||
@@ -138,7 +138,7 @@ spring:
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Note that I'm using my own locally hosted PostgreSQL instances for the main and test database. The Docker Compose file will look something like this:
|
The Docker Compose file for code-server will look something like this:
|
||||||
|
|
||||||
```
|
```
|
||||||
---
|
---
|
||||||
@@ -163,26 +163,43 @@ services:
|
|||||||
- 8124:8080
|
- 8124:8080
|
||||||
- 5173:5173
|
- 5173:5173
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
```
|
||||||
|
|
||||||
vaessl-db:
|
Note that I'm using my own locally hosted PostgreSQL instances for the main and test database. Just add databases via SQL or PgAdmin and install the pgvector extension to each database manually. There is an offical ready-made pgvector docker image but if you already host a PostGreSQL database you need to add the extension yourself.
|
||||||
image: pgvector/pgvector:pg18
|
|
||||||
container_name: vaessl-db
|
|
||||||
environment:
|
|
||||||
- POSTGRES_DB=vaessl
|
|
||||||
- POSTGRES_USER=user
|
|
||||||
- POSTGRES_PASSWORD=pw
|
|
||||||
ports:
|
|
||||||
- 5433:5432
|
|
||||||
|
|
||||||
vassal-test-db:
|
Check the name of your PostGreSQL container:
|
||||||
image: pgvector/pgvector:pg18
|
```
|
||||||
container_name: vassal-test-db
|
docker ps
|
||||||
environment:
|
```
|
||||||
- POSTGRES_DB=vassal_test
|
|
||||||
- POSTGRES_USER=user
|
Enter your container via bash:
|
||||||
- POSTGRES_PASSWORD=pw
|
|
||||||
ports:
|
```
|
||||||
- 5434:5432
|
docker exec -it 876fb382969f bash
|
||||||
|
```
|
||||||
|
Before working on your database backup your databases:
|
||||||
|
```
|
||||||
|
su - postgres -c "pg_dumpall > /tmp/backup200526.sql"
|
||||||
|
|
||||||
|
#exit the container and copy the backup file to local file system
|
||||||
|
docker cp 876fb382969f:/tmp/backup200526.sql .
|
||||||
|
```
|
||||||
|
|
||||||
|
Install dependencies, build and install pgvector:
|
||||||
|
apt-get update
|
||||||
|
apt-get install -y build-essential git postgresql-server-dev-all
|
||||||
|
```
|
||||||
|
git clone https://github.com/pgvector/pgvector.git
|
||||||
|
cd pgvector
|
||||||
|
make
|
||||||
|
make install
|
||||||
|
docker restart 876fb382969f
|
||||||
|
```
|
||||||
|
Enter PostGreSQL container and create pgvector extension for each databse:
|
||||||
|
```
|
||||||
|
docker exec -it <container-name> psql -h localhost -U <db-user> -d <db-name>
|
||||||
|
|
||||||
|
CREATE EXTENSION vector;
|
||||||
```
|
```
|
||||||
|
|
||||||
# Appendix: Additional config for developing in Code-Server
|
# Appendix: Additional config for developing in Code-Server
|
||||||
|
|||||||
@@ -94,7 +94,7 @@ The entire commit can be reviewed under following hash 43bbcece7a901e94021e10bca
|
|||||||
- DELETE /connections/{serviceType} — removes the specific key from the session. If no connections remain, the session is invalidated entirely.
|
- DELETE /connections/{serviceType} — removes the specific key from the session. If no connections remain, the session is invalidated entirely.
|
||||||
|
|
||||||
---
|
---
|
||||||
**connection/HomeboxConnectionProvider.java (modified)**
|
**connection/HomeBoxConnectionProvider.java (modified)**
|
||||||
|
|
||||||
Implements the new interface methods:
|
Implements the new interface methods:
|
||||||
- checkCredentials validates that username and password are present before touching the network.
|
- checkCredentials validates that username and password are present before touching the network.
|
||||||
|
|||||||
@@ -94,11 +94,11 @@ public interface ConnectionProvider {
|
|||||||
- `findUniqueConnectionEntry` — looks up an existing record to decide insert vs. update
|
- `findUniqueConnectionEntry` — looks up an existing record to decide insert vs. update
|
||||||
- `getTokenExpiry` — default returns `null` (no expiry); token-based providers override this so `ConnectionService` can compute the `connected` flag
|
- `getTokenExpiry` — default returns `null` (no expiry); token-based providers override this so `ConnectionService` can compute the `connected` flag
|
||||||
|
|
||||||
***HomeboxConnectionProvider.java***
|
***HomeBoxConnectionProvider.java***
|
||||||
|
|
||||||
```java
|
```java
|
||||||
@Component
|
@Component
|
||||||
public class HomeboxConnectionProvider implements ConnectionProvider {
|
public class HomeBoxConnectionProvider implements ConnectionProvider {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getServiceType() { return "HOMEBOX"; }
|
public String getServiceType() { return "HOMEBOX"; }
|
||||||
|
|||||||
Reference in New Issue
Block a user