Skip to content

Commit

Permalink
support admin rule in batch endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
taba90 committed Jul 15, 2022
1 parent 7559b57 commit e9ffad5
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public enum ServiceName {
users,
groups,
instances,
rules
rules,
adminrules
}

public enum TypeName {
Expand Down Expand Up @@ -99,7 +100,8 @@ public void setCascade(Boolean cascade) {
@XmlElement(name="user", type=RESTInputUser.class),
@XmlElement(name="userGroup", type=RESTInputGroup.class),
@XmlElement(name="instance", type=RESTInputInstance.class),
@XmlElement(name="rule", type=RESTInputRule.class)
@XmlElement(name="rule", type=RESTInputRule.class),
@XmlElement(name="adminrule", type=RESTInputAdminRule.class),
})
public AbstractRESTPayload getPayload() {
return payload;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @author Etj (etj at geo-solutions.it)
*/
@XmlRootElement(name = "adminrule")
@XmlType(name="Rule", propOrder={"position","grant","username","rolename","instance","workspace"})
@XmlType(name="AminRule", propOrder={"position","grant","username","rolename","instance","workspace"})
public class RESTInputAdminRule extends AbstractRESTPayload
{
private RESTRulePosition position;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package org.geoserver.geofence.services.rest.impl;

import org.geoserver.geofence.services.rest.RESTAdminRuleService;
import org.geoserver.geofence.services.rest.RESTBatchService;
import org.geoserver.geofence.services.rest.RESTGSInstanceService;
import org.geoserver.geofence.services.rest.RESTRuleService;
Expand All @@ -17,6 +18,7 @@
import org.geoserver.geofence.services.rest.exception.NotFoundRestEx;
import org.geoserver.geofence.services.rest.model.RESTBatch;
import org.geoserver.geofence.services.rest.model.RESTBatchOperation;
import org.geoserver.geofence.services.rest.model.RESTInputAdminRule;
import org.geoserver.geofence.services.rest.model.RESTInputGroup;
import org.geoserver.geofence.services.rest.model.RESTInputInstance;
import org.geoserver.geofence.services.rest.model.RESTInputRule;
Expand Down Expand Up @@ -53,6 +55,7 @@ public class RESTBatchServiceImpl
private RESTUserGroupService restUserGroupService;
private RESTGSInstanceService restInstanceService;
private RESTRuleService restRuleService;
private RESTAdminRuleService restAdminRuleService;

@Transactional(value="geofenceTransactionManager")
@Override
Expand Down Expand Up @@ -90,6 +93,10 @@ public void runBatch(RESTBatch batch) throws BadRequestRestEx, NotFoundRestEx, I
dispatchRuleOp(op);
break;

case adminrules:
dispatchAdminRuleOp(op);
break;

default:
throw new BadRequestRestEx("Unhandled service for operation " + op);
}
Expand Down Expand Up @@ -237,6 +244,33 @@ else if(op.getId() != null)
}
}

protected void dispatchAdminRuleOp(RESTBatchOperation op) throws NotFoundRestEx, BadRequestRestEx {
switch (op.getType()) {
case insert:
ensurePayload(op);
restAdminRuleService.insert((RESTInputAdminRule) op.getPayload());
break;

case update:
ensurePayload(op);
if (op.getId() != null)
restAdminRuleService.update(op.getId(), (RESTInputAdminRule) op.getPayload());
else
throw new BadRequestRestEx("Missing identifier for op " + op);
break;

case delete:
if (op.getId() != null)
restAdminRuleService.delete(op.getId());
else
throw new BadRequestRestEx("Missing identifier for op " + op);
break;

default:
throw new BadRequestRestEx("Operation not bound " + op);
}
}

// ==========================================================================

private void ensurePayload(RESTBatchOperation op) throws BadRequestRestEx {
Expand Down Expand Up @@ -273,4 +307,7 @@ public void setRestUserService(RESTUserService restUserService) {
this.restUserService = restUserService;
}

public void setRestAdminRuleService(RESTAdminRuleService restAdminRuleService) {
this.restAdminRuleService = restAdminRuleService;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import org.geoserver.geofence.services.RuleAdminService;
import org.geoserver.geofence.services.dto.ShortGroup;
import org.geoserver.geofence.services.rest.RESTAdminRuleService;
import org.geoserver.geofence.services.rest.RESTBatchService;
import org.geoserver.geofence.services.rest.RESTRuleService;
import org.geoserver.geofence.services.rest.RESTUserGroupService;
import org.geoserver.geofence.services.rest.RESTUserService;
Expand Down Expand Up @@ -39,6 +41,8 @@ public abstract class RESTBaseTest {
protected static RESTUserService restUserService;
protected static RESTUserGroupService restUserGroupService;
protected static RESTRuleService restRuleService;
protected static RESTAdminRuleService restAdminRuleService;
protected static RESTBatchService restBatchService;

public RESTBaseTest() {

Expand All @@ -59,11 +63,15 @@ public RESTBaseTest() {
restUserService = (RESTUserService)ctx.getBean("restUserService");
restUserGroupService = (RESTUserGroupService)ctx.getBean("restUserGroupService");
restRuleService = (RESTRuleService)ctx.getBean("restRuleService");
restAdminRuleService = (RESTAdminRuleService)ctx.getBean("restAdminRuleService");
restBatchService = (RESTBatchService) ctx.getBean("restBatchService");
}

assertNotNull(restUserService);
assertNotNull(restUserGroupService);
assertNotNull(restRuleService);
assertNotNull(restAdminRuleService);
assertNotNull(restBatchService);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package org.geoserver.geofence.services.rest.impl;

import org.geoserver.geofence.core.model.enums.AdminGrantType;
import org.geoserver.geofence.core.model.enums.GrantType;
import org.geoserver.geofence.services.rest.exception.BadRequestRestEx;
import org.geoserver.geofence.services.rest.model.RESTBatch;
import org.geoserver.geofence.services.rest.model.RESTBatchOperation;
import org.geoserver.geofence.services.rest.model.RESTInputAdminRule;
import org.geoserver.geofence.services.rest.model.RESTInputGroup;
import org.geoserver.geofence.services.rest.model.RESTInputRule;
import org.geoserver.geofence.services.rest.model.RESTInputUser;
import org.geoserver.geofence.services.rest.model.RESTOutputRule;
import org.geoserver.geofence.services.rest.model.RESTRulePosition;
import org.geoserver.geofence.services.rest.model.util.IdName;
import org.geoserver.geofence.services.rest.model.util.RESTBatchOperationFactory;
import org.junit.Test;

import javax.ws.rs.core.Response;
import java.util.ArrayList;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

public class RESTBatchServiceImplTest extends RESTBaseTest {


@Test
public void testBatchInsertAdminRule() {
RESTInputAdminRule adminRule=new RESTInputAdminRule();
adminRule.setPosition(new RESTRulePosition(RESTRulePosition.RulePosition.offsetFromTop,0));
adminRule.setWorkspace("ws99");
adminRule.setRolename("roleName00");
adminRule.setGrant(AdminGrantType.ADMIN);
RESTBatchOperation restBatchOperation=new RESTBatchOperation();
restBatchOperation.setService(RESTBatchOperation.ServiceName.adminrules);
restBatchOperation.setType(RESTBatchOperation.TypeName.insert);
restBatchOperation.setPayload(adminRule);
RESTInputAdminRule adminRule2=new RESTInputAdminRule();
adminRule2.setPosition(new RESTRulePosition(RESTRulePosition.RulePosition.offsetFromTop,0));
adminRule2.setWorkspace("ws999");
adminRule2.setRolename("roleName00");
adminRule2.setGrant(AdminGrantType.USER);
RESTBatchOperation restBatchOperation2=new RESTBatchOperation();
restBatchOperation2.setService(RESTBatchOperation.ServiceName.adminrules);
restBatchOperation2.setType(RESTBatchOperation.TypeName.insert);
restBatchOperation2.setPayload(adminRule2);
RESTBatch restBatch=new RESTBatch();
restBatch.add(restBatchOperation);
restBatch.add(restBatchOperation2);
assertEquals(200,restBatchService.exec(restBatch).getStatus());

long result=restAdminRuleService.count(null,true,"roleName00",false,null,null,false,null,false);
assertEquals(2,result);
}

@Test
public void testBatchUpdateAdminRule() {
RESTInputAdminRule adminRule=new RESTInputAdminRule();
adminRule.setPosition(new RESTRulePosition(RESTRulePosition.RulePosition.offsetFromTop,0));
adminRule.setWorkspace("ws99");
adminRule.setRolename("roleName11");
adminRule.setGrant(AdminGrantType.ADMIN);
RESTInputAdminRule adminRule2=new RESTInputAdminRule();
adminRule2.setPosition(new RESTRulePosition(RESTRulePosition.RulePosition.offsetFromTop,0));
adminRule2.setWorkspace("ws999");
adminRule2.setRolename("roleName11");
adminRule2.setGrant(AdminGrantType.USER);
Long id=(Long)restAdminRuleService.insert(adminRule).getEntity();
Long id2=(Long)restAdminRuleService.insert(adminRule2).getEntity();
RESTBatchOperation restBatchOperation=new RESTBatchOperation();
restBatchOperation.setService(RESTBatchOperation.ServiceName.adminrules);
restBatchOperation.setType(RESTBatchOperation.TypeName.update);
restBatchOperation.setId(id);
adminRule.setWorkspace("ws00");
adminRule.setPosition(null);
restBatchOperation.setPayload(adminRule);

RESTBatchOperation restBatchOperation2=new RESTBatchOperation();
restBatchOperation2.setService(RESTBatchOperation.ServiceName.adminrules);
restBatchOperation2.setType(RESTBatchOperation.TypeName.update);
restBatchOperation2.setId(id2);
adminRule2.setWorkspace("ws000");
adminRule2.setPosition(null);
restBatchOperation2.setPayload(adminRule2);

RESTBatch batch=new RESTBatch();
batch.add(restBatchOperation);
batch.add(restBatchOperation2);
restBatchService.exec(batch);

assertEquals("ws00",restAdminRuleService.get(id).getWorkspace());
assertEquals("ws000",restAdminRuleService.get(id2).getWorkspace());

}


@Test
public void testBatchDeleteAdminRule() {
RESTInputAdminRule adminRule=new RESTInputAdminRule();
adminRule.setPosition(new RESTRulePosition(RESTRulePosition.RulePosition.offsetFromTop,0));
adminRule.setWorkspace("ws99");
adminRule.setRolename("roleName22");
adminRule.setGrant(AdminGrantType.ADMIN);
RESTInputAdminRule adminRule2=new RESTInputAdminRule();
adminRule2.setPosition(new RESTRulePosition(RESTRulePosition.RulePosition.offsetFromTop,0));
adminRule2.setWorkspace("ws999");
adminRule2.setRolename("roleName22");
adminRule2.setGrant(AdminGrantType.USER);
Long id=(Long)restAdminRuleService.insert(adminRule).getEntity();
Long id2=(Long)restAdminRuleService.insert(adminRule2).getEntity();
RESTBatchOperation restBatchOperation=new RESTBatchOperation();
restBatchOperation.setService(RESTBatchOperation.ServiceName.adminrules);
restBatchOperation.setType(RESTBatchOperation.TypeName.delete);
restBatchOperation.setId(id);

RESTBatchOperation restBatchOperation2=new RESTBatchOperation();
restBatchOperation2.setService(RESTBatchOperation.ServiceName.adminrules);
restBatchOperation2.setType(RESTBatchOperation.TypeName.delete);
restBatchOperation2.setId(id2);

RESTBatch batch=new RESTBatch();
batch.add(restBatchOperation);
batch.add(restBatchOperation2);
restBatchService.exec(batch);
long result=restAdminRuleService.count(null,true,"roleName22",false,null,null,false,null,false);
assertEquals(0,result);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ public void testAllowedAreaSRID() {


}

@Test
public void testLimits() {

Expand Down

0 comments on commit e9ffad5

Please sign in to comment.