package io.lumify.it;
import io.lumify.core.model.properties.LumifyProperties;
import io.lumify.web.clientapi.LumifyApi;
import io.lumify.web.clientapi.codegen.ApiException;
import io.lumify.web.clientapi.model.*;
import io.lumify.web.clientapi.model.util.ObjectMapperFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@RunWith(MockitoJUnitRunner.class)
public class WorkspaceSandboxingIntegrationTest extends TestBase {
private static final String NAME_PROPERTY_IRI = "http://lumify.io/test#name";
private String susanFengVertexId;
@Test
public void testWorkspaceSandboxing() throws ApiException, IOException {
setupTest();
addPropertyWithPublicChangeSandboxStatus();
addPropertyWithPrivateSandboxStatus();
assertAllPropertiesArePublic();
}
private void setupTest() throws ApiException, IOException {
LumifyApi lumifyApi = login(USERNAME_TEST_USER_1);
addUserAuth(lumifyApi, USERNAME_TEST_USER_1, "A");
addUserAuth(lumifyApi, USERNAME_TEST_USER_1, "B");
addUserAuth(lumifyApi, USERNAME_TEST_USER_1, "C");
ClientApiArtifactImportResponse artifact = lumifyApi.getVertexApi().importFile("", "test.txt", new ByteArrayInputStream("Susan Feng knows Joe Ferner.".getBytes()));
assertEquals(1, artifact.getVertexIds().size());
assertNotNull(artifact.getVertexIds().get(0));
lumifyTestCluster.processGraphPropertyQueue();
ClientApiElement susanFengVertex = lumifyApi.getVertexApi().create(CONCEPT_TEST_PERSON, "");
susanFengVertexId = susanFengVertex.getId();
lumifyApi.getVertexApi().setProperty(susanFengVertexId, TEST_MULTI_VALUE_KEY, LumifyProperties.TITLE.getPropertyName(), "Susan Feng", "", "test", null, null);
lumifyTestCluster.processGraphPropertyQueue();
lumifyApi.getVertexApi().setProperty(susanFengVertexId, "key1", NAME_PROPERTY_IRI, "Joe", "A", "test", null, null);
lumifyApi.getVertexApi().setProperty(susanFengVertexId, "key2", NAME_PROPERTY_IRI, "Bob", "A", "test", null, null);
lumifyApi.getVertexApi().setProperty(susanFengVertexId, "key2", NAME_PROPERTY_IRI, "Sam", "B", "test", null, null);
assertPublishAll(lumifyApi, 17);
susanFengVertex = lumifyApi.getVertexApi().getByVertexId(susanFengVertexId);
assertEquals(SandboxStatus.PUBLIC, susanFengVertex.getSandboxStatus());
List<ClientApiProperty> properties = susanFengVertex.getProperties();
assertHasProperty(properties, "key1", NAME_PROPERTY_IRI, "Joe");
List<ClientApiProperty> key2Properties = getProperties(properties, "key2", NAME_PROPERTY_IRI);
assertEquals(2, key2Properties.size());
assertEquals("Bob", key2Properties.get(0).getValue());
assertEquals("Sam", key2Properties.get(1).getValue());
assertEquals(SandboxStatus.PUBLIC, key2Properties.get(0).getSandboxStatus());
assertEquals(SandboxStatus.PUBLIC, key2Properties.get(1).getSandboxStatus());
for (ClientApiProperty property : properties) {
assertEquals(SandboxStatus.PUBLIC, property.getSandboxStatus());
}
lumifyApi.logout();
}
private void addPropertyWithPublicChangeSandboxStatus() throws ApiException, IOException {
LumifyApi lumifyApi = login(USERNAME_TEST_USER_1);
ClientApiElement susanFengVertex = lumifyApi.getVertexApi().setProperty(susanFengVertexId, "key1", NAME_PROPERTY_IRI, "Tom", "A", "test", null, null);
List<ClientApiProperty> key1Properties = getProperties(susanFengVertex.getProperties(), "key1", NAME_PROPERTY_IRI);
assertEquals(2, key1Properties.size());
assertEquals("Tom", key1Properties.get(0).getValue());
assertEquals(SandboxStatus.PUBLIC_CHANGED, key1Properties.get(0).getSandboxStatus());
assertEquals("Joe", key1Properties.get(1).getValue());
assertEquals(SandboxStatus.PUBLIC, key1Properties.get(1).getSandboxStatus());
List<ClientApiProperty> key2Properties = getProperties(susanFengVertex.getProperties(), "key2", NAME_PROPERTY_IRI);
assertEquals("Bob", key2Properties.get(0).getValue());
assertEquals(SandboxStatus.PUBLIC, key2Properties.get(0).getSandboxStatus());
assertEquals("Sam", key2Properties.get(1).getValue());
assertEquals(SandboxStatus.PUBLIC, key2Properties.get(1).getSandboxStatus());
lumifyApi.logout();
}
private void addPropertyWithPrivateSandboxStatus() throws ApiException, IOException {
LumifyApi lumifyApi = login(USERNAME_TEST_USER_1);
lumifyApi.getVertexApi().setProperty(susanFengVertexId, "key2", NAME_PROPERTY_IRI, "Dave", "C", "test", null, null);
ClientApiElement susanFengVertex = lumifyApi.getVertexApi().setProperty(susanFengVertexId, "key3", NAME_PROPERTY_IRI, "Susan", "", "test", null, null);
List<ClientApiProperty> key1Properties = getProperties(susanFengVertex.getProperties(), "key1", NAME_PROPERTY_IRI);
assertEquals(2, key1Properties.size());
assertEquals("Tom", key1Properties.get(0).getValue());
assertEquals(SandboxStatus.PUBLIC_CHANGED, key1Properties.get(0).getSandboxStatus());
assertEquals(SandboxStatus.PUBLIC, key1Properties.get(1).getSandboxStatus());
List<ClientApiProperty> key2Properties = getProperties(susanFengVertex.getProperties(), "key2", NAME_PROPERTY_IRI);
assertEquals(3, key2Properties.size());
assertEquals("Bob", key2Properties.get(0).getValue());
assertEquals(SandboxStatus.PUBLIC, key2Properties.get(0).getSandboxStatus());
assertEquals("Sam", key2Properties.get(1).getValue());
assertEquals(SandboxStatus.PUBLIC, key2Properties.get(1).getSandboxStatus());
assertEquals("Dave", key2Properties.get(2).getValue());
assertEquals(SandboxStatus.PRIVATE, key2Properties.get(2).getSandboxStatus());
List<ClientApiProperty> key3Properties = getProperties(susanFengVertex.getProperties(), "key3", NAME_PROPERTY_IRI);
assertEquals(1, key3Properties.size());
assertEquals("Susan", key3Properties.get(0).getValue());
assertEquals(SandboxStatus.PRIVATE, key3Properties.get(0).getSandboxStatus());
lumifyApi.logout();
}
private void assertAllPropertiesArePublic() throws ApiException, IOException {
LumifyApi lumifyApi = login(USERNAME_TEST_USER_1);
assertPublishAll(lumifyApi, 3);
ClientApiElement susanFengVertex = lumifyApi.getVertexApi().getByVertexId(susanFengVertexId);
List<ClientApiProperty> properties = susanFengVertex.getProperties();
assertEquals(8, properties.size());
VisibilityJson visibilityJsonA = new VisibilityJson();
visibilityJsonA.setSource("A");
VisibilityJson visibilityJsonB = new VisibilityJson();
visibilityJsonB.setSource("B");
VisibilityJson visibilityJsonC = new VisibilityJson();
visibilityJsonC.setSource("C");
VisibilityJson visibilityJsonNoSource = new VisibilityJson();
List<ClientApiProperty> key1Properties = getProperties(susanFengVertex.getProperties(), "key1", NAME_PROPERTY_IRI);
assertEquals(1, key1Properties.size());
assertHasProperty(key1Properties, "key1", NAME_PROPERTY_IRI, "Tom");
ClientApiProperty key1Property = key1Properties.get(0);
checkVisibility(key1Property, visibilityJsonA);
List<ClientApiProperty> key2Properties = getProperties(susanFengVertex.getProperties(), "key2", NAME_PROPERTY_IRI);
assertEquals(3, key2Properties.size());
assertEquals("Bob", key2Properties.get(0).getValue());
assertEquals("Sam", key2Properties.get(1).getValue());
assertEquals("Dave", key2Properties.get(2).getValue());
checkVisibility(key2Properties.get(0), visibilityJsonA);
checkVisibility(key2Properties.get(1), visibilityJsonB);
checkVisibility(key2Properties.get(2), visibilityJsonC);
List<ClientApiProperty> key3Properties = getProperties(susanFengVertex.getProperties(), "key3", NAME_PROPERTY_IRI);
assertEquals(1, key3Properties.size());
assertHasProperty(key3Properties, "key3", NAME_PROPERTY_IRI, "Susan");
ClientApiProperty key3Property = key3Properties.get(0);
checkVisibility(key3Property, visibilityJsonNoSource);
lumifyApi.logout();
}
private void checkVisibility(ClientApiProperty property, VisibilityJson expectedVisibilityJson) {
try {
String visibilityJson = ObjectMapperFactory.getInstance().writeValueAsString(property.getMetadata().get(LumifyProperties.VISIBILITY_JSON.getPropertyName()));
assertEquals(expectedVisibilityJson.toString(), visibilityJson);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}