Skip to content

Commit

Permalink
리팩토링 및 테스트 보강
Browse files Browse the repository at this point in the history
  • Loading branch information
seungh0 committed Jul 7, 2024
1 parent 8874d64 commit c367937
Show file tree
Hide file tree
Showing 24 changed files with 253 additions and 55 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package kr.hakdang.cassdio.common.error;

import lombok.Getter;

import java.util.Collections;
import java.util.List;

@Getter
public abstract class BaseException extends RuntimeException {

private final ErrorCode errorCode;
private final List<String> reasons;

protected BaseException(String message, ErrorCode errorCode) {
super(message);
this.errorCode = errorCode;
this.reasons = Collections.emptyList();
}

protected BaseException(String message, ErrorCode errorCode, Throwable cause) {
super(message, cause);
this.errorCode = errorCode;
this.reasons = Collections.emptyList();
}

protected BaseException(String message, ErrorCode errorCode, List<String> reasons) {
super(message);
this.errorCode = errorCode;
this.reasons = reasons;
}

protected BaseException(String message, ErrorCode errorCode, Throwable cause, List<String> reasons) {
super(message, cause);
this.errorCode = errorCode;
this.reasons = reasons;
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package kr.hakdang.cassdio.common;
package kr.hakdang.cassdio.common.error;

import lombok.Getter;

@Getter
public enum ErrorCode {

E400_INVALID_PARAMETER(400, "invalid_parameter"),
E404_NOT_FOUND_CLUSTER_NODE(404, "not_exists_node"),
E404_NOT_FOUND_KEYSPACE(404, "not_exists_keyspace"),
E404_NOT_FOUND_TABLE(404, "not_exists_table"),
E500_INTERNAL_SERVER_ERROR(500, "internal_server_error"),
;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kr.hakdang.cassdio.common;
package kr.hakdang.cassdio.common.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package kr.hakdang.cassdio.core.domain.cluster;

import kr.hakdang.cassdio.common.error.BaseException;
import kr.hakdang.cassdio.common.error.ErrorCode;

/**
* ClusterException
*
* @author Seungho Kang ([email protected])
* @version 1.0.0
* @since 2024. 07. 07.
*/
public class ClusterException {

public static class ClusterNodeNotFoundException extends BaseException {

protected ClusterNodeNotFoundException(String message) {
super(message, ErrorCode.E404_NOT_FOUND_CLUSTER_NODE);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.metadata.Node;
import kr.hakdang.cassdio.core.domain.cluster.ClusterException.ClusterNodeNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Map;
Expand All @@ -21,7 +22,7 @@ public ClusterNode getNode(CqlSession session, UUID nodeId) {

Node node = nodes.get(nodeId);
if (node == null) {
throw new IllegalArgumentException(String.format("not exists node(%s)", nodeId));
throw new ClusterNodeNotFoundException(String.format("not exists node(%s)", nodeId));
}

return ClusterNode.from(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -49,11 +48,7 @@ public static KeyspaceFilter makeKeyspaceFilter(DriverContext context) {
}

public static boolean isVirtualKeyspace(DriverContext context, String keyspace) {
return !makeKeyspaceFilter(context).includes(keyspace)
&& Arrays.asList(
CassandraSystemKeyspace.SYSTEM_VIRTUAL_SCHEMA.getKeyspaceName(),
CassandraSystemKeyspace.SYSTEM_VIEWS.getKeyspaceName()
).contains(keyspace);
return !makeKeyspaceFilter(context).includes(keyspace) && CassandraSystemKeyspace.isVirtualSystemKeyspace(keyspace);
}

public static String generateClusterId() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package kr.hakdang.cassdio.core.domain.cluster.info;

import com.fasterxml.jackson.databind.ObjectMapper;
import kr.hakdang.cassdio.common.Jsons;
import kr.hakdang.cassdio.common.utils.Jsons;
import kr.hakdang.cassdio.core.domain.cluster.ClusterUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package kr.hakdang.cassdio.core.domain.cluster.info;

import com.fasterxml.jackson.databind.ObjectMapper;
import kr.hakdang.cassdio.common.Jsons;
import kr.hakdang.cassdio.common.utils.Jsons;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,32 @@ public enum CassandraSystemKeyspace {
SYSTEM_AUTH("system_auth"),
SYSTEM_DISTRIBUTED("system_distributed"),
SYSTEM_TRACES("system_traces"),
SYSTEM_VIEWS("system_views"),
SYSTEM_VIRTUAL_SCHEMA("system_virtual_schema"),
SYSTEM_VIEWS("system_views", true),
SYSTEM_VIRTUAL_SCHEMA("system_virtual_schema", true),
;

private final String keyspaceName;
private final boolean isVirtualKeyspace;

CassandraSystemKeyspace(String keyspaceName, boolean isVirtualKeyspace) {
this.keyspaceName = keyspaceName;
this.isVirtualKeyspace = isVirtualKeyspace;
}

CassandraSystemKeyspace(String keyspaceName) {
this.keyspaceName = keyspaceName;
this.isVirtualKeyspace = false;
}

public static boolean isSystemKeyspace(String keyspaceName) {
return Arrays.stream(CassandraSystemKeyspace.values())
.anyMatch(keyspace -> keyspace.getKeyspaceName().equals(keyspaceName));
}

public static boolean isVirtualSystemKeyspace(String keyspaceName) {
return Arrays.stream(CassandraSystemKeyspace.values())
.filter(keyspace -> keyspace.isVirtualKeyspace)
.anyMatch(keyspace -> keyspace.getKeyspaceName().equals(keyspaceName));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package kr.hakdang.cassdio.core.domain.cluster.keyspace;

import kr.hakdang.cassdio.common.error.BaseException;
import kr.hakdang.cassdio.common.error.ErrorCode;

/**
* ClusterKeyspaceException
*
* @author Seungho Kang ([email protected])
* @version 1.0.0
* @since 2024. 07. 07.
*/
public class ClusterKeyspaceException {

public static class ClusterKeyspaceNotFoundException extends BaseException {

public ClusterKeyspaceNotFoundException(String message) {
super(message, ErrorCode.E404_NOT_FOUND_KEYSPACE);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package kr.hakdang.cassdio.core.domain.cluster.keyspace.table;

import kr.hakdang.cassdio.common.error.BaseException;
import kr.hakdang.cassdio.common.error.ErrorCode;

/**
* ClusterTableException
*
* @author Seungho Kang ([email protected])
* @version 1.0.0
* @since 2024. 07. 07.
*/
public class ClusterTableException {

public static class CLusterTableNotFoundException extends BaseException {

protected CLusterTableNotFoundException(String message) {
super(message, ErrorCode.E404_NOT_FOUND_CLUSTER_NODE);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import com.datastax.oss.driver.api.querybuilder.QueryBuilder;
import kr.hakdang.cassdio.core.domain.cluster.BaseClusterCommander;
import kr.hakdang.cassdio.core.domain.cluster.keyspace.CassandraSystemKeyspace;
import kr.hakdang.cassdio.core.domain.cluster.keyspace.ClusterKeyspaceException.ClusterKeyspaceNotFoundException;
import kr.hakdang.cassdio.core.domain.cluster.keyspace.table.ClusterTableArgs.ClusterTableGetArgs;
import kr.hakdang.cassdio.core.domain.cluster.keyspace.table.ClusterTableException.CLusterTableNotFoundException;
import kr.hakdang.cassdio.core.domain.cluster.keyspace.table.column.CassandraSystemTablesColumn;
import kr.hakdang.cassdio.core.domain.cluster.keyspace.table.column.Column;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -42,15 +44,15 @@ public ClusterTableGetResult getTable(CqlSession session, ClusterTableGetArgs ar

Row tableRow = session.execute(statement).one();
if (tableRow == null) {
throw new IllegalArgumentException(String.format("not found table(%s) in keyspace(%s)", args.getTable(), args.getKeyspace()));
throw new CLusterTableNotFoundException(String.format("not found table(%s) in keyspace(%s)", args.getTable(), args.getKeyspace()));
}

String tableDescribe = "";
if (!CassandraSystemKeyspace.isSystemKeyspace(args.getKeyspace()) && args.isWithTableDescribe()) {
TableMetadata tableMetadata = session.getMetadata().getKeyspace(args.getKeyspace())
.orElseThrow(() -> new RuntimeException("not found keyspace"))
.orElseThrow(() -> new ClusterKeyspaceNotFoundException(String.format("not found keyspace (%s)", args.getKeyspace())))
.getTable(args.getTable())
.orElseThrow(() -> new RuntimeException("not found table"));
.orElseThrow(() -> new CLusterTableNotFoundException(String.format("not found table(%s)", args.getTable())));

tableDescribe = tableMetadata.describe(true);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package kr.hakdang.cassdio.core.domain.cluster.keyspace.table;

import kr.hakdang.cassdio.core.domain.cluster.keyspace.table.column.Column;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.List;
import java.util.Map;

/**
Expand All @@ -25,7 +23,7 @@ public class ClusterTableGetResult2 {
private Map<String, Object> describe;

@Builder
private ClusterTableGetResult2(Map<String, Object> table, Map<String, Object> describe, List<Column> columns) {
private ClusterTableGetResult2(Map<String, Object> table, Map<String, Object> describe) {
this.table = table;
this.describe = describe;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.metadata.Node;
import kr.hakdang.cassdio.IntegrationTest;
import kr.hakdang.cassdio.core.domain.cluster.ClusterException.ClusterNodeNotFoundException;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

Expand All @@ -27,7 +28,7 @@ class ClusterNodeGetCommanderTest extends IntegrationTest {
void not_exists_node_in_cluster() {
// when & then
assertThatThrownBy(() -> clusterNodeGetCommander.getNode(makeSession(), UUID.randomUUID()))
.isInstanceOf(IllegalArgumentException.class);
.isInstanceOf(ClusterNodeNotFoundException.class);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package kr.hakdang.cassdio.core.domain.cluster;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/**
* ClusterQueryCommanderArgsTest
*
* @author Seungho Kang ([email protected])
* @version 1.0.0
* @since 2024. 07. 07.
*/
class ClusterQueryCommanderArgsTest {

@Test
void default_pageSize_is_50() {
// given
ClusterQueryCommanderArgs args = ClusterQueryCommanderArgs.builder().build();

// when
int sut = args.getPageSize();

// then
assertThat(sut).isEqualTo(50);
}

@Test
void when_pageSize_is_over_500_throw_exception() {
// when & then
assertThatThrownBy(() ->
ClusterQueryCommanderArgs.builder()
.pageSize(501)
.build()
).isInstanceOf(RuntimeException.class);
}

@Test
void default_timeout_is_3s() {
// given
ClusterQueryCommanderArgs args = ClusterQueryCommanderArgs.builder().build();

// when
int sut = args.getTimeoutSeconds();

// then
assertThat(sut).isEqualTo(3);
}

@Test
void when_timeout_is_over_1m_throw_exception() {
// when & then
assertThatThrownBy(() ->
ClusterQueryCommanderArgs.builder()
.timeoutSeconds(61)
.build()
).isInstanceOf(RuntimeException.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ void not_system_keyspace() {
assertThat(CassandraSystemKeyspace.isSystemKeyspace("demo")).isFalse();
}

@Test
void isVirtualKeyspace() {
assertThat(CassandraSystemKeyspace.isVirtualSystemKeyspace("system_virtual_schema")).isTrue();
assertThat(CassandraSystemKeyspace.isVirtualSystemKeyspace("system_views")).isTrue();
assertThat(CassandraSystemKeyspace.isVirtualSystemKeyspace("system_traces")).isFalse();
assertThat(CassandraSystemKeyspace.isVirtualSystemKeyspace("demo")).isFalse();
}

}
Loading

0 comments on commit c367937

Please sign in to comment.