Skip to content

Commit

Permalink
Updated AvailabilityHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
tsegismont committed Jul 20, 2015
1 parent 136f345 commit 5ad11f2
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@

import static java.util.concurrent.TimeUnit.HOURS;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;

import static org.hawkular.metrics.api.jaxrs.filter.TenantFilter.TENANT_HEADER_NAME;
import static org.hawkular.metrics.api.jaxrs.util.ApiUtils.badRequest;
import static org.hawkular.metrics.api.jaxrs.util.ApiUtils.emptyPayload;
import static org.hawkular.metrics.api.jaxrs.util.ApiUtils.noContent;
import static org.hawkular.metrics.api.jaxrs.util.ApiUtils.requestToAvailabilities;
import static org.hawkular.metrics.api.jaxrs.util.ApiUtils.requestToAvailabilityDataPoints;
import static org.hawkular.metrics.api.jaxrs.util.ApiUtils.serverError;
import static org.hawkular.metrics.api.jaxrs.util.ApiUtils.valueToResponse;
import static org.hawkular.metrics.core.api.MetricType.AVAILABILITY;

import java.net.URI;
Expand All @@ -48,11 +53,6 @@
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import com.wordnik.swagger.annotations.ApiResponse;
import com.wordnik.swagger.annotations.ApiResponses;
import org.hawkular.metrics.api.jaxrs.ApiError;
import org.hawkular.metrics.api.jaxrs.handler.observer.MetricCreatedObserver;
import org.hawkular.metrics.api.jaxrs.handler.observer.ResultSetObserver;
Expand All @@ -71,6 +71,13 @@
import org.hawkular.metrics.core.api.MetricsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import com.wordnik.swagger.annotations.ApiResponse;
import com.wordnik.swagger.annotations.ApiResponses;

import rx.Observable;

/**
Expand Down Expand Up @@ -132,8 +139,8 @@ public void getAvailabilityMetric(@Suspended final AsyncResponse asyncResponse,
metricsService.findMetric(tenantId, AVAILABILITY, new MetricId(id))
.map(MetricDefinition::new)
.map(metricDef -> Response.ok(metricDef).build())
.switchIfEmpty(Observable.just(ApiUtils.noContent()))
.subscribe(asyncResponse::resume, t -> asyncResponse.resume(ApiUtils.serverError(t)));
.switchIfEmpty(Observable.just(noContent()))
.subscribe(asyncResponse::resume, t -> asyncResponse.resume(serverError(t)));
}

@GET
Expand All @@ -150,8 +157,8 @@ public void getAvailabilityMetricTags(
@PathParam("id") String id
) {
metricsService.getMetricTags(tenantId, AVAILABILITY, new MetricId(id)).subscribe(
optional -> asyncResponse.resume(ApiUtils.valueToResponse(optional)),
t -> asyncResponse.resume(ApiUtils.serverError(t)));
optional -> asyncResponse.resume(valueToResponse(optional)),
t -> asyncResponse.resume(serverError(t)));
}

@PUT
Expand Down Expand Up @@ -199,8 +206,8 @@ public void addAvailabilityForMetric(
@Suspended final AsyncResponse asyncResponse, @PathParam("id") String id,
@ApiParam(value = "List of availability datapoints", required = true) List<AvailabilityDataPoint> data
) {
if (data == null) {
asyncResponse.resume(ApiUtils.emptyPayload());
if (data == null || data.isEmpty()) {
asyncResponse.resume(emptyPayload());
} else {
Metric<AvailabilityType> metric = new Metric<>(tenantId,
AVAILABILITY, new MetricId(id), requestToAvailabilityDataPoints(data));
Expand All @@ -222,8 +229,8 @@ public void addAvailabilityData(
@ApiParam(value = "List of availability metrics", required = true)
List<Availability> availabilities
) {
if (availabilities.isEmpty()) {
asyncResponse.resume(ApiUtils.emptyPayload());
if (availabilities == null || availabilities.isEmpty()) {
asyncResponse.resume(emptyPayload());
} else {
metricsService.addAvailabilityData(requestToAvailabilities(tenantId, availabilities))
.subscribe(new ResultSetObserver(asyncResponse));
Expand Down Expand Up @@ -293,7 +300,7 @@ public void findAvailabilityData(
asyncResponse::resume,
t -> {
logger.warn("Failed to fetch availability data", t);
ApiUtils.serverError(t);
serverError(t);
});
} else if (bucketsCount != null && bucketDuration != null) {
asyncResponse.resume(badRequest(new ApiError("Both buckets and bucketDuration parameters are used")));
Expand All @@ -320,12 +327,19 @@ public void findAvailabilityData(
@POST
@Path("/{id}/tag")
@ApiOperation(value = "Add or update availability metric's tags.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "Tags were modified successfully.") })
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Tags were modified successfully."),
@ApiResponse(code = 400, message = "Missing or invalid payload", response = ApiError.class),
})
public void tagAvailabilityData(
@Suspended final AsyncResponse asyncResponse,
@PathParam("id") final String id,
@ApiParam(required = true) TagRequest params
) {
if (params == null) {
asyncResponse.resume(emptyPayload());
return;
}
Observable<Void> resultSetObservable;
Metric<AvailabilityType> metric = new Metric<>(tenantId, AVAILABILITY, new MetricId(id));
if (params.getTimestamp() != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* 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.rest

import org.junit.Test

import static org.junit.Assert.assertEquals

/**
* @author Thomas Segismont
*/
class AvailabilityITest extends RESTTest {

@Test
void shouldNotCreateMetricWithEmptyPayload() {
def tenantId = nextTenantId()

badPost(path: "availability", headers: [(tenantHeaderName): tenantId], body: "" /* Empty Body */) { exception ->
assertEquals(400, exception.response.status)
}
}

@Test
void shouldNotAddAvailabilityForMetricWithEmptyPayload() {
def tenantId = nextTenantId()

badPost(path: "availability/pimpo/data", headers: [(tenantHeaderName): tenantId],
body: "" /* Empty Body */) { exception ->
assertEquals(400, exception.response.status)
}

badPost(path: "availability/pimpo/data", headers: [(tenantHeaderName): tenantId],
body: [] /* Empty List */) { exception ->
assertEquals(400, exception.response.status)
}
}

@Test
void shouldNotAddAvailabilityDataWithEmptyPayload() {
def tenantId = nextTenantId()

badPost(path: "availability/data", headers: [(tenantHeaderName): tenantId],
body: "" /* Empty Body */) { exception ->
assertEquals(400, exception.response.status)
}

badPost(path: "availability/data", headers: [(tenantHeaderName): tenantId],
body: [] /* Empty List */) { exception ->
assertEquals(400, exception.response.status)
}
}

@Test
void shouldNotTagAvailabilityDataWithEmptyPayload() {
def tenantId = nextTenantId()

badPost(path: "availability/pimpo/tag", headers: [(tenantHeaderName): tenantId],
body: "" /* Empty Body */) { exception ->
assertEquals(400, exception.response.status)
}
}

}

0 comments on commit 5ad11f2

Please sign in to comment.