Skip to content

Commit

Permalink
Add support to patch ResourceGroupService
Browse files Browse the repository at this point in the history
Change-Id: I31aa2734fa1a268d81a698f4e257ac970aff45aa
  • Loading branch information
Gurudutt Belur authored and Gerrit Code Review committed Aug 10, 2016
1 parent ea17081 commit 354c7b3
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.vmware.xenon.services.common;

import java.util.EnumSet;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

import com.vmware.xenon.common.Operation;
Expand Down Expand Up @@ -279,4 +280,28 @@ private static boolean isAuthzCacheClearApplicableOperation(Operation op) {
}
return true;
}

/**
* Method to remove a clause from a query. This method bases
* an equality check of the clause on the term and occurance
* fields in the query. Any boolean clauses in the query are
* not considered
* @param inputQuery The input query to remove the clause
* @param clause The clause to remove
* @return
*/
public static Query removeBooleanClause(Query inputQuery, Query inputClause) {
if (inputQuery.booleanClauses == null || inputClause == null) {
return inputQuery;
}
for (Query clause : inputQuery.booleanClauses) {
if (Objects.equals(clause.term, inputClause.term)
&& Objects.equals(clause.occurance, inputClause.occurance)) {
inputQuery.booleanClauses.remove(clause);
break;
}
}
return inputQuery;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Collection;
import java.util.EnumSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import com.vmware.xenon.common.ServiceDocument;
Expand Down Expand Up @@ -393,6 +394,27 @@ public static NumericRange<?> createEqualRange(Number num) {
}
return createLongRange((Long) num, (Long) num, true, true);
}

@Override
public boolean equals(Object obj) {
if (obj != null && obj instanceof NumericRange<?>) {
NumericRange<?> nObj = (NumericRange<?>)obj;
if ((this.isMaxInclusive == nObj.isMaxInclusive)
&& (this.isMinInclusive == nObj.isMinInclusive)
&& Objects.equals(this.max, nObj.max)
&& Objects.equals(this.min, nObj.min)
&& Objects.equals(this.precisionStep, nObj.precisionStep)
&& Objects.equals(this.type, nObj.type)) {
return true;
}
}
return false;
}

@Override
public int hashCode() {
return Objects.hash(this.max, this.min, this.precisionStep, this.type);
}
}

public static class QueryTerm {
Expand All @@ -405,6 +427,26 @@ public enum MatchType {
public String matchValue;
public MatchType matchType;
public NumericRange<?> range;

@Override
public boolean equals(Object obj) {
if (obj != null && obj instanceof QueryTerm) {
QueryTerm qObj = (QueryTerm)obj;
if (Objects.equals(this.propertyName, qObj.propertyName)
&& Objects.equals(this.propertyType, qObj.propertyType)
&& Objects.equals(this.matchType, qObj.matchType)
&& Objects.equals(this.matchValue, qObj.matchValue)
&& Objects.equals(this.range, qObj.range)) {
return true;
}
}
return false;
}

@Override
public int hashCode() {
return Objects.hash(this.propertyName, this.propertyType, this.matchType, this.matchValue, this.range);
}
}

public static class Query {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

package com.vmware.xenon.services.common;

import java.util.Objects;

import com.vmware.xenon.common.FactoryService;
import com.vmware.xenon.common.Operation;
import com.vmware.xenon.common.Service;
Expand Down Expand Up @@ -60,6 +62,23 @@ public ResourceGroupService() {
super.toggleOption(ServiceOption.OWNER_SELECTION, true);
}

public static class PatchQueryRequest {
public static final String KIND = Utils.buildKind(PatchQueryRequest.class);
public Query clause;
public boolean removeClause = false;
public String kind;

private PatchQueryRequest(Query clause, boolean removeClause) {
this.clause = clause;
this.removeClause = removeClause;
this.kind = KIND;
}

public static PatchQueryRequest create(Query clause, boolean removeClause) {
return new PatchQueryRequest(clause, removeClause);
}
}

@Override
public void handleRequest(Operation request, OperationProcessingStage opProcessingStage) {
if (request.getAction() == Action.DELETE || request.getAction() == Action.PUT) {
Expand Down Expand Up @@ -113,6 +132,31 @@ public void handlePut(Operation op) {
op.complete();
}

@Override
public void handlePatch(Operation op) {
if (!op.hasBody()) {
op.fail(new IllegalArgumentException("body is required"));
return;
}
ResourceGroupState currentState = getState(op);
PatchQueryRequest patchQuery = op.getBody(PatchQueryRequest.class);
if (!Objects.equals(patchQuery.kind, PatchQueryRequest.KIND)) {
op.fail(new IllegalArgumentException("kind should be : " + PatchQueryRequest.KIND));
return;
}
if (patchQuery.clause == null) {
op.fail(new IllegalArgumentException("clause is required"));
return;
}
if (patchQuery.removeClause) {
AuthorizationCacheUtils.removeBooleanClause(currentState.query, patchQuery.clause);
} else {
currentState.query.addBooleanClause(patchQuery.clause);
}
op.setBody(currentState);
op.complete();
}

private boolean validate(Operation op, ResourceGroupState state) {
if (state.query == null) {
op.fail(new IllegalArgumentException("query is required"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@

import com.vmware.xenon.common.BasicReusableHostTestCase;
import com.vmware.xenon.common.Operation;
import com.vmware.xenon.common.Service.Action;
import com.vmware.xenon.common.UriUtils;
import com.vmware.xenon.services.common.QueryTask.NumericRange;
import com.vmware.xenon.services.common.QueryTask.Query;
import com.vmware.xenon.services.common.ResourceGroupService.PatchQueryRequest;
import com.vmware.xenon.services.common.ResourceGroupService.ResourceGroupState;

public class TestResourceGroupService extends BasicReusableHostTestCase {
Expand Down Expand Up @@ -65,6 +68,35 @@ public void testIdempotentPost() throws Throwable {
postHelper(state);
}

@Test
public void testPatch() throws Throwable {
ResourceGroupState state = new ResourceGroupState();
state.documentSelfLink = "test-patch";
state.query = new Query();
state.query.setTermPropertyName("name");
state.query.setTermMatchValue("value");
ResourceGroupState returnState = this.postOrPatchHelper(state, Action.POST, this.factoryUri);

// issue a PATCH with a query clause to add
Query clause = new Query();
clause.setTermPropertyName("patch-name");
clause.setTermMatchValue("patch-value");
clause.setNumericRange(NumericRange.createEqualRange(1L));
PatchQueryRequest queryPatch = PatchQueryRequest.create(clause, false);
returnState = this.postOrPatchHelper(queryPatch, Action.PATCH, UriUtils.buildUri(this.host, returnState.documentSelfLink));
assertEquals(returnState.query.booleanClauses.size(), 1);

// issue a PATCH with a query clause to remove an entry that does not exist
queryPatch.clause.setNumericRange(NumericRange.createEqualRange(2L));
queryPatch.removeClause = true;
returnState = this.postOrPatchHelper(queryPatch, Action.PATCH, UriUtils.buildUri(this.host, returnState.documentSelfLink));
assertEquals(returnState.query.booleanClauses.size(), 1);

queryPatch.clause.setNumericRange(NumericRange.createEqualRange(1L));
returnState = this.postOrPatchHelper(queryPatch, Action.PATCH, UriUtils.buildUri(this.host, returnState.documentSelfLink));
assertEquals(returnState.query.booleanClauses.size(), 0);
}

@Test
public void testFactoryIdempotentPost() throws Throwable {
ResourceGroupState state = new ResourceGroupState();
Expand Down Expand Up @@ -131,16 +163,26 @@ public void testFactoryPostFailure() throws Throwable {
}

private void postHelper(ResourceGroupState state) throws Throwable {
ResourceGroupState returnState = postOrPatchHelper(state, Action.POST, this.factoryUri);
assertEquals(state.query.term.propertyName, returnState.query.term.propertyName);
assertEquals(state.query.term.matchValue, returnState.query.term.matchValue);
}

private ResourceGroupState postOrPatchHelper(Object state, Action action, URI uri) throws Throwable {
ResourceGroupState[] outState = new ResourceGroupState[1];

Operation op = Operation.createPost(this.factoryUri)
.setBody(state)
Operation op = null;
if (action.equals(Action.PATCH)) {
op = Operation.createPatch(uri);
} else {
op = Operation.createPost(uri);
}
op.setBody(state)
.setCompletion((o, e) -> {
if (e != null) {
this.host.failIteration(e);
return;
}

outState[0] = o.getBody(ResourceGroupState.class);
this.host.completeIteration();
});
Expand All @@ -149,7 +191,6 @@ private void postHelper(ResourceGroupState state) throws Throwable {
this.host.send(op);
this.host.testWait();

assertEquals(state.query.term.propertyName, outState[0].query.term.propertyName);
assertEquals(state.query.term.matchValue, outState[0].query.term.matchValue);
return outState[0];
}
}

0 comments on commit 354c7b3

Please sign in to comment.