use of edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.ip.IpAddressRange in project dataverse by IQSS.
the class IpGroupTest method testContains.
/**
* Test of contains method, of class IpGroup.
*/
@Test
public void testContains() {
IpGroup sut = new IpGroup();
sut.setId(MocksFactory.nextId());
sut.setDescription("A's description");
sut.setDisplayName("A");
sut.setPersistedGroupAlias("&ip/a");
final IpAddressRange allIPv4 = IpAddressRange.make(IpAddress.valueOf("0.0.0.0"), IpAddress.valueOf("255.255.255.255"));
final IpAddressRange allIPv6 = IpAddressRange.make(IpAddress.valueOf("0:0:0:0:0:0:0:0"), IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
sut.add(allIPv4);
sut.add(allIPv6);
assertTrue(sut.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("1.2.3.4"))));
assertTrue(sut.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("11::fff"))));
sut.remove(allIPv4);
assertFalse(sut.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("1.2.3.4"))));
sut.remove(allIPv6);
assertFalse(sut.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("11::fff"))));
sut.add(IpAddressRange.make(IpAddress.valueOf("0.0.0.0"), IpAddress.valueOf("168.0.0.0")));
assertFalse(sut.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("169.0.0.0"))));
assertTrue(sut.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("167.0.0.0"))));
}
use of edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.ip.IpAddressRange in project dataverse by IQSS.
the class JsonPrinter method json.
public static JsonObjectBuilder json(IpGroup grp) {
// collect single addresses
List<String> singles = grp.getRanges().stream().filter(IpAddressRange::isSingleAddress).map(IpAddressRange::getBottom).map(IpAddress::toString).collect(toList());
// collect "real" ranges
List<List<String>> ranges = grp.getRanges().stream().filter(rng -> !rng.isSingleAddress()).map(rng -> Arrays.asList(rng.getBottom().toString(), rng.getTop().toString())).collect(toList());
JsonObjectBuilder bld = jsonObjectBuilder().add("alias", grp.getPersistedGroupAlias()).add("identifier", grp.getIdentifier()).add("id", grp.getId()).add("name", grp.getDisplayName()).add("description", grp.getDescription());
if (!singles.isEmpty()) {
bld.add("addresses", asJsonArray(singles));
}
if (!ranges.isEmpty()) {
JsonArrayBuilder rangesBld = Json.createArrayBuilder();
ranges.forEach(r -> rangesBld.add(Json.createArrayBuilder().add(r.get(0)).add(r.get(1))));
bld.add("ranges", rangesBld);
}
return bld;
}
Aggregations