Skip to content

Commit

Permalink
[GEOS-8863] [Community GeoFence] GeoFence Embedded REST endpoints cau…
Browse files Browse the repository at this point in the history
…se NPE when Limits have been set on Layers
  • Loading branch information
Alessio Fabiani committed Aug 13, 2018
1 parent 9212a4b commit cbaf449
Show file tree
Hide file tree
Showing 11 changed files with 495 additions and 182 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
Expand Down Expand Up @@ -180,8 +179,9 @@ public JaxbRuleList count(
return new JaxbRuleList(adminService.count(filter));
}

@PostMapping(
value = {"/rules"},
@RequestMapping(
value = "/rules",
method = RequestMethod.POST,
consumes = {
MediaType.TEXT_XML_VALUE,
MediaType.APPLICATION_XML_VALUE,
Expand All @@ -198,10 +198,10 @@ public ResponseEntity<Long> insert(@RequestBody(required = true) JaxbRule rule)

Long id = adminService.insert(rule.toRule());

if (rule.getLimits() != null) {
if (rule.getLimits() != null && rule.getAccess().equals("LIMIT")) {
adminService.setLimits(id, rule.getLimits().toRuleLimits(null));
}
if (rule.getLayerDetails() != null) {
if (rule.getLayerDetails() != null && !rule.getAccess().equals("LIMIT")) {
adminService.setDetails(id, rule.getLayerDetails().toLayerDetails(null));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,10 +594,12 @@ protected void onUpdate(AjaxRequestTarget target) {

ruleFormModel.getObject().layerDetailsCheck =
ruleFormModel.getObject().layerDetailsCheck
&& grantTypeChoice
.getConvertedInput()
.equals(GrantType.ALLOW)
&& !layerChoice.getConvertedInput().isEmpty();
&& (grantTypeChoice.getConvertedInput() != null
&& grantTypeChoice
.getConvertedInput()
.equals(GrantType.ALLOW))
&& (layerChoice.getConvertedInput() != null
&& !layerChoice.getConvertedInput().isEmpty());
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,49 @@

package org.geoserver.geofence.internal;

import static org.junit.Assert.assertTrue;

import org.geoserver.geofence.ServicesTest;
import org.geoserver.geofence.core.model.Rule;
import org.geoserver.geofence.core.model.enums.GrantType;
import org.geoserver.geofence.server.rest.RulesRestController;
import org.geoserver.geofence.services.RuleAdminService;
import org.geoserver.geofence.services.RuleReaderServiceImpl;
import org.geoserver.geofence.services.dto.ShortRule;
import org.junit.Before;
import org.junit.Test;

/**
* *
*
* @author Niels Charlier
*/
/** @author Niels Charlier */
public class InternalServicesTest extends ServicesTest {

@Override
public void oneTimeSetUp() throws Exception {
setValidating(true);
super.oneTimeSetUp();

RuleAdminService adminService =
(RuleAdminService) applicationContext.getBean("ruleAdminService");
adminService.insert(
new Rule(0, "cite", null, null, null, "wms", null, "cite", null, GrantType.ALLOW));
adminService.insert(
new Rule(1, "cite", null, null, null, "wms", null, "sf", null, GrantType.ALLOW));
protected RulesRestController controller;

protected RuleAdminService adminService;

@Before
public void initGeoFenceControllers() {
controller = (RulesRestController) applicationContext.getBean("rulesRestController");
adminService = (RuleAdminService) applicationContext.getBean("ruleAdminService");

if (adminService.getCountAll() > 0) {
for (ShortRule r : adminService.getAll()) {
adminService.delete(r.getId());
}
}

Rule citeRule =
new Rule(0, "cite", null, null, null, "wms", null, "cite", null, GrantType.ALLOW);
Rule sfRule =
new Rule(1, "cite", null, null, null, "wms", null, "sf", null, GrantType.ALLOW);
adminService.insert(citeRule);
adminService.insert(sfRule);
}

@Test
public void testConfigurationInternal() {

assertTrue(configManager.getConfiguration().isInternal());

assertTrue(geofenceService instanceof RuleReaderServiceImpl);
if (geofenceService != null) {
assertTrue(geofenceService instanceof RuleReaderServiceImpl);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.util.UUID;
import org.geoserver.geofence.GeofenceBaseTest;
Expand All @@ -19,6 +21,7 @@
import org.geoserver.geofence.server.rest.AdminRulesRestController;
import org.geoserver.geofence.services.AdminRuleAdminService;
import org.geoserver.geofence.services.exception.NotFoundServiceEx;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -29,10 +32,8 @@ public class AdminRulesRestControllerTest extends GeofenceBaseTest {

protected AdminRuleAdminService adminService;

@Override
public void oneTimeSetUp() throws Exception {
setValidating(true);
super.oneTimeSetUp();
@Before
public void initGeoFenceControllers() {
controller =
(AdminRulesRestController) applicationContext.getBean("adminRulesRestController");
adminService = (AdminRuleAdminService) applicationContext.getBean("adminRuleAdminService");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.util.UUID;
import org.geoserver.geofence.GeofenceBaseTest;
Expand All @@ -21,7 +25,10 @@
import org.geoserver.geofence.services.RuleAdminService;
import org.geoserver.geofence.services.exception.NotFoundServiceEx;
import org.geotools.gml3.bindings.GML3MockData;
import org.junit.Before;
import org.junit.Test;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

Expand All @@ -31,10 +38,8 @@ public class RulesRestControllerTest extends GeofenceBaseTest {

protected RuleAdminService adminService;

@Override
public void oneTimeSetUp() throws Exception {
setValidating(true);
super.oneTimeSetUp();
@Before
public void initGeoFenceControllers() {
controller = (RulesRestController) applicationContext.getBean("rulesRestController");
adminService = (RuleAdminService) applicationContext.getBean("ruleAdminService");
}
Expand All @@ -43,7 +48,7 @@ public void oneTimeSetUp() throws Exception {
public void testInsertUpdateDelete() {
JaxbRule rule = new JaxbRule();
rule.setPriority(5L);
rule.setUserName("pipo");
rule.setUserName("pippo");
rule.setRoleName("clown");
rule.setAddressRange("127.0.0.1/32");
rule.setService("wfs");
Expand All @@ -52,6 +57,49 @@ public void testInsertUpdateDelete() {
rule.setLayer("layer");
rule.setAccess("ALLOW");

JaxbRuleList rules =
controller.count(
"pippo",
false,
"clown",
false,
null,
null,
false,
"wfs",
false,
"getFeature",
false,
"workspace",
false,
"layer",
false);
if (rules.getCount() > 0) {
rules =
controller.get(
0,
(int) rules.getCount(),
true,
"pippo",
false,
"clown",
false,
null,
null,
false,
"wfs",
false,
"getFeature",
false,
"workspace",
false,
"layer",
false);
for (JaxbRule r : rules.getRules()) {
controller.delete(r.getId());
}
}

long id = controller.insert(rule).getBody();

Rule realRule = adminService.get(id);
Expand Down Expand Up @@ -128,7 +176,7 @@ public void testInsertUpdateDelete() {
public void testLimits() {
JaxbRule rule = new JaxbRule();
rule.setPriority(5L);
rule.setUserName("pipo");
rule.setUserName("pippo");
rule.setRoleName("clown");
rule.setAddressRange("127.0.0.1/32");
rule.setService("wfs");
Expand All @@ -140,14 +188,63 @@ public void testLimits() {
rule.getLimits().setAllowedArea(GML3MockData.multiPolygon());
rule.getLimits().setCatalogMode("MIXED");

JaxbRuleList rules =
controller.count(
"pippo",
false,
"clown",
false,
null,
null,
false,
"wfs",
false,
"getFeature",
false,
"workspace",
false,
"layer",
false);
if (rules.getCount() > 0) {
rules =
controller.get(
0,
(int) rules.getCount(),
true,
"pippo",
false,
"clown",
false,
null,
null,
false,
"wfs",
false,
"getFeature",
false,
"workspace",
false,
"layer",
false);
for (JaxbRule r : rules.getRules()) {
controller.delete(r.getId());
}
}

Long id = controller.insert(rule).getBody();

Rule realRule = adminService.get(id);

assertEquals(
rule.getLimits().getCatalogMode(),
realRule.getRuleLimits().getCatalogMode().toString());
assertEquals(rule.getLimits().getAllowedArea(), realRule.getRuleLimits().getAllowedArea());
try {
assertEquals(
new WKTReader().read(rule.getLimits().getAllowedArea()),
realRule.getRuleLimits().getAllowedArea());
} catch (ParseException e) {
assertFalse(e.getLocalizedMessage(), true);
}

rule.getLimits().setCatalogMode("HIDE");

Expand Down Expand Up @@ -200,11 +297,59 @@ public void testLayerDetails() {
rule.getLayerDetails().setDefaultStyle("myDefaultStyle");
rule.getLayerDetails().setLayerType("VECTOR");

JaxbRuleList rules =
controller.count(
"pippo",
false,
"clown",
false,
null,
null,
false,
"wfs",
false,
"getFeature",
false,
"workspace",
false,
"layer",
false);
if (rules.getCount() > 0) {
rules =
controller.get(
0,
(int) rules.getCount(),
true,
"pippo",
false,
"clown",
false,
null,
null,
false,
"wfs",
false,
"getFeature",
false,
"workspace",
false,
"layer",
false);
for (JaxbRule r : rules.getRules()) {
controller.delete(r.getId());
}
}

Long id = controller.insert(rule).getBody();

Rule realRule = adminService.get(id);

assertEquals(rule.getLayerDetails().getAllowedArea(), realRule.getLayerDetails().getArea());
try {
assertEquals(
new WKTReader().read(rule.getLayerDetails().getAllowedArea()),
realRule.getLayerDetails().getArea());
} catch (ParseException e) {
assertFalse(e.getLocalizedMessage(), true);
}
assertEquals(
rule.getLayerDetails().getCatalogMode(),
realRule.getLayerDetails().getCatalogMode().toString());
Expand Down Expand Up @@ -255,8 +400,8 @@ public void testLayerDetails() {
rule.getLayerDetails().getDefaultStyle(),
realRule.getLayerDetails().getDefaultStyle());

assertEquals(3, realRule.getLayerDetails().getAllowedStyles().size());
assertEquals(3, realRule.getLayerDetails().getAttributes().size());
assertEquals(1, realRule.getLayerDetails().getAllowedStyles().size());
assertEquals(2, realRule.getLayerDetails().getAttributes().size());

for (LayerAttribute la : realRule.getLayerDetails().getAttributes()) {
if (la.getName().equals("layerAttribute2")) {
Expand Down

0 comments on commit cbaf449

Please sign in to comment.