public void Test_Construction() { _encryptedWorkspace = new EncryptedWorkspace(_workspace, _encryptionStrategy); Assert.Equal("Name", _encryptedWorkspace.Name); Assert.Equal("Description", _encryptedWorkspace.Description); Assert.Equal("1.2.3", _encryptedWorkspace.Version); Assert.Equal(123, _encryptedWorkspace.Revision); Assert.Equal("Agent", _encryptedWorkspace.LastModifiedAgent); Assert.Equal("User", _encryptedWorkspace.LastModifiedUser); Assert.Equal(1234, _encryptedWorkspace.Id); Assert.Equal("*****@*****.**", _encryptedWorkspace.Configuration.Users.First().Username); Assert.Null(_workspace.Configuration); Assert.Same(_workspace, _encryptedWorkspace.Workspace); Assert.Same(_encryptionStrategy, _encryptedWorkspace.EncryptionStrategy); JsonWriter jsonWriter = new JsonWriter(false); StringWriter stringWriter = new StringWriter(); jsonWriter.WriteAsync(_workspace, stringWriter).Wait(); Assert.Equal(stringWriter.ToString(), _encryptedWorkspace.Plaintext); Assert.Equal(_encryptionStrategy.Encrypt(stringWriter.ToString()), _encryptedWorkspace.Ciphertext); }
public void PutWorkspace(long workspaceId, Workspace workspace) { if (workspace == null) { throw new ArgumentException("A workspace must be supplied"); } else if (workspaceId <= 0) { throw new ArgumentException("The workspace ID must be set"); } if (MergeFromRemote) { Workspace remoteWorkspace = GetWorkspace(workspaceId); if (remoteWorkspace != null) { workspace.Views.CopyLayoutInformationFrom(remoteWorkspace.Views); workspace.Views.Configuration.CopyConfigurationFrom(remoteWorkspace.Views.Configuration); } } workspace.Id = workspaceId; using (WebClient webClient = new WebClient()) { try { string httpMethod = "PUT"; string path = WorkspacePath + workspaceId; string workspaceAsJson = ""; using (StringWriter stringWriter = new StringWriter()) { if (EncryptionStrategy == null) { JsonWriter jsonWriter = new JsonWriter(false); jsonWriter.Write(workspace, stringWriter); } else { EncryptedWorkspace encryptedWorkspace = new EncryptedWorkspace(workspace, EncryptionStrategy); EncryptedJsonWriter jsonWriter = new EncryptedJsonWriter(false); jsonWriter.Write(encryptedWorkspace, stringWriter); } stringWriter.Flush(); workspaceAsJson = stringWriter.ToString(); System.Console.WriteLine(workspaceAsJson); } AddHeaders(webClient, httpMethod, path, workspaceAsJson, "application/json; charset=UTF-8"); string response = webClient.UploadString(this.Url + path, httpMethod, workspaceAsJson); System.Console.WriteLine(response); } catch (Exception e) { throw new StructurizrClientException("There was an error putting the workspace: " + e.Message, e); } } }
/// <summary> /// Gets the workspace with the given ID. /// </summary> /// <param name="workspaceId">The workspace ID.</param> /// <returns>A Workspace object.</returns> public async Task <Workspace> GetWorkspaceAsync(long workspaceId) { if (workspaceId <= 0) { throw new ArgumentException("The workspace ID must be a positive integer."); } using (HttpClient httpClient = createHttpClient()) { string httpMethod = "GET"; string path = WorkspacePath + workspaceId; AddHeaders(httpClient, httpMethod, new Uri(Url + path).AbsolutePath, "", ""); var response = await httpClient.GetAsync(Url + path); if (response.StatusCode != HttpStatusCode.OK) { string jsonResponse = response.Content.ReadAsStringAsync().Result; ApiResponse apiResponse = ApiResponse.Parse(jsonResponse); throw new StructurizrClientException(apiResponse.Message); } string json = await response.Content.ReadAsStringAsync(); ArchiveWorkspace(workspaceId, json); if (EncryptionStrategy == null) { return(await new JsonReader().ReadAsync(new StringReader(json))); } else { EncryptedWorkspace encryptedWorkspace = await new EncryptedJsonReader().ReadAsync(new StringReader(json)); if (encryptedWorkspace.EncryptionStrategy != null) { encryptedWorkspace.EncryptionStrategy.Passphrase = this.EncryptionStrategy.Passphrase; return(encryptedWorkspace.Workspace); } else { // this workspace isn't encrypted, even though the client has an encryption strategy set return(await new JsonReader().ReadAsync(new StringReader(json))); } } } }
public void Test_Workspace_ReturnsTheWorkspace_WhenACipherextIsSpecified() { JsonWriter jsonWriter = new JsonWriter(false); StringWriter stringWriter = new StringWriter(); jsonWriter.Write(_workspace, stringWriter); string expected = stringWriter.ToString(); _encryptedWorkspace = new EncryptedWorkspace(); _encryptedWorkspace.EncryptionStrategy = _encryptionStrategy; _encryptedWorkspace.Ciphertext = _encryptionStrategy.Encrypt(expected); _workspace = _encryptedWorkspace.Workspace; Assert.Equal("Name", _workspace.Name); stringWriter = new StringWriter(); jsonWriter.Write(_workspace, stringWriter); Assert.Equal(expected, stringWriter.ToString()); }
/// <summary> /// Updates the given workspace. /// </summary> /// <param name="workspaceId">The workspace ID.</param> /// <param name="workspace">The workspace to be updated.</param> public void PutWorkspace(long workspaceId, Workspace workspace) { if (workspace == null) { throw new ArgumentException("The workspace must not be null."); } else if (workspaceId <= 0) { throw new ArgumentException("The workspace ID must be a positive integer."); } if (MergeFromRemote) { Workspace remoteWorkspace = GetWorkspace(workspaceId); if (remoteWorkspace != null) { workspace.Views.CopyLayoutInformationFrom(remoteWorkspace.Views); workspace.Views.Configuration.CopyConfigurationFrom(remoteWorkspace.Views.Configuration); } } workspace.Id = workspaceId; workspace.LastModifiedDate = DateTime.UtcNow; workspace.LastModifiedAgent = getAgentName(); workspace.LastModifiedUser = getUser(); using (HttpClient httpClient = createHttpClient()) { try { string httpMethod = "PUT"; string path = WorkspacePath + workspaceId; string workspaceAsJson = ""; using (StringWriter stringWriter = new StringWriter()) { if (EncryptionStrategy == null) { JsonWriter jsonWriter = new JsonWriter(false); jsonWriter.Write(workspace, stringWriter); } else { EncryptedWorkspace encryptedWorkspace = new EncryptedWorkspace(workspace, EncryptionStrategy); EncryptedJsonWriter jsonWriter = new EncryptedJsonWriter(false); jsonWriter.Write(encryptedWorkspace, stringWriter); } stringWriter.Flush(); workspaceAsJson = stringWriter.ToString(); System.Console.WriteLine(workspaceAsJson); } AddHeaders(httpClient, httpMethod, new Uri(Url + path).AbsolutePath, workspaceAsJson, "application/json; charset=UTF-8"); HttpContent content = new StringContent(workspaceAsJson, Encoding.UTF8, "application/json"); content.Headers.ContentType.CharSet = "UTF-8"; string contentMd5 = new Md5Digest().Generate(workspaceAsJson); string contentMd5Base64Encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(contentMd5)); content.Headers.ContentMD5 = Encoding.UTF8.GetBytes(contentMd5); var response = httpClient.PutAsync(this.Url + path, content); string responseContent = response.Result.Content.ReadAsStringAsync().Result; System.Console.WriteLine(responseContent); if (response.Result.StatusCode != HttpStatusCode.OK) { ApiResponse apiResponse = ApiResponse.Parse(responseContent); throw new StructurizrClientException(apiResponse.Message); } } catch (Exception e) { throw new StructurizrClientException("There was an error putting the workspace: " + e.Message, e); } } }