Search in sources :

Example 1 with CannotApplyPatchException

use of org.locationtech.geogig.api.porcelain.CannotApplyPatchException in project GeoGig by boundlessgeo.

the class ApplyPatchOpTest method testModifiedFeatureDoesNotExists.

@Test
public void testModifiedFeatureDoesNotExists() throws Exception {
    Patch patch = new Patch();
    String path = NodeRef.appendChild(pointsName, points1.getIdentifier().getID());
    Map<PropertyDescriptor, AttributeDiff> map = Maps.newHashMap();
    Optional<?> oldValue = Optional.fromNullable(points1.getProperty("sp").getValue());
    GenericAttributeDiffImpl diff = new GenericAttributeDiffImpl(oldValue, Optional.of("new"));
    map.put(pointsType.getDescriptor("sp"), diff);
    FeatureDiff featureDiff = new FeatureDiff(path, map, RevFeatureTypeImpl.build(pointsType), RevFeatureTypeImpl.build(pointsType));
    patch.addModifiedFeature(featureDiff);
    try {
        geogig.command(ApplyPatchOp.class).setPatch(patch).call();
        fail();
    } catch (CannotApplyPatchException e) {
        assertTrue(true);
    }
}
Also used : FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) GenericAttributeDiffImpl(org.locationtech.geogig.api.plumbing.diff.GenericAttributeDiffImpl) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) CannotApplyPatchException(org.locationtech.geogig.api.porcelain.CannotApplyPatchException) Patch(org.locationtech.geogig.api.plumbing.diff.Patch) Test(org.junit.Test)

Example 2 with CannotApplyPatchException

use of org.locationtech.geogig.api.porcelain.CannotApplyPatchException in project GeoGig by boundlessgeo.

the class ApplyPatchOpTest method testModifyFeatureAttributeOutdatedPatch.

@Test
public void testModifyFeatureAttributeOutdatedPatch() throws Exception {
    insert(points1_modified);
    Patch patch = new Patch();
    String path = NodeRef.appendChild(pointsName, points1.getIdentifier().getID());
    Map<PropertyDescriptor, AttributeDiff> map = Maps.newHashMap();
    Optional<?> oldValue = Optional.fromNullable(points1.getProperty("sp").getValue());
    GenericAttributeDiffImpl diff = new GenericAttributeDiffImpl(oldValue, Optional.of("new"));
    map.put(pointsType.getDescriptor("sp"), diff);
    FeatureDiff feaureDiff = new FeatureDiff(path, map, RevFeatureTypeImpl.build(pointsType), RevFeatureTypeImpl.build(pointsType));
    patch.addModifiedFeature(feaureDiff);
    try {
        geogig.command(ApplyPatchOp.class).setPatch(patch).call();
        fail();
    } catch (CannotApplyPatchException e) {
        assertTrue(true);
    }
}
Also used : FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) GenericAttributeDiffImpl(org.locationtech.geogig.api.plumbing.diff.GenericAttributeDiffImpl) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) CannotApplyPatchException(org.locationtech.geogig.api.porcelain.CannotApplyPatchException) Patch(org.locationtech.geogig.api.plumbing.diff.Patch) Test(org.junit.Test)

Example 3 with CannotApplyPatchException

use of org.locationtech.geogig.api.porcelain.CannotApplyPatchException in project GeoGig by boundlessgeo.

the class ApplyPatchOpTest method testRemoveFeatureAttributeOutdatedPatch.

@Test
public void testRemoveFeatureAttributeOutdatedPatch() throws Exception {
    insert(points1B_modified);
    Patch patch = new Patch();
    String path = NodeRef.appendChild(pointsName, points1B.getIdentifier().getID());
    Map<PropertyDescriptor, AttributeDiff> map = Maps.newHashMap();
    Optional<?> oldValue = Optional.fromNullable(points1B.getProperty("extra").getValue());
    GenericAttributeDiffImpl diff = new GenericAttributeDiffImpl(oldValue, null);
    map.put(modifiedPointsType.getDescriptor("extra"), diff);
    FeatureDiff featureDiff = new FeatureDiff(path, map, RevFeatureTypeImpl.build(modifiedPointsType), RevFeatureTypeImpl.build(pointsType));
    patch.addModifiedFeature(featureDiff);
    try {
        geogig.command(ApplyPatchOp.class).setPatch(patch).call();
        fail();
    } catch (CannotApplyPatchException e) {
        assertTrue(true);
    }
}
Also used : FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) GenericAttributeDiffImpl(org.locationtech.geogig.api.plumbing.diff.GenericAttributeDiffImpl) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) CannotApplyPatchException(org.locationtech.geogig.api.porcelain.CannotApplyPatchException) Patch(org.locationtech.geogig.api.plumbing.diff.Patch) Test(org.junit.Test)

Example 4 with CannotApplyPatchException

use of org.locationtech.geogig.api.porcelain.CannotApplyPatchException in project GeoGig by boundlessgeo.

the class Apply method runInternal.

@Override
public void runInternal(GeogigCLI cli) throws IOException {
    checkParameter(patchFiles.size() < 2, "Only one single patch file accepted");
    checkParameter(!patchFiles.isEmpty(), "No patch file specified");
    ConsoleReader console = cli.getConsole();
    GeoGIG geogig = cli.getGeogig();
    File patchFile = new File(patchFiles.get(0));
    checkParameter(patchFile.exists(), "Patch file cannot be found");
    FileInputStream stream;
    try {
        stream = new FileInputStream(patchFile);
    } catch (FileNotFoundException e1) {
        throw new CommandFailedException("Can't open patch file " + patchFile, e1);
    }
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        Closeables.closeQuietly(reader);
        Closeables.closeQuietly(stream);
        throw new CommandFailedException("Error reading patch file " + patchFile, e);
    }
    Patch patch = PatchSerializer.read(reader);
    Closeables.closeQuietly(reader);
    Closeables.closeQuietly(stream);
    if (reverse) {
        patch = patch.reversed();
    }
    if (summary) {
        console.println(patch.toString());
    } else if (check) {
        VerifyPatchResults verify = cli.getGeogig().command(VerifyPatchOp.class).setPatch(patch).call();
        Patch toReject = verify.getToReject();
        Patch toApply = verify.getToApply();
        if (toReject.isEmpty()) {
            console.println("Patch can be applied.");
        } else {
            console.println("Error: Patch cannot be applied\n");
            console.println("Applicable entries:\n");
            console.println(toApply.toString());
            console.println("\nConflicting entries:\n");
            console.println(toReject.toString());
        }
    } else {
        try {
            Patch rejected = geogig.command(ApplyPatchOp.class).setPatch(patch).setApplyPartial(reject).call();
            if (reject) {
                if (rejected.isEmpty()) {
                    console.println("Patch applied succesfully");
                } else {
                    int accepted = patch.count() - rejected.count();
                    StringBuilder sb = new StringBuilder();
                    File file = new File(patchFile.getAbsolutePath() + ".rej");
                    sb.append("Patch applied only partially.\n");
                    sb.append(Integer.toString(accepted) + " changes were applied.\n");
                    sb.append(Integer.toString(rejected.count()) + " changes were rejected.\n");
                    BufferedWriter writer = Files.newWriter(file, Charsets.UTF_8);
                    PatchSerializer.write(writer, patch);
                    writer.flush();
                    writer.close();
                    sb.append("Patch file with rejected changes created at " + file.getAbsolutePath() + "\n");
                    throw new CommandFailedException(sb.toString());
                }
            } else {
                console.println("Patch applied succesfully");
            }
        } catch (CannotApplyPatchException e) {
            throw new CommandFailedException(e);
        }
    }
}
Also used : ConsoleReader(jline.console.ConsoleReader) InputStreamReader(java.io.InputStreamReader) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) VerifyPatchResults(org.locationtech.geogig.api.plumbing.diff.VerifyPatchResults) FileInputStream(java.io.FileInputStream) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) BufferedWriter(java.io.BufferedWriter) BufferedReader(java.io.BufferedReader) CannotApplyPatchException(org.locationtech.geogig.api.porcelain.CannotApplyPatchException) File(java.io.File) Patch(org.locationtech.geogig.api.plumbing.diff.Patch) VerifyPatchOp(org.locationtech.geogig.api.plumbing.diff.VerifyPatchOp) GeoGIG(org.locationtech.geogig.api.GeoGIG)

Example 5 with CannotApplyPatchException

use of org.locationtech.geogig.api.porcelain.CannotApplyPatchException in project GeoGig by boundlessgeo.

the class ApplyPatchOpTest method testAddFeatureAttributeOutdatedPatch.

@Test
public void testAddFeatureAttributeOutdatedPatch() throws Exception {
    insert(points1B);
    Patch patch = new Patch();
    String path = NodeRef.appendChild(pointsName, points1.getIdentifier().getID());
    Map<PropertyDescriptor, AttributeDiff> map = Maps.newHashMap();
    Optional<?> newValue = Optional.fromNullable(points1B.getProperty("extra").getValue());
    GenericAttributeDiffImpl diff = new GenericAttributeDiffImpl(null, newValue);
    map.put(modifiedPointsType.getDescriptor("extra"), diff);
    FeatureDiff featureDiff = new FeatureDiff(path, map, RevFeatureTypeImpl.build(modifiedPointsType), RevFeatureTypeImpl.build(modifiedPointsType));
    patch.addModifiedFeature(featureDiff);
    try {
        geogig.command(ApplyPatchOp.class).setPatch(patch).call();
        fail();
    } catch (CannotApplyPatchException e) {
        assertTrue(true);
    }
}
Also used : FeatureDiff(org.locationtech.geogig.api.plumbing.diff.FeatureDiff) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) GenericAttributeDiffImpl(org.locationtech.geogig.api.plumbing.diff.GenericAttributeDiffImpl) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff) CannotApplyPatchException(org.locationtech.geogig.api.porcelain.CannotApplyPatchException) Patch(org.locationtech.geogig.api.plumbing.diff.Patch) Test(org.junit.Test)

Aggregations

Patch (org.locationtech.geogig.api.plumbing.diff.Patch)7 CannotApplyPatchException (org.locationtech.geogig.api.porcelain.CannotApplyPatchException)7 Test (org.junit.Test)6 AttributeDiff (org.locationtech.geogig.api.plumbing.diff.AttributeDiff)4 FeatureDiff (org.locationtech.geogig.api.plumbing.diff.FeatureDiff)4 GenericAttributeDiffImpl (org.locationtech.geogig.api.plumbing.diff.GenericAttributeDiffImpl)4 PropertyDescriptor (org.opengis.feature.type.PropertyDescriptor)4 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStreamReader (java.io.InputStreamReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ConsoleReader (jline.console.ConsoleReader)1 GeoGIG (org.locationtech.geogig.api.GeoGIG)1 VerifyPatchOp (org.locationtech.geogig.api.plumbing.diff.VerifyPatchOp)1 VerifyPatchResults (org.locationtech.geogig.api.plumbing.diff.VerifyPatchResults)1 CommandFailedException (org.locationtech.geogig.cli.CommandFailedException)1