@Test public void testMergeAlreadyUpToDate() throws Exception { createWorkspace(SimpleMetaStore.DEFAULT_WORKSPACE_NAME); String projectName = getMethodName().concat("Project"); JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString()); JSONObject testTxt = getChild(project, "test.txt"); modifyFile(testTxt, "change in master"); JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); String gitIndexUri = gitSection.getString(GitConstants.KEY_INDEX); String gitStatusUri = gitSection.getString(GitConstants.KEY_STATUS); String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD); // "git add ." WebRequest request = GitAddTest.getPutGitIndexRequest(gitIndexUri); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); assertStatus(new StatusResult().setChanged(1), gitStatusUri); // "git merge master" JSONObject merge = merge(gitHeadUri, Constants.MASTER); MergeStatus mergeResult = MergeResult.MergeStatus.valueOf(merge.getString(GitConstants.KEY_RESULT)); assertEquals(MergeResult.MergeStatus.ALREADY_UP_TO_DATE, mergeResult); // status hasn't changed assertStatus(new StatusResult().setChanged(1), gitStatusUri); }
public void testDeleteUserWithLoan() throws Exception { logger.info("Entered TC18 testDeleteUserWithLoan"); User user; String parameterUserName = "******" + System.currentTimeMillis(); String IsbnName = "testISBN" + System.currentTimeMillis(); user = new User("TestFirstName", "TestLastName", parameterUserName, "password", Role.STUDENT); String parameterBookName = "MyBook" + System.currentTimeMillis(); userService.saveOrUpdate(user); Book book = new Book(parameterBookName, IsbnName, 2); bookService.saveOrUpdate(book); session.refresh(book); logger.info("User added" + user.getUsername()); // now create loan for this user Calendar now = Calendar.getInstance(); now.add(Calendar.MINUTE, 5); logger.info("Book " + book.getBookid() + " user " + user.getUserId()); Loan loan = new Loan(user.getUserId(), book.getBookid(), now.getTime(), 0, 10, false); loandao.saveOrUpdate(loan); loanId = loan.getLoanId(); bookId = book.getBookid(); userId = user.getUserId(); logger.debug("Loan " + loan.getLoanId() + " created for user " + user.getUserId()); logger.info("Loan created: " + loan.getLoanId()); logger.info("trying to delete userID: " + user.getUserId()); WebConversation conversation = new WebConversation(); WebRequest requestDeleteUser = new GetMethodWebRequest(Constant.DELETE_USER_URL + user.getUserId()); WebResponse responseGetUser = conversation.getResponse(requestDeleteUser); WebTable bookListUpdatedTable = responseGetUser.getTableWithID("userListTable"); TableCell tableUpdatedCell = bookListUpdatedTable.getTableCellWithID(user.getUserId()); assertEquals(tableUpdatedCell.getText(), user.getUserId()); logger.info("Exited TC18 testDeleteUserWithLoan"); }
@Test public void testReadDirectoryChildren() throws CoreException, IOException, SAXException, JSONException { String dirName = "path" + System.currentTimeMillis(); String directoryPath = "sample/directory/" + dirName; createDirectory(directoryPath); String subDirectory = "subdirectory"; createDirectory(directoryPath + "/" + subDirectory); String subFile = "subfile.txt"; createFile(directoryPath + "/" + subFile, "Sample file"); WebRequest request = getGetFilesRequest(directoryPath + "?depth=1"); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); List<JSONObject> children = getDirectoryChildren(new JSONObject(response.getText())); assertEquals("Wrong number of directory children", 2, children.size()); for (JSONObject child : children) { if (child.getBoolean("Directory")) { checkDirectoryMetadata(child, subDirectory, null, null, null, null, null); } else { checkFileMetadata(child, subFile, null, null, null, null, null, null, null); } } }
@Test public void testGetNonexistingClone() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); String workspaceId = getWorkspaceId(workspaceLocation); WebRequest request = listGitClonesRequest(workspaceId, null); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject clones = new JSONObject(response.getText()); JSONArray clonesArray = clones.getJSONArray(ProtocolConstants.KEY_CHILDREN); String dummyId = "dummyId"; ensureCloneIdDoesntExist(clonesArray, dummyId); String requestURI = SERVER_LOCATION + GIT_SERVLET_LOCATION + Clone.RESOURCE + "/workspace/" + dummyId; request = getGetRequest(requestURI); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_NOT_FOUND, response.getResponseCode()); requestURI = SERVER_LOCATION + GIT_SERVLET_LOCATION + Clone.RESOURCE + "/file/" + dummyId; request = getGetRequest(requestURI); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_NOT_FOUND, response.getResponseCode()); }
@Test public void testDiffModifiedByOrion() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); String projectName = getMethodName(); JSONObject project = createProjectOrLink(workspaceLocation, projectName, gitDir.toString()); String projectId = project.getString(ProtocolConstants.KEY_ID); WebRequest request = getPutFileRequest(projectId + "/test.txt", "hello"); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject gitSection = project.optJSONObject(GitConstants.KEY_GIT); assertNotNull(gitSection); String gitDiffUri = gitSection.getString(GitConstants.KEY_DIFF); request = getGetGitDiffRequest(gitDiffUri); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); StringBuilder sb = new StringBuilder(); sb.append("diff --git a/test.txt b/test.txt").append("\n"); sb.append("index 30d74d2..b6fc4c6 100644").append("\n"); sb.append("--- a/test.txt").append("\n"); sb.append("+++ b/test.txt").append("\n"); sb.append("@@ -1 +1 @@").append("\n"); sb.append("-test").append("\n"); sb.append("\\ No newline at end of file").append("\n"); sb.append("+hello").append("\n"); sb.append("\\ No newline at end of file").append("\n"); String[] parts = parseMultiPartResponse(response); assertEquals(sb.toString(), parts[1]); }
@Test public void testCloneAndLinkToFolder() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), null); IPath clonePath = new Path("file").append(project.getString(ProtocolConstants.KEY_ID)).makeAbsolute(); String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION); File folder = new File( getRepositoryForContentLocation(contentLocation).getDirectory().getParentFile(), "folder"); JSONObject newProject = createProjectOrLink( workspaceLocation, getMethodName() + "-link", folder.toURI().toString()); String projectContentLocation = newProject.getString(ProtocolConstants.KEY_CONTENT_LOCATION); // http://<host>/file/<projectId>/ WebRequest request = getGetFilesRequest(projectContentLocation); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject link = new JSONObject(response.getText()); String childrenLocation = link.getString(ProtocolConstants.KEY_CHILDREN_LOCATION); assertNotNull(childrenLocation); // http://<host>/file/<projectId>/?depth=1 request = getGetFilesRequest(childrenLocation); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); List<JSONObject> children = getDirectoryChildren(new JSONObject(response.getText())); String[] expectedChildren = new String[] {"folder.txt"}; assertEquals("Wrong number of directory children", expectedChildren.length, children.size()); assertEquals(expectedChildren[0], children.get(0).getString(ProtocolConstants.KEY_NAME)); }
@Test public void testDeleteInWorkspace() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), null); IPath clonePath = new Path("file").append(project.getString(ProtocolConstants.KEY_ID)).makeAbsolute(); JSONObject clone = clone(clonePath); String cloneLocation = clone.getString(ProtocolConstants.KEY_LOCATION); String contentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION); Repository repository = getRepositoryForContentLocation(contentLocation); assertNotNull(repository); // delete folder with cloned repository in it WebRequest request = getDeleteFilesRequest(contentLocation); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); // the clone is gone request = getGetRequest(cloneLocation); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_NOT_FOUND, response.getResponseCode()); assertFalse(repository.getDirectory().exists()); }
@Test public void testCheckoutPathInUri() throws Exception { // clone a repo URI workspaceLocation = createWorkspace(getMethodName()); JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), null); IPath clonePath = new Path("file").append(project.getString(ProtocolConstants.KEY_ID)).makeAbsolute(); clone(clonePath); // get project metadata WebRequest request = getGetFilesRequest(project.getString(ProtocolConstants.KEY_CONTENT_LOCATION)); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); project = new JSONObject(response.getText()); JSONObject testTxt = getChild(project, "test.txt"); modifyFile(testTxt, "change"); JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); String gitStatusUri = gitSection.getString(GitConstants.KEY_STATUS); String gitCloneUri = GitStatusTest.getCloneUri(gitStatusUri); // TODO: don't create URIs out of thin air request = getCheckoutRequest(gitCloneUri + "test.txt", new String[] {}); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, response.getResponseCode()); }
@Test public void testCheckoutAfterResetByPath() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); JSONObject projectTop = createProjectOrLink(workspaceLocation, getMethodName() + "-top", null); IPath clonePathTop = new Path("file").append(projectTop.getString(ProtocolConstants.KEY_ID)).makeAbsolute(); JSONObject projectFolder = createProjectOrLink(workspaceLocation, getMethodName() + "-folder", null); IPath clonePathFolder = new Path("file") .append(projectFolder.getString(ProtocolConstants.KEY_ID)) .append("folder") .makeAbsolute(); IPath[] clonePaths = new IPath[] {clonePathTop, clonePathFolder}; for (IPath clonePath : clonePaths) { // clone a repo JSONObject clone = clone(clonePath); String cloneContentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION); // get project/folder metadata WebRequest request = getGetFilesRequest(cloneContentLocation); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject folder = new JSONObject(response.getText()); JSONObject gitSection = folder.getJSONObject(GitConstants.KEY_GIT); String gitIndexUri = gitSection.getString(GitConstants.KEY_INDEX); String gitStatusUri = gitSection.getString(GitConstants.KEY_STATUS); String gitCloneUri = GitStatusTest.getCloneUri(gitStatusUri); JSONObject testTxt = getChild(folder, "test.txt"); modifyFile(testTxt, "change"); assertStatus(new StatusResult().setModified(1), gitStatusUri); addFile(testTxt); assertStatus(new StatusResult().setChanged(1), gitStatusUri); // unstage request = GitResetTest.getPostGitIndexRequest( gitIndexUri, new String[] {"test.txt"}, null, (String) null); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); // check status again assertStatus(new StatusResult().setModified(1), gitStatusUri); // checkout request = getCheckoutRequest(gitCloneUri, new String[] {"test.txt"}); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); // check status one more time assertStatus(StatusResult.CLEAN, gitStatusUri); } }
@Test public void testUpdateNonExistingConfigEntryUsingPUT() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); IPath[] clonePaths = createTestProjects(workspaceLocation); for (IPath clonePath : clonePaths) { // clone a repo String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION); // get project metadata WebRequest request = getGetRequest(contentLocation); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject project = new JSONObject(response.getText()); JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); String gitConfigUri = gitSection.getString(GitConstants.KEY_CONFIG); final String ENTRY_KEY = "a.b.c"; final String ENTRY_VALUE = "v"; String invalidEntryLocation = gitConfigUri.replace(ConfigOption.RESOURCE, ConfigOption.RESOURCE + "/" + ENTRY_KEY); // check if it doesn't exist request = getGetGitConfigRequest(invalidEntryLocation); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_NOT_FOUND, response.getResponseCode()); // try to update non-existing config entry using PUT (not allowed) request = getPutGitConfigRequest(invalidEntryLocation, ENTRY_VALUE); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_NOT_FOUND, response.getResponseCode()); } }
@Test public void testGetConfigEntryForNonExistingRepository() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); String workspaceId = getWorkspaceId(workspaceLocation); JSONArray clonesArray = listClones(workspaceId, null); String dummyId = "dummyId"; GitCloneTest.ensureCloneIdDoesntExist(clonesArray, dummyId); String entryLocation = SERVER_LOCATION + GIT_SERVLET_LOCATION + ConfigOption.RESOURCE + "/dummyKey/" + Clone.RESOURCE + "/file/" + dummyId; // get value of config entry WebRequest request = getGetGitConfigRequest(entryLocation); WebResponse response = webConversation.getResponse(request); assertEquals( response.getResponseMessage(), HttpURLConnection.HTTP_NOT_FOUND, response.getResponseCode()); }
@Test public void testCreateInvalidConfigEntry() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); IPath[] clonePaths = createTestProjects(workspaceLocation); for (IPath clonePath : clonePaths) { // clone a repo String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION); // get project metadata WebRequest request = getGetRequest(contentLocation); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject project = new JSONObject(response.getText()); JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); String gitConfigUri = gitSection.getString(GitConstants.KEY_CONFIG); final String INVALID_ENTRY_KEY = "a"; // no name specified, dot missing final String ENTRY_VALUE = "v"; // try to set entry with invalid key request = getPostGitConfigRequest(gitConfigUri, INVALID_ENTRY_KEY, ENTRY_VALUE); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, response.getResponseCode()); } }
@Test public void testGetSingleConfigEntry() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); IPath[] clonePaths = createTestProjects(workspaceLocation); for (IPath clonePath : clonePaths) { // clone a repo String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION); // get project metadata WebRequest request = getGetRequest(contentLocation); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject project = new JSONObject(response.getText()); JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); String gitConfigUri = gitSection.getString(GitConstants.KEY_CONFIG); // set some dummy value final String ENTRY_KEY = "a.b.c"; final String ENTRY_VALUE = "v"; request = getPostGitConfigRequest(gitConfigUri, ENTRY_KEY, ENTRY_VALUE); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode()); JSONObject configResponse = new JSONObject(response.getText()); String entryLocation = configResponse.getString(ProtocolConstants.KEY_LOCATION); JSONObject configEntry = listConfigEntries(entryLocation); assertConfigOption(configEntry, ENTRY_KEY, ENTRY_VALUE); } }
/* private static final String GIT_NAME = "test"; private static final String GIT_MAIL = "test mail"; private static final String GIT_COMMIT_MESSAGE = "message"; @Test public void testClonedRepoConfigUsingUserProfile() throws Exception { // set Git name and mail in the user profile WebRequest request = getPutUserRequest(); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); // clone a repo URI workspaceLocation = createWorkspace(getMethodName()); String workspaceId = workspaceIdFromLocation(workspaceLocation); JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), null); IPath clonePath = getClonePath(workspaceId, project); String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION); // check the repository configuration using JGit API Git git = new Git(getRepositoryForContentLocation(contentLocation)); StoredConfig config = git.getRepository().getConfig(); assertEquals(GIT_NAME, config.getString(ConfigConstants.CONFIG_USER_SECTION, null, ConfigConstants.CONFIG_KEY_NAME)); assertEquals(GIT_MAIL, config.getString(ConfigConstants.CONFIG_USER_SECTION, null, ConfigConstants.CONFIG_KEY_EMAIL)); // now check if commits have the right committer set request = getGetRequest(project.getString(ProtocolConstants.KEY_CONTENT_LOCATION)); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); project = new JSONObject(response.getText()); String childrenLocation = project.getString(ProtocolConstants.KEY_CHILDREN_LOCATION); assertNotNull(childrenLocation); // check if Git locations are in place JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); String gitIndexUri = gitSection.getString(GitConstants.KEY_INDEX); String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD); // modify String projectLocation = project.getString(ProtocolConstants.KEY_LOCATION); request = getPutFileRequest(projectLocation + "/test.txt", "change to commit"); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); // add request = GitAddTest.getPutGitIndexRequest(gitIndexUri + "test.txt"); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); // commit all request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, GIT_COMMIT_MESSAGE, false); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); // log JSONArray commitsArray = log(gitHeadUri); assertEquals(2, commitsArray.length()); for (int i = 0; i < commitsArray.length(); i++) { if (commitsArray.getJSONObject(i).getString(GitConstants.KEY_COMMIT_MESSAGE).equals(GIT_COMMIT_MESSAGE)) { assertEquals(GIT_NAME, commitsArray.getJSONObject(i).getString(GitConstants.KEY_AUTHOR_NAME)); assertEquals(GIT_MAIL, commitsArray.getJSONObject(i).getString(GitConstants.KEY_AUTHOR_EMAIL)); } } } @Test public void testInitializedRepoConfigUsingUserProfile() throws Exception { // set Git name and mail in the user profile WebRequest request = getPutUserRequest(); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); // init a repo URI workspaceLocation = createWorkspace(getMethodName()); String workspaceId = workspaceIdFromLocation(workspaceLocation); JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), null); IPath initPath = getClonePath(workspaceId, project); String contentLocation = init(null, initPath, null).getString(ProtocolConstants.KEY_CONTENT_LOCATION); // check the repository configuration using JGit API Git git = new Git(getRepositoryForContentLocation(contentLocation)); StoredConfig config = git.getRepository().getConfig(); assertEquals(GIT_NAME, config.getString(ConfigConstants.CONFIG_USER_SECTION, null, ConfigConstants.CONFIG_KEY_NAME)); assertEquals(GIT_MAIL, config.getString(ConfigConstants.CONFIG_USER_SECTION, null, ConfigConstants.CONFIG_KEY_EMAIL)); // now check if commits have the right committer set request = getGetRequest(project.getString(ProtocolConstants.KEY_CONTENT_LOCATION)); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); project = new JSONObject(response.getText()); String childrenLocation = project.getString(ProtocolConstants.KEY_CHILDREN_LOCATION); assertNotNull(childrenLocation); // check if Git locations are in place JSONObject gitSection = project.optJSONObject(GitConstants.KEY_GIT); assertNotNull(gitSection); String gitIndexUri = gitSection.getString(GitConstants.KEY_INDEX); String gitHeadUri = gitSection.getString(GitConstants.KEY_HEAD); // modify String projectLocation = project.getString(ProtocolConstants.KEY_LOCATION); request = getPutFileRequest(projectLocation + "/test.txt", "change to commit"); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); // add request = GitAddTest.getPutGitIndexRequest(gitIndexUri + "test.txt"); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); // commit all request = GitCommitTest.getPostGitCommitRequest(gitHeadUri, GIT_COMMIT_MESSAGE, false); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); // log JSONArray commitsArray = log(gitHeadUri); assertEquals(2, commitsArray.length()); for (int i = 0; i < commitsArray.length(); i++) { if (commitsArray.getJSONObject(i).getString(GitConstants.KEY_COMMIT_MESSAGE).equals(GIT_COMMIT_MESSAGE)) { assertEquals(GIT_NAME, commitsArray.getJSONObject(i).getString(GitConstants.KEY_AUTHOR_NAME)); assertEquals(GIT_MAIL, commitsArray.getJSONObject(i).getString(GitConstants.KEY_AUTHOR_EMAIL)); } } } */ @Test public void testGetListOfConfigEntries() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); IPath[] clonePaths = createTestProjects(workspaceLocation); for (IPath clonePath : clonePaths) { // clone a repo String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION); // get project metadata WebRequest request = getGetRequest(contentLocation); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject project = new JSONObject(response.getText()); JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); String gitConfigUri = gitSection.getString(GitConstants.KEY_CONFIG); JSONObject configResponse = listConfigEntries(gitConfigUri); JSONArray configEntries = configResponse.getJSONArray(ProtocolConstants.KEY_CHILDREN); for (int i = 0; i < configEntries.length(); i++) { JSONObject configEntry = configEntries.getJSONObject(i); assertNotNull(configEntry.optString(GitConstants.KEY_CONFIG_ENTRY_KEY, null)); assertNotNull(configEntry.optString(GitConstants.KEY_CONFIG_ENTRY_VALUE, null)); assertConfigUri(configEntry.getString(ProtocolConstants.KEY_LOCATION)); assertCloneUri(configEntry.getString(GitConstants.KEY_CLONE)); assertEquals(ConfigOption.TYPE, configEntry.getString(ProtocolConstants.KEY_TYPE)); } } }
@Test public void testCloneEmptyPath() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); IPath clonePath = new Path("workspace").append(getWorkspaceId(workspaceLocation)).makeAbsolute(); AuthorizationService.removeUserRight("test", "/"); AuthorizationService.removeUserRight("test", "/*"); // /workspace/{id} JSONObject clone = clone(clonePath, null, null); String cloneContentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION); WebRequest request = getGetFilesRequest(cloneContentLocation); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject project = new JSONObject(response.getText()); assertEquals(gitDir.getName(), project.getString(ProtocolConstants.KEY_NAME)); assertGitSectionExists(project); String childrenLocation = project.getString(ProtocolConstants.KEY_CHILDREN_LOCATION); assertNotNull(childrenLocation); // http://<host>/file/<projectId>/?depth=1 request = getGetFilesRequest(childrenLocation); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); }
// modified + checkout = clean @Test public void testCheckoutInFolder() throws Exception { // clone a repo URI workspaceLocation = createWorkspace(getMethodName()); JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), null); IPath clonePath = new Path("file").append(project.getString(ProtocolConstants.KEY_ID)).makeAbsolute(); clone(clonePath); // get project metadata WebRequest request = getGetFilesRequest(project.getString(ProtocolConstants.KEY_CONTENT_LOCATION)); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); project = new JSONObject(response.getText()); JSONObject testTxt = getChild(project, "test.txt"); modifyFile(testTxt, "change"); JSONObject folder1 = getChild(project, "folder"); JSONObject folderTxt = getChild(folder1, "folder.txt"); modifyFile(folderTxt, "change"); JSONObject gitSection = folder1.getJSONObject(GitConstants.KEY_GIT); String gitStatusUri = gitSection.getString(GitConstants.KEY_STATUS); // we should get a proper clone URI here: /git/clone/file/{projectId}/ String gitCloneUri = GitStatusTest.getCloneUri(gitStatusUri); request = getCheckoutRequest(gitCloneUri, new String[] {"folder/folder.txt"}); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); // 'test.txt' is still modified assertStatus(new StatusResult().setModifiedNames("test.txt"), gitStatusUri); }
@Test public void testCloneEmptyPathBadUrl() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); IPath workspacePath = new Path("workspace").append(getWorkspaceId(workspaceLocation)).makeAbsolute(); AuthorizationService.removeUserRight("test", "/"); AuthorizationService.removeUserRight("test", "/*"); // /workspace/{id} + {methodName} WebRequest request = new PostGitCloneRequest() .setURIish("I'm//bad!") .setWorkspacePath(workspacePath) .setName(getMethodName()) .getWebRequest(); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, response.getResponseCode()); // no project should be created request = new GetMethodWebRequest(workspaceLocation.toString()); setAuthentication(request); response = webConversation.getResponse(request); JSONObject workspace = new JSONObject(response.getText()); assertEquals(0, workspace.getJSONArray(ProtocolConstants.KEY_CHILDREN).length()); }
@Test public void testCheckoutBranch() throws Exception { // clone a repo URI workspaceLocation = createWorkspace(getMethodName()); JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), null); IPath clonePath = new Path("file").append(project.getString(ProtocolConstants.KEY_ID)).makeAbsolute(); JSONObject clone = clone(clonePath); String cloneContentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION); String cloneLocation = clone.getString(ProtocolConstants.KEY_LOCATION); String branchesLocation = clone.getString(GitConstants.KEY_BRANCH); // get project metadata WebRequest request = getGetFilesRequest(project.getString(ProtocolConstants.KEY_CONTENT_LOCATION)); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); project = new JSONObject(response.getText()); // create branch 'a' branch(branchesLocation, "a"); // checkout 'a' response = checkoutBranch(cloneLocation, "a"); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); Repository db1 = getRepositoryForContentLocation(cloneContentLocation); Git git = new Git(db1); GitRemoteTest.assertOnBranch(git, "a"); }
@Test public void testDeleteInFolder() throws Exception { URI workspaceLocation = createWorkspace(getMethodName()); JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), null); IPath clonePath = new Path("file") .append(project.getString(ProtocolConstants.KEY_ID)) .append("clone") .makeAbsolute(); JSONObject clone = clone(clonePath); String cloneLocation = clone.getString(ProtocolConstants.KEY_LOCATION); String contentLocation = clone.getString(ProtocolConstants.KEY_CONTENT_LOCATION); // delete clone WebRequest request = getDeleteCloneRequest(cloneLocation); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); // the clone is gone request = getGetRequest(cloneLocation); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_NOT_FOUND, response.getResponseCode()); // so it's the folder request = getGetRequest(contentLocation); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_NOT_FOUND, response.getResponseCode()); // but the project is still there request = getGetFilesRequest(project.getString(ProtocolConstants.KEY_ID)); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); }
// modified + checkout = clean @Test @Ignore("not supported yet") public void testCheckoutDotPath() throws Exception { // clone a repo URI workspaceLocation = createWorkspace(getMethodName()); JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), null); IPath clonePath = new Path("file").append(project.getString(ProtocolConstants.KEY_ID)).makeAbsolute(); clone(clonePath); // get project metadata WebRequest request = getGetFilesRequest(project.getString(ProtocolConstants.KEY_CONTENT_LOCATION)); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); project = new JSONObject(response.getText()); JSONObject testTxt = getChild(project, "test.txt"); modifyFile(testTxt, "change"); JSONObject folder1 = getChild(project, "folder"); JSONObject folderTxt = getChild(folder1, "folder.txt"); modifyFile(folderTxt, "change"); JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT); String gitStatusUri = gitSection.getString(GitConstants.KEY_STATUS); String gitCloneUri = GitStatusTest.getCloneUri(gitStatusUri); request = getCheckoutRequest(gitCloneUri, new String[] {"."}); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); assertStatus(StatusResult.CLEAN, gitStatusUri); }
@Test public void testGetCloneAndPull() throws Exception { // see bug 339254 URI workspaceLocation = createWorkspace(getMethodName()); String workspaceId = getWorkspaceId(workspaceLocation); JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), null); IPath clonePath = new Path("file").append(project.getString(ProtocolConstants.KEY_ID)).makeAbsolute(); String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION); // get clones for workspace WebRequest request = listGitClonesRequest(workspaceId, null); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject clones = new JSONObject(response.getText()); JSONArray clonesArray = clones.getJSONArray(ProtocolConstants.KEY_CHILDREN); assertEquals(1, clonesArray.length()); Git git = new Git(getRepositoryForContentLocation(contentLocation)); // TODO: replace with RESTful API when ready, see bug 339114 PullResult pullResult = git.pull().call(); assertEquals(pullResult.getMergeResult().getMergeStatus(), MergeStatus.ALREADY_UP_TO_DATE); assertEquals(RepositoryState.SAFE, git.getRepository().getRepositoryState()); }
/** * Uses HttpUnit functionality to retrieve a single sds offering (including curnit and jnlp) from * the sds. * * @param sdsOfferingId The id of the offering you want to retrieve * @return The SdsOffering with all parameters set. * @throws IOException * @throws JDOMException * @throws SAXException */ protected SdsOffering getOfferingAlternativeMethod(Serializable sdsOfferingId) throws IOException, JDOMException, SAXException { WebResponse webResponse = this.makeHttpRestGetRequest("/offering/" + sdsOfferingId); assertEquals(HttpStatus.SC_OK, webResponse.getResponseCode()); Document doc = createDocumentFromResponse(webResponse); SdsOffering sdsOffering = (SdsOffering) this.applicationContext.getBean("sdsOffering"); Element offeringElement = doc.getRootElement(); sdsOffering.setName(offeringElement.getChild("name").getValue()); sdsOffering.setSdsObjectId(new Long(offeringElement.getChild("id").getValue())); Long sdsCurnitId = new Long(offeringElement.getChild("curnit-id").getValue()); SdsCurnit sdsCurnit = this.getCurnitInSds(sdsCurnitId); sdsOffering.setSdsCurnit(sdsCurnit); Long sdsJnlpId = new Long(offeringElement.getChild("jnlp-id").getValue()); SdsJnlp sdsJnlp = this.getJnlpInSds(sdsJnlpId); sdsOffering.setSdsJnlp(sdsJnlp); WebResponse curnitMapWebResponse = this.makeHttpRestGetRequest("/offering/" + sdsOfferingId + "/curnitmap"); assertEquals(HttpStatus.SC_OK, curnitMapWebResponse.getResponseCode()); Document curnitMapDoc = createDocumentFromResponse(curnitMapWebResponse); sdsOffering.setSdsCurnitMap(curnitMapDoc.getRootElement().getText()); return sdsOffering; }
/** * Test as Html. * * @param jspName jsp name, with full path * @throws Exception any axception thrown during test. */ public void doTest(String jspName) throws Exception { WebRequest request = new GetMethodWebRequest(jspName); WebResponse response = runner.getResponse(request); if (log.isDebugEnabled()) { log.debug("RESPONSE: " + response.getText()); } WebTable[] tables = response.getTables(); assertEquals("Wrong number of tables.", 1, tables.length); assertEquals("Bad number of generated columns.", 2, tables[0].getColumnCount()); assertEquals( "Bad value in column header.", // StringUtils.capitalize(KnownValue.ANT), tables[0].getCellAsText(0, 0)); assertEquals( "Bad value in column header.", // StringUtils.capitalize(KnownValue.CAMEL), tables[0].getCellAsText(0, 1)); }
/** * Uses HttpUnit functionality to retrieve a single sds workgroup from the sds, including the * offering and the members * * @param sdsWorkgroupId The id of the workgroup you want to retrieve * @return The SdsWorkgroup with all parameters set. * @throws IOException * @throws JDOMException * @throws SAXException */ @SuppressWarnings("unchecked") protected SdsWorkgroup getWorkgroupInSds(Serializable sdsWorkgroupId) throws IOException, JDOMException, SAXException { String WORKGROUP_PATH = "/workgroup/" + sdsWorkgroupId; WebResponse webResponse = this.makeHttpRestGetRequest(WORKGROUP_PATH); assertEquals(HttpStatus.SC_OK, webResponse.getResponseCode()); Document doc = createDocumentFromResponse(webResponse); SdsWorkgroup sdsWorkgroup = (SdsWorkgroup) this.applicationContext.getBean("sdsWorkgroup"); Element workgroupElement = doc.getRootElement(); sdsWorkgroup.setName(workgroupElement.getChild("name").getValue()); sdsWorkgroup.setSdsObjectId(new Long(workgroupElement.getChild("id").getValue())); Integer sdsOfferingId = new Integer(workgroupElement.getChild("offering-id").getValue()); SdsOffering sdsOffering = this.getOfferingAlternativeMethod(sdsOfferingId); sdsWorkgroup.setSdsOffering(sdsOffering); WebResponse membersWebResponse = this.makeHttpRestGetRequest(WORKGROUP_PATH + "/membership"); assertEquals(HttpStatus.SC_OK, membersWebResponse.getResponseCode()); Document membersDoc = createDocumentFromResponse(membersWebResponse); List<Element> memberElements = XPath.newInstance("/workgroup-memberships/workgroup-membership/sail-user-id") .selectNodes(membersDoc); for (Element memberNode : memberElements) { SdsUser sdsUser = this.getUserInSds(new Long(memberNode.getValue())); sdsWorkgroup.addMember(sdsUser); } return sdsWorkgroup; }
static String[] parseMultiPartResponse(WebResponse response) throws IOException { String typeHeader = response.getHeaderField(ProtocolConstants.HEADER_CONTENT_TYPE); String boundary = typeHeader.substring( typeHeader.indexOf("boundary=\"") + 10, typeHeader.length() - 1); // $NON-NLS-1$ BufferedReader reader = new BufferedReader(new StringReader(response.getText())); StringBuilder buf = new StringBuilder(); String line; List<String> parts = new ArrayList<String>(); while ((line = reader.readLine()) != null) { if (line.equals("--" + boundary)) { line = reader.readLine(); // Content-Type:{...} if (buf.length() > 0) { parts.add(buf.toString()); buf.setLength(0); } } else { if (buf.length() > 0) buf.append("\n"); buf.append(line); } } parts.add(buf.toString()); assertEquals(2, parts.size()); // JSON assertTrue(parts.get(0).startsWith("{")); // diff or empty when there is no difference assertTrue(parts.get(1).length() == 0 || parts.get(1).startsWith("diff")); return parts.toArray(new String[0]); }
/** * Check sorted column. * * @param jspName jsp name, with full path * @throws Exception any axception thrown during test. */ @Test public void doTest() throws Exception { WebRequest request = new GetMethodWebRequest(getJspUrl(getJspName())); ParamEncoder encoder = new ParamEncoder("table"); request.setParameter(encoder.encodeParameterName(TableTagParameters.PARAMETER_SORT), "1"); request.setParameter( encoder.encodeParameterName(TableTagParameters.PARAMETER_SORTUSINGNAME), "1"); WebResponse response = runner.getResponse(request); if (log.isDebugEnabled()) { log.debug(response.getText()); } WebTable[] tables = response.getTables(); Assert.assertEquals("Wrong number of tables in result.", 1, tables.length); Assert.assertEquals("Wrong number of rows in result.", 3, tables[0].getRowCount()); if (log.isDebugEnabled()) { log.debug(response.getText()); } Assert.assertEquals( "Wrong value in first row. Table incorrectly sorted?", "2", tables[0].getCellAsText(1, 1)); Assert.assertEquals( "Column 1 should not be marked as sorted.", "sortable", tables[0].getTableCell(0, 1).getClassName()); Assert.assertEquals( "Column 2 should be marked as sorted.", "sortable sorted order1", tables[0].getTableCell(0, 2).getClassName()); }
@Test public void testDeleteNonEmptyDirectory() throws CoreException, IOException, SAXException { String dirPath1 = "sample/directory/path/sample1" + System.currentTimeMillis(); String dirPath2 = "sample/directory/path/sample2" + System.currentTimeMillis(); String fileName = "subfile.txt"; String subDirectory = "subdirectory"; createDirectory(dirPath1); createFile(dirPath1 + "/" + fileName, "Sample file content"); createDirectory(dirPath2 + "/" + subDirectory); WebRequest request = getDeleteFilesRequest(dirPath1); WebResponse response = webConversation.getResponse(request); assertEquals( "Could not delete directory with file", HttpURLConnection.HTTP_OK, response.getResponseCode()); assertFalse( "Delete directory with file request returned OK, but the file still exists", checkDirectoryExists(dirPath1)); request = getDeleteFilesRequest(dirPath2); response = webConversation.getResponse(request); assertEquals( "Could not delete directory with subdirectory", HttpURLConnection.HTTP_OK, response.getResponseCode()); assertFalse( "Delete directory with subdirectory request returned OK, but the file still exists", checkDirectoryExists(dirPath2)); }
/** * @param jspName jsp name, with full path * @throws Exception any axception thrown during test. */ @Test public void doTest() throws Exception { WebRequest request = new GetMethodWebRequest(getJspUrl(getJspName())); WebResponse response = runner.getResponse(request); if (log.isDebugEnabled()) { log.debug(response.getText()); } WebTable[] tables = response.getTables(); Assert.assertEquals("Wrong number of tables.", 1, tables.length); Assert.assertEquals("Wrong number of columns.", 2, tables[0].getColumnCount()); Assert.assertEquals("Wrong number of rows.", 3, tables[0].getRowCount()); // 2 plus header TableRow[] rows = tables[0].getRows(); Assert.assertEquals("Wrong id for row 1", "idcamel0", rows[1].getID()); Assert.assertEquals("Wrong id for row 2", "idcamel1", rows[2].getID()); Assert.assertEquals("Wrong class for row 1", "odd classcamel0", rows[1].getClassName()); Assert.assertEquals("Wrong class for row 2", "even classcamel1", rows[2].getClassName()); }
@Test public void testCopyFileOverwrite() throws Exception { String directoryPath = "/testCopyFile/directory/path" + System.currentTimeMillis(); String sourcePath = directoryPath + "/source.txt"; String destName = "destination.txt"; String destPath = directoryPath + "/" + destName; createDirectory(directoryPath); createFile(sourcePath, "This is the contents"); createFile(destPath, "Original file"); // with no-overwrite, copy should fail JSONObject requestObject = new JSONObject(); addSourceLocation(requestObject, sourcePath); WebRequest request = getPostFilesRequest(directoryPath, requestObject.toString(), "destination.txt"); request.setHeaderField("X-Create-Options", "copy,no-overwrite"); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_PRECON_FAILED, response.getResponseCode()); // now omit no-overwrite and copy should succeed and return 200 instead of 201 request = getPostFilesRequest(directoryPath, requestObject.toString(), "destination.txt"); request.setHeaderField("X-Create-Options", "copy"); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject responseObject = new JSONObject(response.getText()); checkFileMetadata(responseObject, destName, null, null, null, null, null, null, null); assertTrue(checkFileExists(sourcePath)); assertTrue(checkFileExists(destPath)); }
@SuppressWarnings("unchecked") public List<Group> getAllGroups() throws Exception { // {"vfwebqq":"3e99140e076c4dc3c8a774f1e3a37518055f3f39f94428a8eaa07c1f7c0ebfea3a18792878f2c061"} String url = "http://s.web2.qq.com/api/get_group_name_list_mask2"; String content = "{\"vfwebqq\":\"" + vfwebqq + "\"}"; PostMethodWebRequest post = new PostMethodWebRequest(url); post.setParameter("r", content); wc.setHeaderField("Referer", "http://s.web2.qq.com/proxy.html?v=20110412001&callback=1&id=2"); WebResponse rs = wc.getResponse(post); log.info("sendMsg response:" + rs.getText()); List<Group> groups = new ArrayList<Group>(); JSONObject retJson = new JSONObject(rs.getText()); if (retJson.getInt("retcode") == 0) { JSONArray infos = retJson.getJSONObject("result").getJSONArray("gnamelist"); for (int i = 0; i < infos.length(); i++) { JSONObject obj = infos.getJSONObject(i); Group group = new Group(); group.setGid(obj.getString("gid")); group.setName(obj.getString("name")); group.setCode(obj.getString("code")); groups.add(group); } } return groups; }