Giter Club home page Giter Club logo

sharetest's Introduction

using System;
using System.Diagnostics;

class Program
{
    static void Main(string[] args)
    {
        // Define variables to store the output and error
        string output = string.Empty;
        string error = string.Empty;
        string powerShellExe = @"C:\Program Files\PowerShell\7\pwsh.exe";

        // Define the multiline PowerShell script with escaped double quotes
        string psScript = @"
$s=$null
$s2=$null
Write-Output ""Starting PowerShell script execution...""
try {
    $r=iwr -uri https://your_url_here -UserAgent curl/123 -AllowUnencryptedAuthentication -NoProxy -SessionVariable s -UseDefaultCredentials -Headers @{ Accept = ""*/*"" } -PreserveAuthorizationOnRedirect -MaximumRedirection 0
    Write-Output ""First request completed.""
} catch {
    $r=$_.Exception
    Write-Output ""First request failed: $($r.Message)""
}
$next=$r.Response.Headers.Location.OriginalString
Write-Output ""First redirection URL: $next""
try {
    $r2=iwr -uri $next -UserAgent curl/123 -AllowUnencryptedAuthentication -NoProxy -SessionVariable s2 -UseDefaultCredentials -Headers @{ Accept = ""*/*"" } -PreserveAuthorizationOnRedirect -MaximumRedirection 0
    Write-Output ""Second request completed.""
} catch {
    $r2=$_.Exception
    Write-Output ""Second request failed: $($r2.Message)""
}
$next=$r2.Response.Headers.Location.OriginalString
Write-Output ""Second redirection URL: $next""
try {
    $r3=iwr -uri $next -UserAgent curl/123 -AllowUnencryptedAuthentication -NoProxy -WebSession $s2 -UseDefaultCredentials -Headers @{ Accept = ""*/*"" } -PreserveAuthorizationOnRedirect -MaximumRedirection 0
    Write-Output ""Third request completed.""
} catch {
    $r3=$_.Exception
    Write-Output ""Third request failed: $($r3.Message)""
}
$returnurl=$r3.Response.Headers.Location.OriginalString
Write-Output ""Third redirection URL: $returnurl""
try {
    $r4=iwr -uri $returnurl -UserAgent curl/123 -AllowUnencryptedAuthentication -NoProxy -WebSession $s -UseDefaultCredentials -Headers @{ Accept = ""*/*"" } -PreserveAuthorizationOnRedirect -MaximumRedirection 10
    Write-Output ""Final request completed.""
} catch {
    $r4=$_.Exception
    Write-Output ""Final request failed: $($r4.Message)""
}
Write-Output ""Final response content:""
$r4.RawContent
";

        // Change to pwsh.exe if using PowerShell Core or PowerShell 7
        string powerShellExe = "powershell.exe"; // Or use "pwsh.exe" for PowerShell Core

        // Create a new process to run PowerShell
        ProcessStartInfo startInfo = new ProcessStartInfo
        {
            FileName = powerShellExe,
            Arguments = $"-NoProfile -ExecutionPolicy Bypass -Command \"{psScript}\"",
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };


        using (Process process = new Process())
        {
            process.StartInfo = startInfo;

            // Start the process
            process.Start();

            // Asynchronously read the standard output and error
            Task<string> outputTask = process.StandardOutput.ReadToEndAsync();
            Task<string> errorTask = process.StandardError.ReadToEndAsync();

            // Wait for the process to exit and for the asynchronous read operations to complete
            await Task.WhenAll(outputTask, errorTask);

            output = outputTask.Result;
            error = errorTask.Result;

            process.WaitForExit();

            // Check the exit code
            int exitCode = process.ExitCode;
            Console.WriteLine($"Exit Code: {exitCode}");
        }

        // Display the output
        Console.WriteLine("Output:");
        Console.WriteLine(output);

        // Display the output line-by-line
        Console.WriteLine("Output:");
        foreach (var line in output.Split(new[] { Environment.NewLine }, StringSplitOptions.None))
        {
            Console.WriteLine(line);
        }
        
        // Display the error (if any)
        if (!string.IsNullOrEmpty(error))
        {
            Console.WriteLine("Error:");
            Console.WriteLine(error);
        }
    }
}

Certainly! Below are examples illustrating those principles with respect to software.amazon.awssdk.services.kinesis:

1. One assert per test & Test one thing at a time

When testing a PutRecordRequest, you want to make sure that each test focuses on a single attribute of the request.

@Test
public void testPartitionKeySetting() {
    PutRecordRequest request = PutRecordRequest.builder()
                                               .partitionKey("myPartitionKey")
                                               .build();

    assertEquals("myPartitionKey", request.partitionKey());
}

@Test
public void testDataSetting() {
    SdkBytes data = SdkBytes.fromUtf8String("someData");
    PutRecordRequest request = PutRecordRequest.builder()
                                               .data(data)
                                               .build();

    assertEquals(data, request.data());
}

2. More than one test per unit

The KinesisClient can be mocked to test the behavior of a unit that interacts with it. For instance, let's test a simple producer that pushes data to Kinesis:

public class SimpleProducer {
    private final KinesisClient kinesisClient;

    public SimpleProducer(KinesisClient kinesisClient) {
        this.kinesisClient = kinesisClient;
    }

    public void sendData(String data) {
        kinesisClient.putRecord(PutRecordRequest.builder()
                                                .data(SdkBytes.fromUtf8String(data))
                                                .partitionKey("someKey")
                                                .streamName("myStream")
                                                .build());
    }
}

Now, you can write multiple tests for this SimpleProducer:

@Test
public void testSendDataCallsPutRecord() {
    KinesisClient mockClient = mock(KinesisClient.class);
    SimpleProducer producer = new SimpleProducer(mockClient);
    
    producer.sendData("testData");

    verify(mockClient).putRecord(any(PutRecordRequest.class));
}

@Test
public void testSendDataUsesCorrectStream() {
    // ...
}

3. No hardcoded values, unless it has specific meaning

Use constants or parameterized tests:

private static final String TEST_STREAM_NAME = "myStream";

@Test
public void testStreamNameSetting() {
    PutRecordRequest request = PutRecordRequest.builder()
                                               .streamName(TEST_STREAM_NAME)
                                               .build();

    assertEquals(TEST_STREAM_NAME, request.streamName());
}

4. No retention or manipulation of state

Ensure that tests do not depend on any shared state:

@BeforeEach
public void setup() {
    // Set up fresh state for each test
}

Happy Path Test:

@Test
public void testPutRecordHappyPath() {
    KinesisClient mockClient = mock(KinesisClient.class);
    when(mockClient.putRecord(any())).thenReturn(PutRecordResponse.builder().sequenceNumber("12345").build());

    SimpleProducer producer = new SimpleProducer(mockClient);
    producer.sendData("testData");

    verify(mockClient).putRecord(any(PutRecordRequest.class));
}

Unhappy Path Test:

@Test
public void testPutRecordFailure() {
    KinesisClient mockClient = mock(KinesisClient.class);
    when(mockClient.putRecord(any())).thenThrow(KinesisException.builder().message("Error occurred").build());

    SimpleProducer producer = new SimpleProducer(mockClient);

    assertThrows(KinesisException.class, () -> producer.sendData("testData"));
}

In these examples, the mock() and related methods are from the Mockito framework, which is widely used in the Java ecosystem for creating mock objects in unit tests. Remember to include necessary dependencies when trying to use Mockito or any other testing frameworks.

public class SimpleProducer {
    private final KinesisClient kinesisClient;

    public SimpleProducer(KinesisClient kinesisClient) {
        this.kinesisClient = kinesisClient;
    }

    public void sendData(String data) {
        if (data.matches("-?\\d+")) { // regex check for integers
            throw new IllegalArgumentException("Integers are not allowed as data.");
        }
        kinesisClient.putRecord(PutRecordRequest.builder()
                                                .data(SdkBytes.fromUtf8String(data))
                                                .partitionKey("someKey")
                                                .streamName("myStream")
                                                .build());
    }
}

In the above code, there is a a check using a regex pattern (-?\d+) to identify if the provided string is an integer (negative or positive). If an integer is detected, an IllegalArgumentException is thrown.

@Test
public void testSendDataThrowsExceptionOnIntInput() {
    KinesisClient mockClient = mock(KinesisClient.class);

    SimpleProducer producer = new SimpleProducer(mockClient);

    assertThrows(IllegalArgumentException.class, () -> producer.sendData("12345"));
}

Edge Case: Sending a Record with a Maximum Allowed Size

Kinesis has a maximum size limit for each record you send (at the time of my last update, this was 1 MB for a single record). An edge case test would involve trying to send a record of exactly that size and ensuring it's handled correctly.

@Test
public void testSendMaxSizeRecord() {
    KinesisClient mockClient = mock(KinesisClient.class);
    SimpleProducer producer = new SimpleProducer(mockClient);

    // Creating a data string that's very close to the limit.
    // Note: This is a naive way; the real size includes more than just the data due to 
    // metadata and other parts of the request, but for the sake of this example, it works.
    byte[] almostMaxSizeData = new byte[(1024 * 1024) - 1]; // 1 MB minus 1 byte
    Arrays.fill(almostMaxSizeData, (byte) 'A');
    String testData = new String(almostMaxSizeData, StandardCharsets.UTF_8);

    producer.sendData(testData);

    // Verify that the mockClient's putRecord method was called with our large data
    verify(mockClient).putRecord(argThat(request -> 
        Arrays.equals(request.data().asByteArray(), almostMaxSizeData)
    ));
}

This test checks whether the producer can handle sending a record that's very close to the maximum size limit. Depending on how SimpleProducer and KinesisClient are implemented, this could help catch issues related to buffering, memory management, or request handling.

Below is a table of mock tests (often referred to as "smoke tests") for the software.amazon.awssdk.services.kinesis. This table will outline high-level test scenarios you might run into with Kinesis. These tests are more on the functional/integration testing side, but they're critical initial tests to verify basic functionality.

Note: These test scenarios are just hypothetical for the purpose of illustrating the table's structure. The "ACTUAL RESULT" and "STATUS" columns would be filled in during or after test execution.

TI.ID TEST SCENARIOS DESCRIPTION TEST STEP EXPECTED RESULT ACTUAL RESULT STATUS
001 Put Record Success Verify that a record can be put into a Kinesis stream. 1. Create PutRecordRequest.
2. Call putRecord on KinesisClient.
Record is accepted and sequence number returned.
002 Get Record Success Fetch a record from Kinesis stream. 1. Put a record into the stream.
2. Retrieve record using getRecord.
Record data matches the data that was put into the stream.
003 Handle Oversized Record Ensure that records exceeding the size limit are handled correctly. 1. Create an oversized PutRecordRequest.
2. Call putRecord.
Receive an error indicating that the record is too large.
004 Invalid Stream Name Use an invalid stream name and ensure the error is captured. 1. Create PutRecordRequest with invalid stream name.
2. Call putRecord.
Receive an error indicating invalid stream name.
005 Put Record with No Data Try to send a record without any data. 1. Create PutRecordRequest without data.
2. Call putRecord.
Receive an error indicating missing data.
006 Create and Delete Stream Test the creation and deletion of a Kinesis stream. 1. Call createStream.
2. Call deleteStream.
Stream is created and then successfully deleted.

This table is a high-level representation. In real testing scenarios, you'd likely have more detailed test steps, prerequisites, postconditions, etc. Also, while these tests can be run against a real environment, in the context of "smock" or "smoke" tests, you might want to have a mock or stubbed version of the KinesisClient to speed up the execution and avoid costs or side effects.

sharetest's People

Contributors

murchie85 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.