package org.rmatil.sync.test.syncer.sharing;

import org.junit.BeforeClass;
import org.junit.Test;
import org.rmatil.sync.core.syncer.sharing.SharingSyncer;
import org.rmatil.sync.network.core.model.NodeLocation;
import org.rmatil.sync.persistence.exceptions.InputOutputException;
import org.rmatil.sync.test.messaging.base.BaseNetworkHandlerTest;
import org.rmatil.sync.version.api.AccessType;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static org.junit.Assert.*;

public class SharingSyncerTest extends BaseNetworkHandlerTest {

    protected static SharingSyncer SHARING_SYNCER;
    protected static Path TEST_DIR  = Paths.get("myDir");
    protected static Path TEST_FILE = TEST_DIR.resolve("myFile.txt");

    @BeforeClass
    public static void setUpChild()
            throws IOException, InputOutputException {
        SHARING_SYNCER = new SharingSyncer(
                CLIENT_1,
                CLIENT_MANAGER_1,
                STORAGE_ADAPTER_1,
                OBJECT_STORE_1
        );

        Files.createDirectory(ROOT_TEST_DIR1.resolve(TEST_DIR));
        Files.createFile(ROOT_TEST_DIR1.resolve(TEST_FILE));

        OBJECT_STORE_1.sync(ROOT_TEST_DIR1.toFile());

        OBJECT_STORE_1.getSharerManager().addSharer(
                USER_2.getUserName(),
                AccessType.WRITE,
                TEST_DIR.toString()
        );

        OBJECT_STORE_1.getSharerManager().addSharer(
                USER_2.getUserName(),
                AccessType.WRITE,
                TEST_FILE.toString()
        );
    }

    @Test
    public void testGetClientLocationFromSharer() {
        NodeLocation nodeLocation = SHARING_SYNCER.getClientLocationFromSharer(USER_1.getUserName());

        assertNotNull("NodeLocation should not be null", nodeLocation);

        NodeLocation nodeLocation2 = SHARING_SYNCER.getClientLocationFromSharer("someNonExistingUser");
        assertNull("NodeLocation2 should be null", nodeLocation2);
    }
}


@@ -90,70 +90,385 @@
 nit.
-Test;%0Aimport org.rmatil.sync.core.syncer.sharing.SharingSyncer
+Rule;%0Aimport org.junit.Test;%0Aimport org.junit.rules.ExpectedException;%0Aimport org.rmatil.sync.core.exception.SharingFailedException;%0Aimport org.rmatil.sync.core.exception.UnsharingFailedException;%0Aimport org.rmatil.sync.core.syncer.sharing.SharingSyncer;%0Aimport org.rmatil.sync.core.syncer.sharing.event.ShareEvent;%0Aimport org.rmatil.sync.core.syncer.sharing.event.UnshareEvent
 ;%0Aim
@@ -851,9 +851,64 @@
 ert.
-*
+assertNotNull;%0Aimport static org.junit.Assert.assertNull
 ;%0A%0Ap
@@ -971,16 +971,91 @@
 Test %7B%0A%0A
+    @Rule%0A    public ExpectedException thrown = ExpectedException.none();%0A%0A
     prot
@@ -2465,12 +2465,1344 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testShareWithOurSelf() %7B%0A        ShareEvent shareEvent = new ShareEvent(%0A                TEST_DIR,%0A                AccessType.WRITE,%0A                CLIENT_1.getUser().getUserName()%0A        );%0A%0A        thrown.expect(SharingFailedException.class);%0A        SHARING_SYNCER.sync(shareEvent);%0A    %7D%0A%0A    @Test%0A    public void testShareWithOurSelf2() %7B%0A        ShareEvent shareEvent = new ShareEvent(%0A                TEST_DIR,%0A                AccessType.WRITE,%0A                CLIENT_1.getUser().getUserName()%0A        );%0A%0A        thrown.expect(SharingFailedException.class);%0A        SHARING_SYNCER.syncShareEvent(shareEvent);%0A    %7D%0A%0A    @Test%0A    public void testUnshareWithOurSelf() %7B%0A        UnshareEvent unshareEvent = new UnshareEvent(%0A                TEST_DIR,%0A                AccessType.WRITE,%0A                CLIENT_1.getUser().getUserName()%0A        );%0A%0A        thrown.expect(UnsharingFailedException.class);%0A        SHARING_SYNCER.sync(unshareEvent);%0A    %7D%0A%0A    @Test%0A    public void testUnshareWithOurSelf2() %7B%0A        UnshareEvent unshareEvent = new UnshareEvent(%0A                TEST_DIR,%0A                AccessType.WRITE,%0A                CLIENT_1.getUser().getUserName()%0A        );%0A%0A        thrown.expect(UnsharingFailedException.class);%0A        SHARING_SYNCER.syncUnshareEvent(unshareEvent);%0A    %7D
 %0A%7D%0A%0A

Add test for (un-) sharing with own user