/*******************************************************************************
* Copyright (c) 2012-2015 Codenvy, S.A.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
*******************************************************************************/
package org.eclipse.che.api.vfs.server.impl.memory;
import org.eclipse.che.api.vfs.server.VirtualFile;
import org.eclipse.che.api.vfs.shared.dto.Principal;
import org.eclipse.che.api.vfs.shared.dto.VirtualFileSystemInfo.BasicPermissions;
import com.google.common.collect.Sets;
import org.everrest.core.impl.ContainerResponse;
import java.io.ByteArrayInputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/** @author andrew00x */
public class UpdateTest extends MemoryFileSystemTest {
private String fileId;
@Override
protected void setUp() throws Exception {
super.setUp();
String name = getClass().getName();
VirtualFile updateTestFolder = mountPoint.getRoot().createFolder(name);
VirtualFile file =
updateTestFolder.createFile("UpdateTest_FILE", "text/plain", new ByteArrayInputStream(DEFAULT_CONTENT.getBytes()));
fileId = file.getId();
}
public void testUpdatePropertiesFile() throws Exception {
String properties = "[{\"name\":\"MyProperty\", \"value\":[\"MyValue\"]}]";
doUpdate(fileId, properties);
VirtualFile file = mountPoint.getVirtualFileById(fileId);
assertEquals("MyValue", file.getPropertyValue("MyProperty"));
}
public void testUpdatePropertiesLockedFile() throws Exception {
VirtualFile file = mountPoint.getVirtualFileById(fileId);
String lockToken = file.lock(0);
String properties = "[{\"name\":\"MyProperty\", \"value\":[\"MyValue\"]}]";
String path = SERVICE_URI + "item/" + fileId + "?lockToken=" + lockToken;
Map<String, List<String>> h = new HashMap<>(1);
h.put("Content-Type", Arrays.asList("application/json"));
ContainerResponse response = launcher.service("POST", path, BASE_URI, h, properties.getBytes(), null);
assertEquals(200, response.getStatus());
file = mountPoint.getVirtualFileById(fileId);
assertEquals("MyValue", file.getPropertyValue("MyProperty"));
}
public void testUpdatePropertiesLockedFileNoLockToken() throws Exception {
VirtualFile file = mountPoint.getVirtualFileById(fileId);
file.lock(0);
String properties = "[{\"name\":\"MyProperty\", \"value\":[\"MyValue\"]}]";
String path = SERVICE_URI + "item/" + fileId;
Map<String, List<String>> h = new HashMap<>(1);
h.put("Content-Type", Arrays.asList("application/json"));
ContainerResponse response = launcher.service("POST", path, BASE_URI, h, properties.getBytes(), null);
assertEquals(403, response.getStatus());
file = mountPoint.getVirtualFileById(fileId);
assertEquals(null, file.getPropertyValue("MyProperty"));
}
public void testUpdatePropertiesNoPermissions() throws Exception {
VirtualFile file = mountPoint.getVirtualFileById(fileId);
Principal adminPrincipal = createPrincipal("admin", Principal.Type.USER);
Principal userPrincipal = createPrincipal("john", Principal.Type.USER);
Map<Principal, Set<String>> permissions = new HashMap<>(2);
permissions.put(adminPrincipal, Sets.newHashSet(BasicPermissions.ALL.value()));
permissions.put(userPrincipal, Sets.newHashSet(BasicPermissions.READ.value()));
file.updateACL(createAcl(permissions), true, null);
String properties = "[{\"name\":\"MyProperty\", \"value\":[\"MyValue\"]}]";
String path = SERVICE_URI + "item/" + fileId;
Map<String, List<String>> h = new HashMap<>(1);
h.put("Content-Type", Arrays.asList("application/json"));
ContainerResponse response = launcher.service("POST", path, BASE_URI, h, properties.getBytes(), null);
assertEquals(403, response.getStatus());
file = mountPoint.getVirtualFileById(fileId);
assertEquals(null, file.getPropertyValue("MyProperty"));
}
public void doUpdate(String id, String rawData) throws Exception {
String path = SERVICE_URI + "item/" + id;
Map<String, List<String>> h = new HashMap<>(1);
h.put("Content-Type", Arrays.asList("application/json"));
ContainerResponse response = launcher.service("POST", path, BASE_URI, h, rawData.getBytes(), null);
assertEquals(200, response.getStatus());
}
}