Skip to content

Commit

Permalink
added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oemergenc committed Mar 12, 2020
1 parent 55ba247 commit 2f28399
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.github.oemergenc.hbase.orm.extensions.componenttests

import io.github.oemergenc.hbase.orm.extensions.dao.MultipleDynamicColumsWithSameFamilyDao

import static io.github.oemergenc.hbase.orm.extensions.data.TestContent.getDefaultMultiHBDynamicColumWithSameFamilyRecord

class MultiDynamicColumnWithSameFamilyComponentSpec extends AbstractComponentSpec {
def dao = new MultipleDynamicColumsWithSameFamilyDao(bigTableHelper.connect())

def "Converting to get for dynamic qualifiers works"() {
given:
def staticId = UUID.randomUUID() as String
def record = getDefaultMultiHBDynamicColumWithSameFamilyRecord(staticId)

when:
dao.persist(record)

then:
def resultResult = dao.getRecordForDynamicFamily(staticId, queriedQualifierParts)

then:
resultResult.isPresent() == isPresent

where:
queriedQualifierParts | isPresent
["dv1_dp1_1", "otherId_1"] | true
["dv1_dp1_2", "otherId_2"] | true
["dv2_dp1_1", "otherId_1"] | true
["dv2_dp1_2", "otherId_2"] | true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.github.oemergenc.hbase.orm.extensions.data
import io.github.oemergenc.hbase.orm.extensions.domain.dto.DependentWithListType
import io.github.oemergenc.hbase.orm.extensions.domain.dto.DependentWithPrimitiveTypes
import io.github.oemergenc.hbase.orm.extensions.domain.records.MultipleHBDynamicColumnsRecord
import io.github.oemergenc.hbase.orm.extensions.domain.records.MultipleHBDynamicColumnsWithSameFamilyRecord

class TestContent {
static def getStubDependend() {
Expand Down Expand Up @@ -52,4 +53,17 @@ class TestContent {
def record = new MultipleHBDynamicColumnsRecord(staticId, dynamicValues1, dynamicValues2, dynamicValues3, dynamicValues4)
record
}

static def getDefaultMultiHBDynamicColumWithSameFamilyRecord(String staticId) {
def dynamicValues1 = [
new DependentWithListType("dv1_dp1_1", "otherId_1", [], "aString_1"),
new DependentWithListType("dv1_dp1_2", "otherId_2", [], "aString_2"),
]
def dynamicValues2 = [
new DependentWithListType("dv2_dp1_1", "otherId_1", [], "aString_1"),
new DependentWithListType("dv2_dp1_2", "otherId_2", [], "aString_2"),
]
def record = new MultipleHBDynamicColumnsWithSameFamilyRecord(staticId, dynamicValues1, dynamicValues2)
record
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package io.github.oemergenc.hbase.orm.extensions.mapper

import io.github.oemergenc.hbase.orm.extensions.HBDynamicColumnObjectMapper
import io.github.oemergenc.hbase.orm.extensions.domain.records.MultipleHBDynamicColumnsRecord
import io.github.oemergenc.hbase.orm.extensions.domain.records.MultipleHBDynamicColumnsWithSameFamilyRecord
import spock.lang.Specification

import static io.github.oemergenc.hbase.orm.extensions.data.TestContent.getDefaultMultiHBDynamicColumWithSameFamilyRecord

class MultiDynamicColumnWithSameFamilyMapperSpec extends Specification {
def mapper = new HBDynamicColumnObjectMapper()

def "Converting a record with multiple dynamic columns works"() {
given:
def staticId = UUID.randomUUID() as String
def record = getDefaultMultiHBDynamicColumWithSameFamilyRecord(staticId)

when:
def result = mapper.writeValueAsResult(record)

then:
result
result.getFamilyMap("dynamicFamily1".bytes)["df1#dv1_dp1_1:otherId_1".bytes]
result.getFamilyMap("dynamicFamily1".bytes)["df1#dv1_dp1_2:otherId_2".bytes]
result.getFamilyMap("dynamicFamily1".bytes)["df2#dv2_dp1_1:otherId_1".bytes]
result.getFamilyMap("dynamicFamily1".bytes)["df2#dv2_dp1_2:otherId_2".bytes]
result.getFamilyMap("staticFamily".bytes)['staticId'.bytes]

when:
def recordResult = mapper.readValue(result, MultipleHBDynamicColumnsWithSameFamilyRecord.class)

then:
recordResult
recordResult.staticId == staticId

and:
recordResult.dynamicValues1.collect { it.dynamicPart1 }.containsAll(["dv1_dp1_1", "dv1_dp1_2"])
recordResult.dynamicValues1.collect { it.otherId }.containsAll(["otherId_1", "otherId_2"])
recordResult.dynamicValues1.collect { it.aString }.containsAll(["aString_1", "aString_2"])

and:
recordResult.dynamicValues2.collect { it.dynamicPart1 }.containsAll(["dv2_dp1_1", "dv2_dp1_2"])
recordResult.dynamicValues2.collect { it.otherId }.containsAll(["otherId_1", "otherId_2"])
recordResult.dynamicValues2.collect { it.aString }.containsAll(["aString_1", "aString_2"])
recordResult.dynamicValues2.flatten { it.nodes } == []
}

def "Converting to get for dynamic qualifiers works"() {
given:
def staticId = UUID.randomUUID() as String
def record = getDefaultMultiHBDynamicColumWithSameFamilyRecord(staticId)

when:
mapper.writeValueAsResult(record)

then:
def get = mapper.getAsGet(MultipleHBDynamicColumnsRecord.class, staticId.bytes, "dynamicFamily1", queriedQualifierParts)

then:
get
get.numFamilies() == expectedNumSize

where:
queriedQualifierParts | expectedNumSize
["dynamicId1"] | 1
["dv1_dp1_1", "dv1_dp2_1"] | 1
["237912837"] | 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.github.oemergenc.hbase.orm.extensions.dao;

import io.github.oemergenc.hbase.orm.extensions.AbstractHBDynamicDAO;
import io.github.oemergenc.hbase.orm.extensions.domain.records.MultipleHBDynamicColumnsWithSameFamilyRecord;
import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.hbase.client.Connection;

import java.util.List;
import java.util.Optional;

@Slf4j
public class MultipleDynamicColumsWithSameFamilyDao extends AbstractHBDynamicDAO<String, MultipleHBDynamicColumnsWithSameFamilyRecord> {
public MultipleDynamicColumsWithSameFamilyDao(Connection connection) {
super(connection);
}

public Optional<MultipleHBDynamicColumnsWithSameFamilyRecord> getRecordForDynamicFamily(String rowKey,
List<String> qualifierParts) {
try {
return Optional.ofNullable(getDynamicCell(rowKey, "dynamicFamily1", qualifierParts));
} catch (Exception e) {
log.error("There was an error while trying to get column family data of hbdynamic column", e);
e.printStackTrace();
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.github.oemergenc.hbase.orm.extensions.domain.records;

import com.flipkart.hbaseobjectmapper.DynamicQualifier;
import com.flipkart.hbaseobjectmapper.Family;
import com.flipkart.hbaseobjectmapper.HBColumn;
import com.flipkart.hbaseobjectmapper.HBRecord;
import com.flipkart.hbaseobjectmapper.HBRowKey;
import com.flipkart.hbaseobjectmapper.HBTable;
import io.github.oemergenc.hbase.orm.extensions.HBDynamicColumn;
import io.github.oemergenc.hbase.orm.extensions.domain.dto.DependentWithListType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
@HBTable(name = "multiple_dynamic_columns_table", families = {
@Family(name = "dynamicFamily1"),
@Family(name = "staticFamily")})
public class MultipleHBDynamicColumnsWithSameFamilyRecord implements HBRecord<String> {

@HBRowKey
@HBColumn(family = "staticFamily", column = "staticId")
private String staticId;

@HBDynamicColumn(family = "dynamicFamily1", alias = "df1", qualifier = @DynamicQualifier(parts = {"dynamicPart1", "otherId"}))
private List<DependentWithListType> dynamicValues1;

@HBDynamicColumn(family = "dynamicFamily1", alias = "df2", qualifier = @DynamicQualifier(parts = {"dynamicPart1", "otherId"}))
private List<DependentWithListType> dynamicValues2;

@Override
public String composeRowKey() {
return staticId;
}

@Override
public void parseRowKey(String rowKey) {
this.staticId = rowKey;
}
}

0 comments on commit 2f28399

Please sign in to comment.