Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add savable filters of checks table #393

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* 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 com.seyren.acceptancetests;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.restdriver.serverdriver.http.response.Response;
import org.junit.Test;

import static com.github.restdriver.serverdriver.Matchers.hasJsonPath;
import static com.github.restdriver.serverdriver.Matchers.hasStatusCode;
import static com.github.restdriver.serverdriver.RestServerDriver.*;
import static com.seyren.acceptancetests.util.SeyrenDriver.checks;
import static com.seyren.acceptancetests.util.SeyrenDriver.filters;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

public class FiltersAT {

@Test
public void testFiltersShouldBeSavedAndRetrievedAndDeleted() {
String body = "{ \"name\" : \"TestFilter\", \"filter\" : \"app.mx.test.rate5\" }";
Response response = post(filters(), body(body, "application/json"));
assertThat(response, hasStatusCode(201));

body = "{ \"name\" : \"TestFilter2\", \"filter\" : \"app.ar.test.rate5\" }";
response = post(filters(), body(body, "application/json"));
assertThat(response, hasStatusCode(201));

Response newResponse = get(filters());
assertThat(newResponse, hasStatusCode(200));
JsonNode responseJson = newResponse.asJson();
assertThat(responseJson.findValues("id").size(), greaterThanOrEqualTo(2));

String idToBeDeleted = responseJson.findValues("id").get(0).asText();
response = delete(new StringBuilder().append(filters()).append("/").append(idToBeDeleted));
assertThat(response, hasStatusCode(204));

newResponse = get(filters());
responseJson = newResponse.asJson();
assertThat(newResponse, hasStatusCode(200));
assertThat(responseJson.findValues("id").size(), is(1));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ public static Url check(String checkId) {
public static Url alerts(String checkId) {
return check(checkId).withPath("/alerts");
}


public static Url filters() {
return baseUri().withPath("checks/filters");
}

public static Url subscriptions(String checkId) {
return check(checkId).withPath("/subscriptions");
}
Expand Down
58 changes: 58 additions & 0 deletions seyren-api/src/main/java/com/seyren/api/bean/FiltersBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* 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 com.seyren.api.bean;

import com.seyren.api.jaxrs.FiltersResource;
import com.seyren.core.domain.Filter;
import com.seyren.core.domain.SeyrenResponse;
import com.seyren.core.store.FiltersStore;

import javax.inject.Inject;
import javax.inject.Named;
import javax.ws.rs.core.Response;
import java.net.URI;
import java.net.URISyntaxException;

@Named
public class FiltersBean implements FiltersResource {

@Inject
private FiltersStore filtersStore;

@Override
public Response getFilters() {
SeyrenResponse<Filter> filter = filtersStore.getFilters();
return Response.ok(filter).build();
}

@Override
public Response saveFilter(Filter filter) {
Filter stored = filtersStore.createFilter(filter);
return Response.created(uri(stored.getId())).build();
}

@Override
public Response deleteFilter(String filterId) {
filtersStore.deleteFilter(filterId);
return Response.noContent().build();
}

private URI uri(String filterId) {
try {
return new URI("checks/filters/" + filterId);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
}
39 changes: 39 additions & 0 deletions seyren-api/src/main/java/com/seyren/api/jaxrs/FiltersResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* 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 com.seyren.api.jaxrs;

import com.seyren.core.domain.Filter;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/")
public interface FiltersResource {

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/checks/filters")
Response getFilters();

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Path("/checks/filters")
Response saveFilter(Filter filter);

@DELETE
@Path("/checks/filters/{filterId}")
Response deleteFilter(@PathParam("filterId") String filterId);

}
70 changes: 70 additions & 0 deletions seyren-core/src/main/java/com/seyren/core/domain/Filter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* 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 com.seyren.core.domain;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
* This class represents a saved filter
*
* @author tamas
*
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class Filter {

private String id;
private String name;
private String filter;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public Filter withId(String id) {
setId(id);
return this;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Filter withName(String name) {
setName(name);
return this;
}

public String getFilter() {
return filter;
}

public void setFilter(String filter) {
this.filter = filter;
}

public Filter withFilter(String filter) {
this.filter = filter;
return this;
}

}
40 changes: 40 additions & 0 deletions seyren-core/src/main/java/com/seyren/core/store/FiltersStore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* 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.
*/
/**
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 com.seyren.core.store;

import com.seyren.core.domain.Filter;
import com.seyren.core.domain.SeyrenResponse;

public interface FiltersStore {

Filter createFilter(Filter filter);

SeyrenResponse<Filter> getFilters();

void deleteFilter(String filterId);

}
47 changes: 31 additions & 16 deletions seyren-mongo/src/main/java/com/seyren/mongo/MongoMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,17 @@
*/
package com.seyren.mongo;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.common.base.Strings;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.seyren.core.domain.*;
import org.bson.types.ObjectId;
import org.joda.time.DateTime;
import org.joda.time.LocalTime;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.seyren.core.domain.Alert;
import com.seyren.core.domain.AlertType;
import com.seyren.core.domain.Check;
import com.seyren.core.domain.Subscription;
import com.seyren.core.domain.SubscriptionType;
import java.math.BigDecimal;
import java.util.*;

public class MongoMapper {

Expand Down Expand Up @@ -131,7 +122,18 @@ public Alert alertFrom(DBObject dbo) {
.withToType(toType)
.withTimestamp(timestamp);
}


public Filter filterFrom(DBObject dbo) {
String id = dbo.get("_id").toString();
String name = getString(dbo, "name");
String filter = getString(dbo, "filter");

return new Filter().withId(id)
.withName(name)
.withFilter(filter);
}


public DBObject checkToDBObject(Check check) {
return new BasicDBObject(propertiesToMap(check));
}
Expand All @@ -143,6 +145,10 @@ public DBObject subscriptionToDBObject(Subscription subscription) {
public DBObject alertToDBObject(Alert alert) {
return new BasicDBObject(propertiesToMap(alert));
}

public DBObject filterToDBObject(Filter filter) {
return new BasicDBObject(propertiesToMap(filter));
}

@SuppressWarnings({ "unchecked", "rawtypes" })
private Map propertiesToMap(Check check) {
Expand Down Expand Up @@ -232,6 +238,15 @@ private Map propertiesToMap(Alert alert) {
map.put("timestamp", new Date(alert.getTimestamp().getMillis()));
return map;
}

@SuppressWarnings({"unchecked", "rawtypes"})
private Map propertiesToMap(Filter filter) {
Map map = new HashMap();
map.put("_id", filter.getId());
map.put("name", filter.getName());
map.put("filter", filter.getFilter());
return map;
}

private boolean getBoolean(DBObject dbo, String key) {
return (Boolean) dbo.get(key);
Expand Down
Loading