Skip to content

Instantly share code, notes, and snippets.

@albertywu
Created May 9, 2023 22:28
Show Gist options
  • Select an option

  • Save albertywu/f71ddefac853a6e2ecb8d33dbd505faf to your computer and use it in GitHub Desktop.

Select an option

Save albertywu/f71ddefac853a6e2ecb8d33dbd505faf to your computer and use it in GitHub Desktop.
gpt-generated tests
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
import software.amazon.awssdk.services.location.LocationAsyncClient;
import software.amazon.awssdk.services.location.model.Place;
import software.amazon.awssdk.services.location.model.SearchPlaceIndexForTextResponse;
import software.amazon.awssdk.services.location.model.SearchPlaceIndexForTextResult;
import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.CompletableFuture;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
public class AddressNormalizerTest {
@Mock
private LocationAsyncClient locationAsyncClient;
private AddressNormalizer addressNormalizer;
@BeforeEach
void setUp() {
addressNormalizer = new AddressNormalizer(locationAsyncClient);
}
@Test
void testGetGeocode() {
String address = "123 Main St";
String formattedAddress = "123 Main St, Springfield, MA";
SearchPlaceIndexForTextResult result = SearchPlaceIndexForTextResult.builder()
.place(Place.builder().label(formattedAddress).build())
.build();
SearchPlaceIndexForTextResponse response = SearchPlaceIndexForTextResponse.builder()
.results(Collections.singletonList(result))
.build();
when(locationAsyncClient.searchPlaceIndexForText(any())).thenReturn(CompletableFuture.completedFuture(response));
StepVerifier.create(addressNormalizer.getGeocode(address))
.expectNext(formattedAddress)
.verifyComplete();
}
@Test
void testGetGeocode_NoResults() {
String address = "123 Main St";
SearchPlaceIndexForTextResponse response = SearchPlaceIndexForTextResponse.builder()
.results(Collections.emptyList())
.build();
when(locationAsyncClient.searchPlaceIndexForText(any())).thenReturn(CompletableFuture.completedFuture(response));
StepVerifier.create(addressNormalizer.getGeocode(address))
.expectError(NoSuchElementException.class)
.verify();
}
@Test
void testNormalize() {
String address1 = "123 Main St";
String address2 = "456 Elm St";
String formattedAddress1 = "123 Main St, Springfield, MA";
String formattedAddress2 = "456 Elm St, Springfield, MA";
SearchPlaceIndexForTextResult result1 = SearchPlaceIndexForTextResult.builder()
.place(Place.builder().label(formattedAddress1).build())
.build();
SearchPlaceIndexForTextResult result2 = SearchPlaceIndexForTextResult.builder()
.place(Place.builder().label(formattedAddress2).build())
.build();
SearchPlaceIndexForTextResponse response1 = SearchPlaceIndexForTextResponse.builder()
.results(Collections.singletonList(result1))
.build();
SearchPlaceIndexForTextResponse response2 = SearchPlaceIndexForTextResponse.builder()
.results(Collections.singletonList(result2))
.build();
when(locationAsyncClient.searchPlaceIndexForText(any())).thenReturn(
CompletableFuture.completedFuture(response1),
CompletableFuture.completedFuture(response2)
);
StepVerifier.create(addressNormalizer.normalize(Arrays.asList(address1, address2)))
.expectNext(formattedAddress1, formattedAddress2)
.verifyComplete();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment