Skip to content

Commit

Permalink
#15: Renaming MarshallingDTO -> ClusterHealthDTO
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Geisselmeier committed May 5, 2015
1 parent e41578c commit be324c3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

public abstract class Marshaller {

public static final class MarshallingDTO {
public static final class ClusterHealthDTO {
public List<String> instances;
public List<Rule<?>> rules;
}
Expand Down Expand Up @@ -46,14 +46,14 @@ public static String lookupVersion(String serialized) throws MarshallingExceptio
throw new MarshallingException("No serialization version found");
}

public abstract String marshall(MarshallingDTO dto) throws MarshallingException;
public abstract String marshall(ClusterHealthDTO dto) throws MarshallingException;

public String marshall(List<String> instances, List<Rule<?>> rules) throws MarshallingException {
MarshallingDTO dto = new MarshallingDTO();
ClusterHealthDTO dto = new ClusterHealthDTO();
dto.instances = instances;
dto.rules = rules;
return marshall(dto);
};

public abstract MarshallingDTO unmarshall(String s) throws MarshallingException;
public abstract ClusterHealthDTO unmarshall(String s) throws MarshallingException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

import org.levigo.jadice.server.converterclient.gui.clusterhealth.rule.ImmutableBooleanRule;
import org.levigo.jadice.server.converterclient.gui.clusterhealth.rule.NumericRule;
import org.levigo.jadice.server.converterclient.gui.clusterhealth.serialization.Marshaller.MarshallingDTO;
import org.levigo.jadice.server.converterclient.gui.clusterhealth.serialization.Marshaller.ClusterHealthDTO;
import org.levigo.jadice.server.converterclient.gui.clusterhealth.serialization.MarshallingException;

public class ClusterHealthMapper {

public ClusterHealth map(MarshallingDTO dto) throws MarshallingException {
public ClusterHealth map(ClusterHealthDTO dto) throws MarshallingException {
final ClusterHealth result = new ClusterHealth();

result.instances = dto.instances;
Expand All @@ -32,8 +32,8 @@ public ClusterHealth map(MarshallingDTO dto) throws MarshallingException {
return result;
}

public MarshallingDTO unmap(ClusterHealth ch) throws MarshallingException {
MarshallingDTO result = new MarshallingDTO();
public ClusterHealthDTO unmap(ClusterHealth ch) throws MarshallingException {
ClusterHealthDTO result = new ClusterHealthDTO();
result.instances = ch.instances;
result.rules = new ArrayList<>(ch.rules.size());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class V1Marshaller extends Marshaller {

private final boolean prettyPrint = true;

public String marshall(MarshallingDTO dto) throws MarshallingException {
public String marshall(ClusterHealthDTO dto) throws MarshallingException {
final ClusterHealth ch = mapper.map(dto);
try {
return prettyPrint //
Expand All @@ -28,7 +28,7 @@ public String marshall(MarshallingDTO dto) throws MarshallingException {
}
}

public MarshallingDTO unmarshall(String s) throws MarshallingException {
public ClusterHealthDTO unmarshall(String s) throws MarshallingException {
try {
final ClusterHealth ch = objectMapper.readValue(s.getBytes(Charset.forName("UTF-8")), ClusterHealth.class);
return mapper.unmap(ch);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.levigo.jadice.server.converterclient.gui.clusterhealth.serialization;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand All @@ -21,7 +18,7 @@
import org.levigo.jadice.server.converterclient.gui.clusterhealth.rule.Rule;
import org.levigo.jadice.server.converterclient.gui.clusterhealth.rule.ServerRunningRule;
import org.levigo.jadice.server.converterclient.gui.clusterhealth.rule.TotalFailureRateRule;
import org.levigo.jadice.server.converterclient.gui.clusterhealth.serialization.Marshaller.MarshallingDTO;
import org.levigo.jadice.server.converterclient.gui.clusterhealth.serialization.Marshaller.ClusterHealthDTO;
import org.levigo.jadice.server.converterclient.gui.clusterhealth.serialization.v1.V1Marshaller;

import com.levigo.jadice.server.util.Util;
Expand All @@ -35,19 +32,7 @@ public class TestMarshaller {
private static List<Rule<?>> RULES;

private static List<String> INSTANCES;

@Test
public void testEmptySerialization() throws Exception {
final MarshallingDTO dto1 = new MarshallingDTO();
dto1.instances = Collections.emptyList();
dto1.rules = Collections.emptyList();
Marshaller m = Marshaller.getDefault();
final MarshallingDTO dto2 = m.unmarshall(m.marshall(dto1));

assertTrue("instances shall be empty", dto2.instances.isEmpty());
assertTrue("rules shall be empty", dto2.rules.isEmpty());
}


@BeforeClass
public static void createRules() {
RULES = new ArrayList<>();
Expand All @@ -58,14 +43,26 @@ public static void createRules() {
RULES.add(new RecentFailureRateRule(40f));
RULES.add(new TotalFailureRateRule(50f));
}

@BeforeClass
public static void createInstances() {
INSTANCES = new ArrayList<>();
INSTANCES.add("localhost:61619");
INSTANCES.add("jadice-server.example.com:61619");
}


@Test
public void testEmptySerialization() throws Exception {
final ClusterHealthDTO dto1 = new ClusterHealthDTO();
dto1.instances = Collections.emptyList();
dto1.rules = Collections.emptyList();
Marshaller m = Marshaller.getDefault();
final ClusterHealthDTO dto2 = m.unmarshall(m.marshall(dto1));

assertTrue("instances shall be empty", dto2.instances.isEmpty());
assertTrue("rules shall be empty", dto2.rules.isEmpty());
}
@Test
public void testLookupVersion() throws Exception {
assertEquals("Version mismatch", "42.0", Marshaller.lookupVersion("{\"version\": \"42.0\"}"));
Expand All @@ -86,7 +83,7 @@ public void testNoV99MarshallerAvailable() throws Exception {
public void testV1Marshalling() throws Exception {
final String m = Marshaller.get(V1).marshall(INSTANCES, RULES);
assertEquals("Wrong version marshalled", "1.0", Marshaller.lookupVersion(m));
final MarshallingDTO unmarshalled = Marshaller.get(V1).unmarshall(m);
final ClusterHealthDTO unmarshalled = Marshaller.get(V1).unmarshall(m);

assertArrayEquals("Wrong instances", INSTANCES.toArray(), unmarshalled.instances.toArray());
assertArrayEquals("Wrong rules", RULES.toArray(), unmarshalled.rules.toArray());
Expand All @@ -99,7 +96,7 @@ public void testV1Unmarshalling() throws Exception {
String version = Marshaller.lookupVersion(json);
assertEquals("Wrong version detected", V1, version);

final MarshallingDTO unmarshalled = Marshaller.get(V1).unmarshall(json);
final ClusterHealthDTO unmarshalled = Marshaller.get(V1).unmarshall(json);

assertArrayEquals("Wrong instances", INSTANCES.toArray(), unmarshalled.instances.toArray());
assertArrayEquals("Wrong rules", RULES.toArray(), unmarshalled.rules.toArray());
Expand Down

0 comments on commit be324c3

Please sign in to comment.