Skip to content

Commit

Permalink
#207: Unify class building process
Browse files Browse the repository at this point in the history
Signed-off-by: Jonas Arnhold <[email protected]>
  • Loading branch information
jnsrnhld committed Jul 16, 2023
1 parent 1e97f5d commit f5032c7
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,4 +25,20 @@ public class ClusterDetailDto {
private String brokerServiceUrl;
private String serviceUrl;

public static ClusterDetailDto create(
String name,
List<String> activeBrokers,
List<String> 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,27 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class NamespaceDetailDto {

private String id;
private String name;
private List<String> 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<String> 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<String> topics) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,4 +24,12 @@ public List<String> getNamespaces() {
return List.copyOf(namespaces);
}

public static TenantDetailDto create(String name, TenantInfo tenantInfo, List<String> namespaces) {
TenantDetailDto tenantDetailDto = new TenantDetailDto();
tenantDetailDto.name = name;
tenantDetailDto.tenantInfo = tenantInfo;
tenantDetailDto.namespaces = namespaces;
return tenantDetailDto;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,12 @@ public List<ClusterDto> getAllNames() {

@Cacheable("cluster.detail")
public ClusterDetailDto getClusterDetails(String clusterName) {

ClusterData clusterData = getClusterData(clusterName);
List<String> activeBrokers = getActiveBrokers(clusterName);
List<String> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,23 @@ public List<NamespaceDto> getAllForTenants(List<String> 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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ public List<TenantDto> getAllFiltered(List<String> tenants) throws PulsarApiExce
@Cacheable("tenant.detail")
public TenantDetailDto getTenantDetails(String tenantName) {
List<String> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down

0 comments on commit f5032c7

Please sign in to comment.