Searching...
lunes, 19 de junio de 2023

Prueba de lectura de Parameter Store en ScalaTest

 import org.scalatest.flatspec.AnyFlatSpec

import org.scalatest.matchers.should.Matchers

import org.scalatestplus.mockito.MockitoSugar

import software.amazon.awssdk.services.ssm.SsmClient

import software.amazon.awssdk.services.ssm.model.{GetParameterRequest, GetParameterResponse, Parameter}

import scala.util.{Success, Try}


trait ParameterStoreReader {

  protected val ssmClient: SsmClient


  def getParameter(parameterName: String): Try[String] = {

    val request = GetParameterRequest.builder().name(parameterName).build()

    val response = ssmClient.getParameter(request)


    Success(response.parameter().value())

  }

}


class ParameterStoreReaderSpec extends AnyFlatSpec with Matchers with MockitoSugar {

  it should "retrieve a parameter from AWS Parameter Store" in {

    val expectedParameterValue = "parameterValue"


    // Create a mock SsmClient

    val ssmClientMock: SsmClient = mock[SsmClient]


    // Create an instance of the trait to be tested, passing the mock SsmClient

    val parameterStoreReader: ParameterStoreReader = new ParameterStoreReader {

      override protected val ssmClient: SsmClient = ssmClientMock

    }


    // Simulate the response from AWS Parameter Store

    val response = GetParameterResponse.builder()

      .parameter(Parameter.builder()

        .name("parameterName")

        .value(expectedParameterValue)

        .build())

      .build()


    // Stub the getParameter method of the mock SsmClient

    when(ssmClientMock.getParameter(any[GetParameterRequest])).thenReturn(response)


    // Test the getParameter method

    val result = parameterStoreReader.getParameter("parameterName")


    result shouldBe Success(expectedParameterValue)


    // Verify that the getParameter method was called with the correct parameter name

    verify(ssmClientMock).getParameter(GetParameterRequest.builder().name("parameterName").build())

  }

}


0 comentarios:

Publicar un comentario

Gracias por participar en esta página.

 
Back to top!