Skip to content

Commit

Permalink
Create custom Jackson serializer and deserializer for new typed Metri…
Browse files Browse the repository at this point in the history
…cType, change type output to lower case
  • Loading branch information
Michael Burman committed Aug 26, 2015
1 parent ee9f169 commit 4d07950
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.hawkular.metrics.api.jaxrs.jackson;

import java.io.IOException;

import org.hawkular.metrics.core.api.MetricType;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;

/**
* Created by miburman on 8/26/15.
*/
public class MetricTypeDeserializer extends JsonDeserializer<MetricType<?>> {

@Override public MetricType<?> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
throws IOException, JsonProcessingException {
JsonNode jsonNode = jsonParser.getCodec().readTree(jsonParser);
return MetricType.fromTextCode(jsonNode.get("type").asText());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.hawkular.metrics.api.jaxrs.jackson;

import java.io.IOException;

import org.hawkular.metrics.core.api.MetricType;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

/**
* Created by miburman on 8/26/15.
*/
public class MetricTypeSerializer extends JsonSerializer<MetricType<?>> {

@Override
public void serialize(MetricType<?> metricType, JsonGenerator jsonGenerator,
SerializerProvider serializerProvider)
throws IOException, JsonProcessingException {
jsonGenerator.writeString(metricType.getText());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,22 @@
import java.util.Map;
import java.util.Objects;

import org.hawkular.metrics.api.jaxrs.jackson.MetricTypeDeserializer;
import org.hawkular.metrics.api.jaxrs.jackson.MetricTypeSerializer;
import org.hawkular.metrics.core.api.Metric;
import org.hawkular.metrics.core.api.MetricType;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.wordnik.swagger.annotations.ApiModel;

/**
* @author jsanda
*/
@ApiModel(description = "The definition of a metric to create")
public class MetricDefinition {
public class MetricDefinition<T> {

// TODO Do we need this?
@JsonProperty
Expand Down Expand Up @@ -64,17 +68,20 @@ public class MetricDefinition {
@org.codehaus.jackson.annotate.JsonProperty
@org.codehaus.jackson.map.annotate.JsonSerialize(
include = org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion.NON_EMPTY)
private MetricType type;
@JsonSerialize(using = MetricTypeSerializer.class)
@JsonDeserialize(using = MetricTypeDeserializer.class)
private MetricType<T> type;

public MetricDefinition() {
}

@JsonCreator
public MetricDefinition(

@JsonProperty("id") String id,
@JsonProperty(value = "tags") Map<String, String> tags,
@JsonProperty("dataRetention") Integer dataRetention,
@JsonProperty("type") MetricType type) {
@JsonProperty("type") MetricType<T> type) {
this.id = id;
this.tags = tags == null ? emptyMap() : unmodifiableMap(tags);
this.dataRetention = dataRetention;
Expand All @@ -100,7 +107,7 @@ public Integer getDataRetention() {
return dataRetention;
}

public MetricType getType() {
public MetricType<T> getType() {
return type;
}

Expand All @@ -117,7 +124,7 @@ public MetricDefinition(Metric metric) {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MetricDefinition gauge = (MetricDefinition) o;
MetricDefinition<?> gauge = (MetricDefinition<?>) o;
return Objects.equals(id, gauge.getId()) && Objects.equals(type, gauge.getType());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ class CORSITest extends RESTTest {
assertEquals(200, response.status)
assertEquals(
[
[tenantId: tenantId, id: 'm11', type: 'GAUGE'],
[tenantId: tenantId, id: 'm12', type: 'GAUGE'],
[tenantId: tenantId, id: 'm11', type: 'gauge'],
[tenantId: tenantId, id: 'm12', type: 'gauge'],
],
response.data
)
Expand Down Expand Up @@ -145,8 +145,8 @@ class CORSITest extends RESTTest {
assertEquals(200, response.status)
assertEquals(
[
[tenantId: tenantId, id: 'm11', type: 'GAUGE'],
[tenantId: tenantId, id: 'm12', type: 'GAUGE'],
[tenantId: tenantId, id: 'm11', type: 'gauge'],
[tenantId: tenantId, id: 'm12', type: 'gauge'],
],
response.data
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,9 +414,9 @@ class CassandraBackendITest extends RESTTest {
assertEquals(200, response.status)
assertEquals(
[
[tenantId: tenantId, id: 'm11', type: 'GAUGE'],
[tenantId: tenantId, id: 'm12', type: 'GAUGE'],
[tenantId: tenantId, id: 'm13', tags: [a1: 'A', B1: 'B'], dataRetention: 32, type: 'GAUGE']
[tenantId: tenantId, id: 'm11', type: 'gauge'],
[tenantId: tenantId, id: 'm12', type: 'gauge'],
[tenantId: tenantId, id: 'm13', tags: [a1: 'A', B1: 'B'], dataRetention: 32, type: 'gauge']
],
response.data
)
Expand Down Expand Up @@ -454,9 +454,9 @@ class CassandraBackendITest extends RESTTest {
assertEquals(200, response.status)
assertEquals(
[
[tenantId: tenantId, id: 'm14', type: 'AVAILABILITY'],
[tenantId: tenantId, id: 'm15', type: 'AVAILABILITY'],
[tenantId: tenantId, id: 'm16', tags: [a10: '10', a11: '11'], dataRetention: 7, type: 'AVAILABILITY']
[tenantId: tenantId, id: 'm14', type: 'availability'],
[tenantId: tenantId, id: 'm15', type: 'availability'],
[tenantId: tenantId, id: 'm16', tags: [a10: '10', a11: '11'], dataRetention: 7, type: 'availability']
],
response.data
)
Expand Down Expand Up @@ -562,7 +562,7 @@ class CassandraBackendITest extends RESTTest {
assertEquals([
tenantId: tenantId,
id: 'Empty1',
type: 'GAUGE'
type: 'gauge'
], response.data)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class CountersITest extends RESTTest {
response = hawkularMetrics.get(path: "counters/$id", headers: [(tenantHeaderName): tenantId])
assertEquals(200, response.status)

def expectedData = [tenantId: tenantId, id: id, type: 'COUNTER']
def expectedData = [tenantId: tenantId, id: id, type: 'counter']
assertEquals(expectedData, response.data)
}

Expand Down Expand Up @@ -129,7 +129,7 @@ class CountersITest extends RESTTest {
id: id,
tags: [tag1: 'one', tag2: 'two'],
dataRetention: 100,
type: 'COUNTER'
type: 'counter'
]
assertEquals(expectedData, response.data)
}
Expand Down Expand Up @@ -168,7 +168,7 @@ class CountersITest extends RESTTest {
[
tenantId: tenantId,
id: counter1,
type: 'COUNTER'
type: 'counter'
],
[
tenantId: tenantId,
Expand All @@ -177,7 +177,7 @@ class CountersITest extends RESTTest {
tag1: 'one',
tag2: 'two'
],
type: 'COUNTER'
type: 'counter'
]
]
assertEquals(expectedData, response.data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class TagsITest extends RESTTest {
tenantId: tenantId,
id : 'N1',
tags: [' a 1 ': ' A', 'bsq d1': 'B '],
type: 'GAUGE'
type: 'gauge'
], response.data)

// Make sure we do not allow duplicates
Expand Down Expand Up @@ -217,14 +217,14 @@ class TagsITest extends RESTTest {
tenantId: tenantId,
id : 'N1',
tags : ['a1': 'A', 'd1': 'B'],
type: 'GAUGE'
type: 'gauge'
]))
assertTrue(response.data.contains([
tenantId : tenantId,
id : 'A2',
tags : [a1: 'A2', a22: '22', b22: '22'],
dataRetention: 48,
type: 'AVAILABILITY'
type: 'availability'
]))

// Fetch with tags & type
Expand All @@ -236,7 +236,7 @@ class TagsITest extends RESTTest {
tenantId: tenantId,
id : 'N1',
tags : ['a1': 'A', 'd1': 'B'],
type: 'GAUGE'
type: 'gauge'
]], response.data)

// Fetch with incorrect regexp
Expand Down

0 comments on commit 4d07950

Please sign in to comment.