<commit_before>$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".")

function Stop-Pester($message = "EMERGENCY: Script cannot continue.")
{
	$msg = $message;
	$e = New-CustomErrorRecord -msg $msg -cat OperationStopped -o $msg;
	$PSCmdlet.ThrowTerminatingError($e);
}

Describe -Tags "SpecialOperation.Tests" "SpecialOperation.Tests" {

	Mock Export-ModuleMember { return $null; }
	
	Context "SetCreatedBy.Tests" {
		
		BeforeEach {
			$moduleName = 'biz.dfch.PS.Appclusive.Client';
			Remove-Module $moduleName -ErrorAction:SilentlyContinue;
			Import-Module $moduleName;
			
			$svc = Enter-ApcServer;
		}

		It "SetCreatedBy-WithMissingParameterInBodyFails" -Test {
			# Arrange
			
			
			# Act
			try
			{
				$svc.Core.InvokeEntitySetActionWithVoidResult("SpecialOperations", "SetCreatedBy", @{EntityType = 'AuditTrails'; EntityId = '42'});			
				"No error occurred" | Should Be "An exception was expected but did not occur."
			}
			catch
			{
				# Assert	
				$errorResponse = $error[0].Exception.InnerException.InnerException.Message | ConvertFrom-Json;
				$errorResponse.'odata.error'.message.value -match 'Precondition.+CreatedBy' | Should Be $true
			}
		}
		
		It "SetCreatedBy-WithInvalidEntityTypeInBodyFails" -Test {
			# Arrange
			
			
			# Act
			try
			{
				$svc.Core.InvokeEntitySetActionWithVoidResult("SpecialOperations", "SetCreatedBy", @{EntityType = 'ArbitraryType'; EntityId = '42'; CreatedBy = 'testuser'});			
				"No error occurred" | Should Be "An exception was expected but did not occur."
			}
			catch
			{
				# Assert	
				$errorResponse = $error[0].Exception.InnerException.InnerException.Message | ConvertFrom-Json;
				$errorResponse.'odata.error'.message.value -match 'Assertion.+entityType' | Should Be $true
			}
		}
		
		It "SetCreatedBy-ForKeyNameValueSucceeds" -Test {
			# Arrange
			$creator = 'testuser';
			$value = [guid]::NewGuid().Guid;
			$knv = New-ApcKeyNameValue -Name $value -Key $value -Value $value;
			
			# Act
			$svc.Core.InvokeEntitySetActionWithVoidResult("SpecialOperations", "SetCreatedBy", @{EntityType = 'biz.dfch.CS.Appclusive.Core.OdataServices.Core.KeyNameValue'; EntityId = $knv.Id; CreatedBy = $creator});
			
			# Assert
			$svc = Enter-ApcServer
			$knv = $svc.Core.KeyNameValues.AddQueryOption('$filter', ("Name eq '{0}'" -f $value)) | Select;
			$knv.CreatedBy | Should be $creator;
			
			Remove-ApcKeyNameValue -Name $value -Confirm:$false;
		}
		
		It "SetCreatedBy-ForAuditTrailFails" -Test {
			# Arrange
			
			
			# Act
			try
			{
				$svc.Core.InvokeEntitySetActionWithVoidResult("SpecialOperations", "SetCreatedBy", @{EntityType = 'biz.dfch.CS.Appclusive.Core.OdataServices.Diagnostics.AuditTrail'; EntityId = '42'; CreatedBy = 'testuser'});			
				"No error occurred" | Should Be "An exception was expected but did not occur."
			}
			catch
			{
				# Assert	
				$errorResponse = $error[0].Exception.InnerException.InnerException.Message | ConvertFrom-Json;
				$errorResponse.'odata.error'.message.value -match 'Assertion.+Blacklist.+Contains.+entityType' | Should Be $true
			}
		}
	}
	
	Context "ClearAuditLog.Tests" {
		
		BeforeEach {
			$moduleName = 'biz.dfch.PS.Appclusive.Client';
			Remove-Module $moduleName -ErrorAction:SilentlyContinue;
			Import-Module $moduleName;
			$svc = Enter-ApcServer;
		}
		
		It "ClearAuditLog-DeletesAllAuditTrailEntries" -Test {
			# Arrange
			$value = [guid]::NewGuid().Guid;
			$knv = New-ApcKeyNameValue -Name $value -Key $value -Value $value;
			
			# Act
			$auditTrails = $svc.Diagnostics.AuditTrails | Select;
			$auditTrails.Count | Should Not Be 0;
			
			$svc.Core.InvokeEntitySetActionWithVoidResult("SpecialOperations", "ClearAuditLog", $null);
			
			# Assert	
			$auditTrails = $svc.Diagnostics.AuditTrails | Select;
			$auditTrails.Count | Should Be 0;
			
			Remove-ApcKeyNameValue -Name $value -Confirm:$false;
		}
	}
	
	Context "RaiseUpdateConfigurationEvent.Tests" {
		
		BeforeEach {
			$moduleName = 'biz.dfch.PS.Appclusive.Client';
			Remove-Module $moduleName -ErrorAction:SilentlyContinue;
			Import-Module $moduleName;
			$svc = Enter-ApcServer;
		}
		
		It "RaiseUpdateConfigurationEvent-WritesMessageToTheBus" -Test {
			# Arrange
			$job = Start-Job -ScriptBlock {Import-Module biz.dfch.PS.Azure.ServiceBus.Client; $null = Enter-SBServer; Get-SBMessage -Receivemode ReceiveAndDelete -Facility NOTIFY-ALL -EnsureFacility | Get-SBMessageBody};
			
			# Act
			Sleep -Seconds 3;
			$svc.Core.InvokeEntitySetActionWithVoidResult("SpecialOperations", "RaiseUpdateConfigurationEvent", $null);
			
			# Assert
			Sleep -Seconds 2;
			$jobResult = Get-Job -Id $job.Id | Receive-Job;
			
			$jobResult -match 'UpdateConfigurationEvent.+UpdateConfigurationEventBody' | Should Be $true
		}
	}
}

#
# Copyright 2015 d-fens GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# SIG # Begin signature block<commit_msg>* tests for 'SetTenant' action in SpecialOperationsController * minor improvements of existing tests<commit_after>$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".")

function Stop-Pester($message = "EMERGENCY: Script cannot continue.")
{
	$msg = $message;
	$e = New-CustomErrorRecord -msg $msg -cat OperationStopped -o $msg;
	$PSCmdlet.ThrowTerminatingError($e);
}

Describe -Tags "SpecialOperation.Tests" "SpecialOperation.Tests" {

	Mock Export-ModuleMember { return $null; }
	
	Context "SetCreatedBy.Tests" {
	
		$actionName = 'SetCreatedBy';
		
		BeforeEach {
			$moduleName = 'biz.dfch.PS.Appclusive.Client';
			Remove-Module $moduleName -ErrorAction:SilentlyContinue;
			Import-Module $moduleName;
			
			$svc = Enter-ApcServer;
		}

		It "SetCreatedBy-WithMissingParameterInBodyFails" -Test {
			# Arrange
			
			
			# Act
			try
			{
				$svc.Core.InvokeEntitySetActionWithVoidResult("SpecialOperations", $actionName, @{EntityType = 'AuditTrails'; EntityId = '42'});			
				"No error occurred" | Should Be "An exception was expected but did not occur."
			}
			catch
			{
				# Assert	
				$errorResponse = $error[0].Exception.InnerException.InnerException.Message | ConvertFrom-Json;
				$errorResponse.'odata.error'.message.value -match 'Precondition.+CreatedBy' | Should Be $true
			}
		}
		
		It "SetCreatedBy-WithInvalidEntityTypeInBodyFails" -Test {
			# Arrange
			$creator = 'testuser';
			
			# Act
			try
			{
				$svc.Core.InvokeEntitySetActionWithVoidResult("SpecialOperations", $actionName, @{EntityType = 'ArbitraryType'; EntityId = '42'; CreatedBy = $creator});			
				"No error occurred" | Should Be "An exception was expected but did not occur."
			}
			catch
			{
				# Assert	
				$errorResponse = $error[0].Exception.InnerException.InnerException.Message | ConvertFrom-Json;
				$errorResponse.'odata.error'.message.value -match 'Assertion.+entityType' | Should Be $true
			}
		}
		
		It "SetCreatedBy-ForKeyNameValueSucceeds" -Test {
			# Arrange
			$creator = 'testuser';
			$value = [guid]::NewGuid().Guid;
			$knv = New-ApcKeyNameValue -Name $value -Key $value -Value $value;
			
			# Act
			$svc.Core.InvokeEntitySetActionWithVoidResult("SpecialOperations", $actionName, @{EntityType = 'biz.dfch.CS.Appclusive.Core.OdataServices.Core.KeyNameValue'; EntityId = $knv.Id; CreatedBy = $creator});
			
			# Assert
			$svc = Enter-ApcServer
			$knv = $svc.Core.KeyNameValues.AddQueryOption('$filter', ("Name eq '{0}'" -f $value)) | Select;
			$knv.CreatedBy | Should be $creator;
			
			Remove-ApcKeyNameValue -Name $value -Confirm:$false;
		}
		
		It "SetCreatedBy-ForAuditTrailFails" -Test {
			# Arrange
			$creator = 'testuser';
			
			# Act
			try
			{
				$svc.Core.InvokeEntitySetActionWithVoidResult("SpecialOperations", $actionName, @{EntityType = 'biz.dfch.CS.Appclusive.Core.OdataServices.Diagnostics.AuditTrail'; EntityId = '42'; CreatedBy = $creator});			
				"No error occurred" | Should Be "An exception was expected but did not occur."
			}
			catch
			{
				# Assert	
				$errorResponse = $error[0].Exception.InnerException.InnerException.Message | ConvertFrom-Json;
				$errorResponse.'odata.error'.message.value -match 'Assertion.+Blacklist.+Contains.+entityType' | Should Be $true
			}
		}
	}
	
	Context "ClearAuditLog.Tests" {
		
		$actionName = 'ClearAuditLog';
		
		BeforeEach {
			$moduleName = 'biz.dfch.PS.Appclusive.Client';
			Remove-Module $moduleName -ErrorAction:SilentlyContinue;
			Import-Module $moduleName;
			$svc = Enter-ApcServer;
		}
		
		It "ClearAuditLog-DeletesAllAuditTrailEntries" -Test {
			# Arrange
			$value = [guid]::NewGuid().Guid;
			$knv = New-ApcKeyNameValue -Name $value -Key $value -Value $value;
			
			# Act
			$auditTrails = $svc.Diagnostics.AuditTrails | Select;
			$auditTrails.Count | Should Not Be 0;
			
			$svc.Core.InvokeEntitySetActionWithVoidResult("SpecialOperations", $actionName, $null);
			
			# Assert	
			$auditTrails = $svc.Diagnostics.AuditTrails | Select;
			$auditTrails.Count | Should Be 0;
			
			Remove-ApcKeyNameValue -Name $value -Confirm:$false;
		}
	}
	
	Context "RaiseUpdateConfigurationEvent.Tests" {
		
		$actionName = 'RaiseUpdateConfigurationEvent';
		
		BeforeEach {
			$moduleName = 'biz.dfch.PS.Appclusive.Client';
			Remove-Module $moduleName -ErrorAction:SilentlyContinue;
			Import-Module $moduleName;
			$svc = Enter-ApcServer;
		}
		
		It "RaiseUpdateConfigurationEvent-WritesMessageToTheBus" -Test {
			# Arrange
			$job = Start-Job -ScriptBlock {Import-Module biz.dfch.PS.Azure.ServiceBus.Client; $null = Enter-SBServer; Get-SBMessage -Receivemode ReceiveAndDelete -Facility NOTIFY-ALL -EnsureFacility | Get-SBMessageBody};
			
			# Act
			Sleep -Seconds 3;
			$svc.Core.InvokeEntitySetActionWithVoidResult("SpecialOperations", $actionName, $null);
			
			# Assert
			Sleep -Seconds 2;
			$jobResult = Get-Job -Id $job.Id | Receive-Job;
			
			$jobResult -match 'UpdateConfigurationEvent.+UpdateConfigurationEventBody' | Should Be $true
		}
	}
	
	Context "SetTenant.Tests" {
		
		$actionName = 'SetTenant';
		
		BeforeEach {
			$moduleName = 'biz.dfch.PS.Appclusive.Client';
			Remove-Module $moduleName -ErrorAction:SilentlyContinue;
			Import-Module $moduleName;
			
			$svc = Enter-ApcServer;
		}

		It "SetTenant-WithMissingParameterInBodyFails" -Test {
			# Arrange
			
			
			# Act
			try
			{
				$svc.Core.InvokeEntitySetActionWithVoidResult("SpecialOperations", $actionName, @{EntityType = 'AuditTrails'; EntityId = '42'});			
				"No error occurred" | Should Be "An exception was expected but did not occur."
			}
			catch
			{
				# Assert	
				$errorResponse = $error[0].Exception.InnerException.InnerException.Message | ConvertFrom-Json;
				$errorResponse.'odata.error'.message.value -match 'Precondition.+TenantId' | Should Be $true
			}
		}
		
		It "SetTenant-WithInvalidEntityTypeInBodyFails" -Test {
			# Arrange
			$tenantId = [guid]::NewGuid().Guid;
			
			# Act
			try
			{
				$svc.Core.InvokeEntitySetActionWithVoidResult("SpecialOperations", $actionName, @{EntityType = 'ArbitraryType'; EntityId = '42'; TenantId = $tenantId.ToString()});		
				"No error occurred" | Should Be "An exception was expected but did not occur."
			}
			catch
			{
				# Assert	
				$errorResponse = $error[0].Exception.InnerException.InnerException.Message | ConvertFrom-Json;
				$errorResponse.'odata.error'.message.value -match 'Assertion.+entityType' | Should Be $true
			}
		}

		It "SetTenant-ForKeyNameValueSucceeds" -Test {
			# Arrange
			$tenantId = [guid]::NewGuid().Guid;
			$value = [guid]::NewGuid().Guid;
			$knv = New-ApcKeyNameValue -Name $value -Key $value -Value $value;
			
			# Act
			$svc.Core.InvokeEntitySetActionWithVoidResult("SpecialOperations", $actionName, @{EntityType = 'biz.dfch.CS.Appclusive.Core.OdataServices.Core.KeyNameValue'; EntityId = $knv.Id; TenantId = $tenantId.ToString()});
			
			# Assert
			$svc = Enter-ApcServer
			$knv = $svc.Core.KeyNameValues.AddQueryOption('$filter', ("Name eq '{0}'" -f $value)) | Select;
			$knv.Tid | Should be $tenantId;
			
			Remove-ApcKeyNameValue -Name $value -Confirm:$false;
		}
		
		It "SetTenant-ForAuditTrailFails" -Test {
			# Arrange
			$tenantId = [guid]::NewGuid().Guid;
			
			# Act
			try
			{
				$svc.Core.InvokeEntitySetActionWithVoidResult("SpecialOperations", $actionName, @{EntityType = 'biz.dfch.CS.Appclusive.Core.OdataServices.Diagnostics.AuditTrail'; EntityId = '42'; TenantId = $tenantId.ToString()});			
				"No error occurred" | Should Be "An exception was expected but did not occur."
			}
			catch
			{
				# Assert	
				$errorResponse = $error[0].Exception.InnerException.InnerException.Message | ConvertFrom-Json;
				$errorResponse.'odata.error'.message.value -match 'Assertion.+Blacklist.+Contains.+entityType' | Should Be $true
			}
		}
	}
}

#
# Copyright 2015 d-fens GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# SIG # Begin signature block
{'fraction_non_alphanumeric': 0.12464975932179036, 'fraction_numerical': 0.0038795890509375672, 'mean_word_length': 3.765491270112975, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '640303', 'n_tokens_mistral': 5307, 'n_tokens_neox': 4509, 'n_words': 974}