Skip to content

Commit

Permalink
Merge pull request #864 from Nitish1814/testRefactor
Browse files Browse the repository at this point in the history
Test refactor
  • Loading branch information
sonalgoyal authored Aug 14, 2024
2 parents b370045 + b330a03 commit 257851e
Show file tree
Hide file tree
Showing 40 changed files with 1,912 additions and 1,102 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package zingg.common.client.util;

import java.util.List;

import zingg.common.client.ZFrame;

public abstract class DFObjectUtil<S, D, R, C> {

protected final IWithSession<S> iWithSession;

protected DFObjectUtil(IWithSession<S> iWithSession) {
this.iWithSession = iWithSession;
}

public abstract ZFrame<D, R, C> getDFFromObjectList(List objList, Class objClass) throws Exception;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package zingg.common.client.util;

public interface IWithSession<S> {

public void setSession(S s);

public S getSession();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package zingg.common.client.util;

import java.lang.reflect.Field;

public class PojoToArrayConverter {

public static Object[] getObjectArray(Object object) throws IllegalAccessException {
Field[] fieldsInChildClass = object.getClass().getDeclaredFields();
Field[] fieldsInParentClass = null;

int fieldCountInChildClass = fieldsInChildClass.length;
int fieldCount = fieldCountInChildClass;

if (object.getClass().getSuperclass() != null) {
fieldCount += object.getClass().getSuperclass().getDeclaredFields().length;
fieldsInParentClass = object.getClass().getSuperclass().getDeclaredFields();
}

//fieldCount = fieldCountChild + fieldCountParent
Object[] objArr = new Object[fieldCount];

int idx = 0;

//iterate through child class fields
for (; idx < fieldCountInChildClass; idx++) {
Field field = fieldsInChildClass[idx];
field.setAccessible(true);
objArr[idx] = field.get(object);
}

//iterate through super class fields
for (; idx < fieldCount; idx++) {
Field field = fieldsInParentClass[idx - fieldCountInChildClass];
field.setAccessible(true);
objArr[idx] = field.get(object);
}

return objArr;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package zingg.common.client.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public abstract class StructTypeFromPojoClass<ST, SF, T> {

public abstract ST getStructType(Class<?> objClass) throws Exception;

public List<SF> getFields(Class<?> objClass) {
List<SF> structFields = new ArrayList<SF>();
Field[] fields = objClass.getDeclaredFields();

//add child class fields in struct
for (Field f : fields) {
structFields.add(getStructField(f));
}

//add parent class fields in struct
if (objClass.getSuperclass() != null) {
Field[] fieldsSuper = objClass.getSuperclass().getDeclaredFields();
for (Field f : fieldsSuper) {
structFields.add(getStructField(f));
}
}
return structFields;
}

public abstract SF getStructField(Field field);

public abstract T getSFType(Class<?> t);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package zingg.common.client.util;

public class WithSession<S> implements IWithSession<S> {

S session;
@Override
public void setSession(S session) {
this.session = session;
}

@Override
public S getSession() {
return session;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package zingg.common.client.util;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.stream.Stream;
Expand All @@ -10,14 +8,10 @@
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.Arguments;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import static org.junit.jupiter.api.Named.named;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.junit.jupiter.api.Test;

public class TestStringRedactor {

@ParameterizedTest(name="{0}")
Expand Down
100 changes: 100 additions & 0 deletions common/core/src/test/java/zingg/common/core/block/TestBlockBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package zingg.common.core.block;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.junit.jupiter.api.Test;

import zingg.common.client.ArgumentsUtil;
import zingg.common.client.FieldDefinition;
import zingg.common.client.IArguments;
import zingg.common.client.MatchType;
import zingg.common.client.ZFrame;
import zingg.common.client.ZinggClientException;
import zingg.common.client.util.DFObjectUtil;
import zingg.common.core.util.BlockingTreeUtil;
import zingg.common.core.util.HashUtil;
import zingg.common.core.model.Event;
import zingg.common.core.model.EventPair;
import zingg.common.core.data.EventTestData;

public abstract class TestBlockBase<S, D, R, C, T> {

public ArgumentsUtil argumentsUtil = new ArgumentsUtil();
public final DFObjectUtil<S, D, R, C> dfObjectUtil;
public final HashUtil<S, D, R, C, T> hashUtil;
public final BlockingTreeUtil<S, D, R, C, T> blockingTreeUtil;

public TestBlockBase(DFObjectUtil<S, D, R, C> dfObjectUtil, HashUtil<S, D, R, C, T> hashUtil, BlockingTreeUtil<S, D, R, C, T> blockingTreeUtil) {
this.dfObjectUtil = dfObjectUtil;
this.hashUtil = hashUtil;
this.blockingTreeUtil = blockingTreeUtil;
}

@Test
public void testTree() throws Throwable {

// form tree
ZFrame<D, R, C> zFrameEvent = dfObjectUtil.getDFFromObjectList(EventTestData.createSampleEventData(), Event.class);
ZFrame<D, R, C> zFrameEventCluster = dfObjectUtil.getDFFromObjectList(EventTestData.createSampleClusterEventData(), EventPair.class);
IArguments args = getArguments();

Tree<Canopy<R>> blockingTree = blockingTreeUtil.createBlockingTreeFromSample(zFrameEvent, zFrameEventCluster, 0.5, -1,
args, hashUtil.getHashFunctionList());

// primary deciding is unique year so identityInteger should have been picked
Canopy<R> head = blockingTree.getHead();
assertEquals("identityInteger", head.getFunction().getName());
blockingTree.toString();
}

private IArguments getArguments() throws ZinggClientException {
String configFilePath = Objects.requireNonNull(getClass().getResource("../../../../testFebrl/config.json")).getFile();

IArguments args = argumentsUtil.createArgumentsFromJSON(configFilePath, "trainMatch");

List<FieldDefinition> fdList = getFieldDefList();

args.setFieldDefinition(fdList);
return args;
}

private List<FieldDefinition> getFieldDefList() {
List<FieldDefinition> fdList = new ArrayList<FieldDefinition>(4);

FieldDefinition idFD = new FieldDefinition();
idFD.setDataType("integer");
idFD.setFieldName("id");
ArrayList<MatchType> matchTypelistId = new ArrayList<MatchType>();
matchTypelistId.add(MatchType.DONT_USE);
idFD.setMatchType(matchTypelistId);
fdList.add(idFD);

ArrayList<MatchType> matchTypelistFuzzy = new ArrayList<MatchType>();
matchTypelistFuzzy.add(MatchType.FUZZY);


FieldDefinition yearFD = new FieldDefinition();
yearFD.setDataType("integer");
yearFD.setFieldName("year");
yearFD.setMatchType(matchTypelistFuzzy);
fdList.add(yearFD);

FieldDefinition eventFD = new FieldDefinition();
eventFD.setDataType("string");
eventFD.setFieldName("event");
eventFD.setMatchType(matchTypelistFuzzy);
fdList.add(eventFD);

FieldDefinition commentFD = new FieldDefinition();
commentFD.setDataType("string");
commentFD.setFieldName("comment");
commentFD.setMatchType(matchTypelistFuzzy);
fdList.add(commentFD);
return fdList;
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package zingg.block;
package zingg.common.core.block;

import org.junit.jupiter.api.*;

import zingg.common.core.block.Tree;

import static org.junit.jupiter.api.Assertions.*;

public class TestTree {

@Test
Expand Down
Loading

0 comments on commit 257851e

Please sign in to comment.