Skip to content

Commit

Permalink
[HWKMETRICS-83] Add a simple test for the bus MetricsService implemen…
Browse files Browse the repository at this point in the history
…tation. Also adjust core domain models to be JSON serializable/deserializable.
  • Loading branch information
Stefan Negrea committed Jul 17, 2015
1 parent e2f4780 commit 308268d
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
6 changes: 6 additions & 0 deletions core/metrics-core-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
<artifactId>cassandra-driver-core</artifactId>
</dependency>

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<scope>provided</scope>
</dependency>

<!-- test -->

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public class Metric<T> {
private Integer dataRetention;
private List<DataPoint<T>> dataPoints = new ArrayList<>();

public Metric() {
}

public Metric(String tenantId, MetricType type, MetricId id) {
this.tenantId = tenantId;
this.type = type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.hawkular.metrics.core.api;

import com.fasterxml.jackson.annotation.JsonValue;
import com.google.common.base.Objects;

/**
Expand All @@ -36,6 +37,7 @@ public MetricId(String name, Interval interval) {
this.interval = interval;
}

@JsonValue
public String getName() {
return name;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2014-2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.metrics.core.bus;

import org.hawkular.bus.common.ConnectionContextFactory;
import org.hawkular.bus.common.Endpoint;
import org.hawkular.bus.common.Endpoint.Type;
import org.hawkular.bus.common.MessageProcessor;
import org.hawkular.bus.common.ObjectMessage;
import org.hawkular.bus.common.consumer.ConsumerConnectionContext;
import org.hawkular.bus.common.test.SimpleTestListener;
import org.hawkular.bus.common.test.VMEmbeddedBrokerWrapper;
import org.hawkular.metrics.core.api.Metric;
import org.hawkular.metrics.core.api.MetricId;
import org.hawkular.metrics.core.api.MetricType;
import org.hawkular.metrics.core.api.MetricsService;
import org.junit.Test;
import org.testng.Assert;

import rx.observers.TestSubscriber;

/**
* @author Stefan Negrea
*/
public class BusMessageSenderTest {

@Test
public void testQueryOrderedAsc() throws Exception {
VMEmbeddedBrokerWrapper broker = new VMEmbeddedBrokerWrapper();
broker.start();

// create topic listener
String brokerURL = broker.getBrokerURL();
ConnectionContextFactory consumerFactory = new ConnectionContextFactory(brokerURL);
Endpoint endpoint = new Endpoint(Type.TOPIC, BusMessageSender.GAUGE_METRICS_TOPIC);
ConsumerConnectionContext consumerContext = consumerFactory.createConsumerConnectionContext(endpoint);
SimpleTestListener<ObjectMessage> listener = new SimpleTestListener<ObjectMessage>(ObjectMessage.class);
MessageProcessor serverSideProcessor = new MessageProcessor();
serverSideProcessor.listen(consumerContext, listener);


// create bus message creator service
MetricsService serviceUnderTest = new MetricsServiceBusDelivery(consumerFactory);

// push a message and listen for it
TestSubscriber<Void> readSubscriber = new TestSubscriber<>();
Metric<Double> sampleMetric = new Metric<>("123", MetricType.GAUGE, new MetricId("test"));
serviceUnderTest.createMetric(sampleMetric).subscribe(readSubscriber);
readSubscriber.awaitTerminalEvent();
readSubscriber.assertNoErrors();

listener.waitForMessage(3);

// verify results
ObjectMessage receivedMessage = listener.getReceivedMessage();
Assert.assertNotNull(receivedMessage, "Should have received the message.");

receivedMessage.setObjectClass(Metric.class);
Metric<Double> receivedMetric = (Metric<Double>) receivedMessage.getObject();
Assert.assertNotNull(receivedMetric, "Should have received the message with a metric payload.");

broker.stop();
}
}

0 comments on commit 308268d

Please sign in to comment.