Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 042c706407 | |||
| 5823d2b6a0 | |||
| 3fcea268a9 | |||
| 3c51b761e0 | |||
| c78f2713c3 |
@ -4,11 +4,21 @@ import com.hithomelabs.CFTunnels.Entity.Request;
|
|||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface RequestRepository extends JpaRepository<Request, UUID> {
|
public interface RequestRepository extends JpaRepository<Request, UUID> {
|
||||||
Page<Request> findByStatus(Request.RequestStatus status, Pageable pageable);
|
Page<Request> findByStatus(Request.RequestStatus status, Pageable pageable);
|
||||||
|
|
||||||
|
@Query("SELECT r FROM Request r JOIN FETCH r.mapping m JOIN FETCH m.tunnel JOIN FETCH r.createdBy LEFT JOIN FETCH r.acceptedBy")
|
||||||
|
List<Request> findAllWithDetails();
|
||||||
|
|
||||||
|
@Query("SELECT r FROM Request r JOIN FETCH r.mapping m JOIN FETCH m.tunnel JOIN FETCH r.createdBy LEFT JOIN FETCH r.acceptedBy WHERE r.id = :id")
|
||||||
|
Optional<Request> findByIdWithDetails(@Param("id") UUID id);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -59,8 +59,9 @@ public class MappingRequestService {
|
|||||||
return createRequest(mapping, user);
|
return createRequest(mapping, user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
public List<Request> getAllRequests() {
|
public List<Request> getAllRequests() {
|
||||||
return requestRepository.findAll();
|
return requestRepository.findAllWithDetails();
|
||||||
}
|
}
|
||||||
|
|
||||||
public User mapUser(OidcUser oidcUser){
|
public User mapUser(OidcUser oidcUser){
|
||||||
@ -99,7 +100,7 @@ public class MappingRequestService {
|
|||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Request approveRequest(UUID requestId, User approver) {
|
public Request approveRequest(UUID requestId, User approver) {
|
||||||
Request request = requestRepository.findById(requestId)
|
Request request = requestRepository.findByIdWithDetails(requestId)
|
||||||
.orElseThrow(() -> new NoSuchElementException("Request not found"));
|
.orElseThrow(() -> new NoSuchElementException("Request not found"));
|
||||||
|
|
||||||
if (request.getStatus() != Request.RequestStatus.PENDING) {
|
if (request.getStatus() != Request.RequestStatus.PENDING) {
|
||||||
@ -127,7 +128,7 @@ public class MappingRequestService {
|
|||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Request rejectRequest(UUID requestId, User rejecter) {
|
public Request rejectRequest(UUID requestId, User rejecter) {
|
||||||
Request request = requestRepository.findById(requestId)
|
Request request = requestRepository.findByIdWithDetails(requestId)
|
||||||
.orElseThrow(() -> new NoSuchElementException("Request not found"));
|
.orElseThrow(() -> new NoSuchElementException("Request not found"));
|
||||||
|
|
||||||
if (request.getStatus() != Request.RequestStatus.PENDING) {
|
if (request.getStatus() != Request.RequestStatus.PENDING) {
|
||||||
@ -145,7 +146,7 @@ public class MappingRequestService {
|
|||||||
Tunnel tunnel = mapping.getTunnel();
|
Tunnel tunnel = mapping.getTunnel();
|
||||||
String protocol = mapping.getProtocol().name().toLowerCase();
|
String protocol = mapping.getProtocol().name().toLowerCase();
|
||||||
String service = protocol + "://" + SERVER_IP + ":" + mapping.getPort();
|
String service = protocol + "://" + SERVER_IP + ":" + mapping.getPort();
|
||||||
String hostname = mapping.getSubdomain() + "." + tunnel.getName() + ".hithomelabs.com";
|
String hostname = mapping.getSubdomain() + ".hithomelabs.com";
|
||||||
return new Ingress(service, hostname, null, null);
|
return new Ingress(service, hostname, null, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,6 +7,6 @@ management.endpoint.health.show-details=always
|
|||||||
logging.level.org.hibernate.SQL=DEBUG
|
logging.level.org.hibernate.SQL=DEBUG
|
||||||
debug=true
|
debug=true
|
||||||
|
|
||||||
spring.jpa.hibernate.ddl-auto=create-drop
|
spring.jpa.hibernate.ddl-auto=update
|
||||||
spring.jpa.show-sql=true
|
spring.jpa.show-sql=true
|
||||||
spring.datasource.url=jdbc:postgresql://localhost:5432/cftunnel
|
spring.datasource.url=jdbc:postgresql://localhost:5432/cftunnel
|
||||||
|
|||||||
@ -8,5 +8,5 @@ spring.datasource.driver-class-name=org.postgresql.Driver
|
|||||||
|
|
||||||
# JPA Configuration
|
# JPA Configuration
|
||||||
spring.jpa.hibernate.ddl-auto=create-drop
|
spring.jpa.hibernate.ddl-auto=create-drop
|
||||||
spring.jpa.show-sql=true
|
spring.jpa.show-sql=false
|
||||||
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
|
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
|
||||||
@ -7,6 +7,6 @@ spring.datasource.password=${POSTGRES_PASSWORD}
|
|||||||
spring.datasource.driver-class-name=org.postgresql.Driver
|
spring.datasource.driver-class-name=org.postgresql.Driver
|
||||||
|
|
||||||
# JPA Configuration
|
# JPA Configuration
|
||||||
spring.jpa.hibernate.ddl-auto=create-drop
|
spring.jpa.hibernate.ddl-auto=update
|
||||||
spring.jpa.show-sql=true
|
spring.jpa.show-sql=true
|
||||||
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
|
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
|
||||||
|
|||||||
@ -0,0 +1,179 @@
|
|||||||
|
package com.hithomelabs.CFTunnels.Repositories;
|
||||||
|
|
||||||
|
import com.hithomelabs.CFTunnels.Entity.Mapping;
|
||||||
|
import com.hithomelabs.CFTunnels.Entity.Protocol;
|
||||||
|
import com.hithomelabs.CFTunnels.Entity.Request;
|
||||||
|
import com.hithomelabs.CFTunnels.Entity.Tunnel;
|
||||||
|
import com.hithomelabs.CFTunnels.Entity.User;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||||
|
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
|
||||||
|
import org.springframework.test.annotation.DirtiesContext;
|
||||||
|
import org.springframework.test.context.TestPropertySource;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
@DataJpaTest
|
||||||
|
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.ANY)
|
||||||
|
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
|
||||||
|
@TestPropertySource(properties = {
|
||||||
|
"spring.jpa.hibernate.ddl-auto=create-drop",
|
||||||
|
"spring.jpa.show-sql=false",
|
||||||
|
"spring.sql.init.mode=never"
|
||||||
|
})
|
||||||
|
class RequestRepositoryTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RequestRepository requestRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TunnelRepository tunnelRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MappingRepository mappingRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
private Tunnel tunnel;
|
||||||
|
private User createdByUser;
|
||||||
|
private User acceptedByUser;
|
||||||
|
private Mapping mapping;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
tunnel = new Tunnel();
|
||||||
|
tunnel.setId(UUID.randomUUID());
|
||||||
|
tunnel.setEnvironment("test");
|
||||||
|
tunnel.setName("test-tunnel");
|
||||||
|
tunnel = tunnelRepository.save(tunnel);
|
||||||
|
|
||||||
|
createdByUser = new User();
|
||||||
|
createdByUser.setEmail("creator@example.com");
|
||||||
|
createdByUser.setName("Creator User");
|
||||||
|
createdByUser = userRepository.save(createdByUser);
|
||||||
|
|
||||||
|
acceptedByUser = new User();
|
||||||
|
acceptedByUser.setEmail("approver@example.com");
|
||||||
|
acceptedByUser.setName("Approver User");
|
||||||
|
acceptedByUser = userRepository.save(acceptedByUser);
|
||||||
|
|
||||||
|
mapping = new Mapping();
|
||||||
|
mapping.setTunnel(tunnel);
|
||||||
|
mapping.setPort(8080);
|
||||||
|
mapping.setProtocol(Protocol.HTTP);
|
||||||
|
mapping.setSubdomain("test-subdomain");
|
||||||
|
mapping = mappingRepository.save(mapping);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("findAllWithDetails should return requests with all relationships loaded")
|
||||||
|
void findAllWithDetails_ShouldReturnRequestsWithAllRelationships() {
|
||||||
|
Request request = new Request();
|
||||||
|
request.setMapping(mapping);
|
||||||
|
request.setCreatedBy(createdByUser);
|
||||||
|
request.setAcceptedBy(acceptedByUser);
|
||||||
|
request.setStatus(Request.RequestStatus.PENDING);
|
||||||
|
requestRepository.save(request);
|
||||||
|
|
||||||
|
List<Request> results = requestRepository.findAllWithDetails();
|
||||||
|
|
||||||
|
assertThat(results).hasSize(1);
|
||||||
|
Request result = results.get(0);
|
||||||
|
assertThat(result.getMapping()).isNotNull();
|
||||||
|
assertThat(result.getMapping().getTunnel()).isNotNull();
|
||||||
|
assertThat(result.getCreatedBy()).isNotNull();
|
||||||
|
assertThat(result.getAcceptedBy()).isNotNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("findByIdWithDetails should return request with all relationships loaded")
|
||||||
|
void findByIdWithDetails_ShouldReturnRequestWithAllRelationships() {
|
||||||
|
Request request = new Request();
|
||||||
|
request.setMapping(mapping);
|
||||||
|
request.setCreatedBy(createdByUser);
|
||||||
|
request.setAcceptedBy(acceptedByUser);
|
||||||
|
request.setStatus(Request.RequestStatus.PENDING);
|
||||||
|
UUID requestId = requestRepository.save(request).getId();
|
||||||
|
|
||||||
|
Optional<Request> result = requestRepository.findByIdWithDetails(requestId);
|
||||||
|
|
||||||
|
assertThat(result).isPresent();
|
||||||
|
assertThat(result.get().getMapping()).isNotNull();
|
||||||
|
assertThat(result.get().getMapping().getTunnel()).isNotNull();
|
||||||
|
assertThat(result.get().getCreatedBy()).isNotNull();
|
||||||
|
assertThat(result.get().getAcceptedBy()).isNotNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("findByIdWithDetails should return empty for non-existent id")
|
||||||
|
void findByIdWithDetails_ShouldReturnEmptyForNonExistentId() {
|
||||||
|
Optional<Request> result = requestRepository.findByIdWithDetails(UUID.randomUUID());
|
||||||
|
|
||||||
|
assertThat(result).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("findAllWithDetails should return empty list when no requests exist")
|
||||||
|
void findAllWithDetails_ShouldReturnEmptyListWhenNoRequests() {
|
||||||
|
List<Request> results = requestRepository.findAllWithDetails();
|
||||||
|
|
||||||
|
assertThat(results).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("findAllWithDetails should handle multiple requests with different statuses")
|
||||||
|
void findAllWithDetails_ShouldHandleMultipleRequests() {
|
||||||
|
Mapping mapping1 = new Mapping();
|
||||||
|
mapping1.setTunnel(tunnel);
|
||||||
|
mapping1.setPort(8080);
|
||||||
|
mapping1.setProtocol(Protocol.HTTP);
|
||||||
|
mapping1.setSubdomain("pending-subdomain");
|
||||||
|
mapping1 = mappingRepository.save(mapping1);
|
||||||
|
|
||||||
|
Mapping mapping2 = new Mapping();
|
||||||
|
mapping2.setTunnel(tunnel);
|
||||||
|
mapping2.setPort(8081);
|
||||||
|
mapping2.setProtocol(Protocol.HTTP);
|
||||||
|
mapping2.setSubdomain("approved-subdomain");
|
||||||
|
mapping2 = mappingRepository.save(mapping2);
|
||||||
|
|
||||||
|
Mapping mapping3 = new Mapping();
|
||||||
|
mapping3.setTunnel(tunnel);
|
||||||
|
mapping3.setPort(8082);
|
||||||
|
mapping3.setProtocol(Protocol.HTTP);
|
||||||
|
mapping3.setSubdomain("rejected-subdomain");
|
||||||
|
mapping3 = mappingRepository.save(mapping3);
|
||||||
|
|
||||||
|
Request pendingRequest = new Request();
|
||||||
|
pendingRequest.setMapping(mapping1);
|
||||||
|
pendingRequest.setCreatedBy(createdByUser);
|
||||||
|
pendingRequest.setStatus(Request.RequestStatus.PENDING);
|
||||||
|
requestRepository.save(pendingRequest);
|
||||||
|
|
||||||
|
Request approvedRequest = new Request();
|
||||||
|
approvedRequest.setMapping(mapping2);
|
||||||
|
approvedRequest.setCreatedBy(createdByUser);
|
||||||
|
approvedRequest.setAcceptedBy(acceptedByUser);
|
||||||
|
approvedRequest.setStatus(Request.RequestStatus.APPROVED);
|
||||||
|
requestRepository.save(approvedRequest);
|
||||||
|
|
||||||
|
Request rejectedRequest = new Request();
|
||||||
|
rejectedRequest.setMapping(mapping3);
|
||||||
|
rejectedRequest.setCreatedBy(createdByUser);
|
||||||
|
rejectedRequest.setAcceptedBy(acceptedByUser);
|
||||||
|
rejectedRequest.setStatus(Request.RequestStatus.REJECTED);
|
||||||
|
requestRepository.save(rejectedRequest);
|
||||||
|
|
||||||
|
List<Request> results = requestRepository.findAllWithDetails();
|
||||||
|
|
||||||
|
assertThat(results).hasSize(3);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user