From f5032c74c42784e9e2be33f1dfbd1f663ac49074 Mon Sep 17 00:00:00 2001 From: Jonas Arnhold Date: Wed, 12 Jul 2023 16:34:18 +0200 Subject: [PATCH] #207: Unify class building process Signed-off-by: Jonas Arnhold --- .../apachepulsarui/dto/ClusterDetailDto.java | 21 +++++++++++++++++-- .../dto/NamespaceDetailDto.java | 20 +++++++++++++----- .../apachepulsarui/dto/TenantDetailDto.java | 13 ++++++++++-- .../service/ClusterService.java | 18 ++++++---------- .../service/NamespaceService.java | 19 +++++++++++++++-- .../apachepulsarui/service/TenantService.java | 10 ++++----- .../controller/NamespaceControllerTest.java | 11 +++++++--- .../NamespaceServiceIntegrationTest.java | 2 +- 8 files changed, 82 insertions(+), 32 deletions(-) diff --git a/backend/src/main/java/de/amos/apachepulsarui/dto/ClusterDetailDto.java b/backend/src/main/java/de/amos/apachepulsarui/dto/ClusterDetailDto.java index a61bad31..96e21b68 100644 --- a/backend/src/main/java/de/amos/apachepulsarui/dto/ClusterDetailDto.java +++ b/backend/src/main/java/de/amos/apachepulsarui/dto/ClusterDetailDto.java @@ -6,14 +6,15 @@ package de.amos.apachepulsarui.dto; import lombok.AccessLevel; -import lombok.Builder; import lombok.Data; +import lombok.NoArgsConstructor; import lombok.Setter; +import org.apache.pulsar.common.policies.data.ClusterData; import java.util.List; @Data -@Builder +@NoArgsConstructor(access = AccessLevel.PRIVATE) public class ClusterDetailDto { private String name; @@ -24,4 +25,20 @@ public class ClusterDetailDto { private String brokerServiceUrl; private String serviceUrl; + public static ClusterDetailDto create( + String name, + List activeBrokers, + List tenants, + ClusterData clusterData + ) { + ClusterDetailDto clusterDetailDto = new ClusterDetailDto(); + clusterDetailDto.name = name; + clusterDetailDto.serviceUrl = clusterDetailDto.getServiceUrl(); + clusterDetailDto.brokerServiceUrl = clusterDetailDto.getBrokerServiceUrl(); + clusterDetailDto.brokers = activeBrokers; + clusterDetailDto.amountOfBrokers = activeBrokers.size(); + clusterDetailDto.tenants = tenants; + return clusterDetailDto; + } + } diff --git a/backend/src/main/java/de/amos/apachepulsarui/dto/NamespaceDetailDto.java b/backend/src/main/java/de/amos/apachepulsarui/dto/NamespaceDetailDto.java index c7110f74..ffec9f40 100644 --- a/backend/src/main/java/de/amos/apachepulsarui/dto/NamespaceDetailDto.java +++ b/backend/src/main/java/de/amos/apachepulsarui/dto/NamespaceDetailDto.java @@ -17,17 +17,27 @@ @NoArgsConstructor(access = AccessLevel.PRIVATE) public class NamespaceDetailDto { - private String id; + private String name; private List topics; private String tenant; private BundlesData bundlesData; private Integer messagesTTL; private RetentionPolicies retentionPolicies; - public static NamespaceDetailDto fromString(String namespaceId) { - NamespaceDetailDto namespaceDto = new NamespaceDetailDto(); - namespaceDto.setId(namespaceId); - return namespaceDto; + public static NamespaceDetailDto create( + String name, + BundlesData bundlesData, + Integer messagesTTL, + RetentionPolicies retentionPolicies, + List topics + ) { + NamespaceDetailDto namespaceDetailDto = new NamespaceDetailDto(); + namespaceDetailDto.name = name; + namespaceDetailDto.bundlesData = bundlesData; + namespaceDetailDto.messagesTTL = messagesTTL; + namespaceDetailDto.retentionPolicies = retentionPolicies; + namespaceDetailDto.topics = topics; + return namespaceDetailDto; } public void setTopics(List topics) { diff --git a/backend/src/main/java/de/amos/apachepulsarui/dto/TenantDetailDto.java b/backend/src/main/java/de/amos/apachepulsarui/dto/TenantDetailDto.java index 47670593..50247057 100644 --- a/backend/src/main/java/de/amos/apachepulsarui/dto/TenantDetailDto.java +++ b/backend/src/main/java/de/amos/apachepulsarui/dto/TenantDetailDto.java @@ -5,14 +5,15 @@ package de.amos.apachepulsarui.dto; -import lombok.Builder; +import lombok.AccessLevel; import lombok.Data; +import lombok.NoArgsConstructor; import org.apache.pulsar.common.policies.data.TenantInfo; import java.util.List; @Data -@Builder +@NoArgsConstructor(access = AccessLevel.PRIVATE) public class TenantDetailDto { private String name; @@ -23,4 +24,12 @@ public List getNamespaces() { return List.copyOf(namespaces); } + public static TenantDetailDto create(String name, TenantInfo tenantInfo, List namespaces) { + TenantDetailDto tenantDetailDto = new TenantDetailDto(); + tenantDetailDto.name = name; + tenantDetailDto.tenantInfo = tenantInfo; + tenantDetailDto.namespaces = namespaces; + return tenantDetailDto; + } + } diff --git a/backend/src/main/java/de/amos/apachepulsarui/service/ClusterService.java b/backend/src/main/java/de/amos/apachepulsarui/service/ClusterService.java index 90c87d7e..c36374e6 100644 --- a/backend/src/main/java/de/amos/apachepulsarui/service/ClusterService.java +++ b/backend/src/main/java/de/amos/apachepulsarui/service/ClusterService.java @@ -37,18 +37,12 @@ public List getAllNames() { @Cacheable("cluster.detail") public ClusterDetailDto getClusterDetails(String clusterName) { - - ClusterData clusterData = getClusterData(clusterName); - List activeBrokers = getActiveBrokers(clusterName); - List tenantsAllowedForCluster = getTenantsAllowedForCluster(clusterName); - return ClusterDetailDto.builder() - .name(clusterName) - .serviceUrl(clusterData.getServiceUrl()) - .brokerServiceUrl(clusterData.getBrokerServiceUrl()) - .brokers(activeBrokers) - .amountOfBrokers(activeBrokers.size()) - .tenants(tenantsAllowedForCluster) - .build(); + return ClusterDetailDto.create( + clusterName, + getActiveBrokers(clusterName), + getTenantsAllowedForCluster(clusterName), + getClusterData(clusterName) + ); } private ClusterData getClusterData(String clusterName) throws PulsarApiException { diff --git a/backend/src/main/java/de/amos/apachepulsarui/service/NamespaceService.java b/backend/src/main/java/de/amos/apachepulsarui/service/NamespaceService.java index 21f1d80f..4c6e2088 100644 --- a/backend/src/main/java/de/amos/apachepulsarui/service/NamespaceService.java +++ b/backend/src/main/java/de/amos/apachepulsarui/service/NamespaceService.java @@ -48,8 +48,23 @@ public List getAllForTenants(List tenants) { } @Cacheable("namespace.detail") - public NamespaceDetailDto getNamespaceDetails(String namespaceName) { - return enrichWithNamespaceData(NamespaceDetailDto.fromString(namespaceName)); + public NamespaceDetailDto getNamespaceDetails(String namespace) { + try { + + Namespaces namespaces = pulsarAdmin.namespaces(); + + return NamespaceDetailDto.create( + namespace, + namespaces.getBundles(namespace), + namespaces.getNamespaceMessageTTL(namespace), + namespaces.getRetention(namespace), + topicService.getAllByNamespace(namespace) + ); + } catch (PulsarAdminException e) { + throw new PulsarApiException( + "Could not fetch namespace data of namespace '%s'".formatted(namespace), e + ); + } } @Cacheable("namespace.allNames") diff --git a/backend/src/main/java/de/amos/apachepulsarui/service/TenantService.java b/backend/src/main/java/de/amos/apachepulsarui/service/TenantService.java index 9447bf80..167d67f9 100644 --- a/backend/src/main/java/de/amos/apachepulsarui/service/TenantService.java +++ b/backend/src/main/java/de/amos/apachepulsarui/service/TenantService.java @@ -56,11 +56,11 @@ public List getAllFiltered(List tenants) throws PulsarApiExce @Cacheable("tenant.detail") public TenantDetailDto getTenantDetails(String tenantName) { List namespacesOfTenant = namespaceService.getAllOfTenant(tenantName); - return TenantDetailDto.builder() - .name(tenantName) - .tenantInfo(getTenantInfo(tenantName)) - .namespaces(namespacesOfTenant) - .build(); + return TenantDetailDto.create( + tenantName, + getTenantInfo(tenantName), + namespacesOfTenant + ); } private TenantInfo getTenantInfo(String tenantName) { diff --git a/backend/src/test/java/de/amos/apachepulsarui/controller/NamespaceControllerTest.java b/backend/src/test/java/de/amos/apachepulsarui/controller/NamespaceControllerTest.java index d3529015..82ee4fc3 100644 --- a/backend/src/test/java/de/amos/apachepulsarui/controller/NamespaceControllerTest.java +++ b/backend/src/test/java/de/amos/apachepulsarui/controller/NamespaceControllerTest.java @@ -94,14 +94,19 @@ void getAll_WithNothing_returnsAllNamespacesFromAllTenants() throws Exception { @Test void getNamespaceDetails_returnsNamespace() throws Exception { - NamespaceDetailDto namespace = NamespaceDetailDto.fromString("tenantX/namespace1"); - namespace.setTopics(List.of("a", "b")); + NamespaceDetailDto namespace = NamespaceDetailDto.create( + "tenantX/namespace1", + null, + null, + null, + List.of("a", "b") + ); Mockito.when(namespaceService.getNamespaceDetails("tenantX/namespace1")).thenReturn(namespace); mockMvc.perform(get("/namespace?name=tenantX/namespace1")) .andExpect(status().isOk()) - .andExpect(jsonPath("$.id", equalTo(namespace.getId()))) + .andExpect(jsonPath("$.name", equalTo(namespace.getName()))) .andExpect(jsonPath("$.topics", equalTo(namespace.getTopics()))); } diff --git a/backend/src/test/java/de/amos/apachepulsarui/service/NamespaceServiceIntegrationTest.java b/backend/src/test/java/de/amos/apachepulsarui/service/NamespaceServiceIntegrationTest.java index 3fb43b63..6a07df76 100644 --- a/backend/src/test/java/de/amos/apachepulsarui/service/NamespaceServiceIntegrationTest.java +++ b/backend/src/test/java/de/amos/apachepulsarui/service/NamespaceServiceIntegrationTest.java @@ -76,7 +76,7 @@ void getNamespaceDetails_returnsNamespaces() throws PulsarAdminException { NamespaceDetailDto namespace = namespaceService.getNamespaceDetails("tenant1/namespace1"); - Assertions.assertThat(namespace.getId()).isEqualTo("tenant1/namespace1"); + Assertions.assertThat(namespace.getName()).isEqualTo("tenant1/namespace1"); Assertions.assertThat(namespace.getTopics()).contains("persistent://tenant1/namespace1/testTopic"); }