/** Fails if the map is not empty. */ @CanIgnoreReturnValue public Ordered containsExactly() { return containsExactlyEntriesIn(ImmutableMap.of()); }
/** Fails if the map does not contain exactly the given set of entries in the given map. */ @CanIgnoreReturnValue public Ordered containsExactlyEntriesIn(Map<?, ?> expectedMap) { return delegate().containsExactlyEntriesIn(expectedMap); }
/** * Fails if the map does not contain exactly the given set of key/value pairs. * * <p><b>Warning:</b> the use of varargs means that we cannot guarantee an equal number of * key/value pairs at compile time. Please make sure you provide varargs in key/value pairs! * * <p>The arguments must not contain duplicate keys. */ @CanIgnoreReturnValue public Ordered containsExactly(@NullableDecl Object k0, @NullableDecl Object v0, Object... rest) { return containsExactlyEntriesIn(accumulateMap(k0, v0, rest)); }
@Test public void testNewExtraHeaders() { SpannerMetadataProvider metadataProvider = SpannerMetadataProvider.create(ImmutableMap.<String, String>of(), "header1"); Map<String, List<String>> extraHeaders = metadataProvider.newExtraHeaders(null, "value1"); assertThat(extraHeaders) .containsExactlyEntriesIn( ImmutableMap.<String, List<String>>of("header1", ImmutableList.<String>of("value1"))); }
@SuppressWarnings("unchecked") @Test public void data() { webSocketFactory.webSocket.listener.onMessage(webSocketFactory.webSocket, "{\"type\":\"data\",\"id\":\"subscriptionId\",\"payload\":{\"data\":{\"commentAdded\":{\"__typename\":\"Comment\",\"id\":10,\"content\":\"test10\"}}}}"); assertThat(transportCallback.lastMessage).isInstanceOf(OperationServerMessage.Data.class); assertThat(((OperationServerMessage.Data) transportCallback.lastMessage).id).isEqualTo("subscriptionId"); assertThat((Map<String, Object>) ((Map<String, Object>) ((OperationServerMessage.Data) transportCallback.lastMessage).payload.get("data")).get("commentAdded")) .containsExactlyEntriesIn(new UnmodifiableMapBuilder<String, Object>() .put("__typename", "Comment") .put("id", BigDecimal.valueOf(10)) .put("content", "test10") .build() ); }
@Test public void builder() { String host = "http://localhost:8000/"; String projectId = "test-project"; Map<String, String> labels = new HashMap<>(); labels.put("env", "dev"); SpannerOptions options = SpannerOptions.newBuilder() .setHost(host) .setProjectId(projectId) .setPrefetchChunks(2) .setSessionLabels(labels) .build(); assertThat(options.getHost()).isEqualTo(host); assertThat(options.getProjectId()).isEqualTo(projectId); assertThat(options.getPrefetchChunks()).isEqualTo(2); assertThat(options.getSessionLabels()).containsExactlyEntriesIn(labels); }
@Test public void append() { Statement stmt = Statement.newBuilder("SELECT Name FROM Users") .append(" WHERE Id = @id") .bind("id") .to(1234) .append(" AND Status = @status") .bind("status") .to("ACTIVE") .build(); String expectedSql = "SELECT Name FROM Users WHERE Id = @id AND Status = @status"; assertThat(stmt.getSql()).isEqualTo(expectedSql); assertThat(stmt.hasBinding("id")).isTrue(); assertThat(stmt.hasBinding("status")).isTrue(); assertThat(stmt.getParameters()) .containsExactlyEntriesIn( ImmutableMap.of("id", Value.int64(1234), "status", Value.string("ACTIVE"))); assertThat(stmt.toString()).startsWith(expectedSql); assertThat(stmt.toString()).contains("id: 1234"); assertThat(stmt.toString()).contains("status: ACTIVE"); }
@Test public void mapByName_identicalValues() { Map<GreekLetters, MathsSymbols> enumMap = EnumMapper.mapByName(GreekLetters.class, MathsSymbols.class); assertThat(enumMap) .containsExactlyEntriesIn( ImmutableMap.<GreekLetters, MathsSymbols>builder() .put(GreekLetters.ALPHA, MathsSymbols.ALPHA) .put(GreekLetters.BETA, MathsSymbols.BETA) .put(GreekLetters.GAMMA, MathsSymbols.GAMMA) .build()); }
@Test public void mapByName_differentValuesIgnored() { Map<MoreGreekLetters, GreekLetters> enumMap = EnumMapper.mapByName( MoreGreekLetters.class, GreekLetters.class, ImmutableSet.of(MoreGreekLetters.DELTA)); assertThat(enumMap) .containsExactlyEntriesIn( ImmutableMap.<MoreGreekLetters, GreekLetters>builder() .put(MoreGreekLetters.ALPHA, GreekLetters.ALPHA) .put(MoreGreekLetters.BETA, GreekLetters.BETA) .put(MoreGreekLetters.GAMMA, GreekLetters.GAMMA) .build()); }
/** Fails if the map does not contain exactly the given set of entries in the given map. */ @CanIgnoreReturnValue public Ordered containsExactlyEntriesIn(Map<?, ?> expectedMap) { return delegate().containsExactlyEntriesIn(expectedMap); }
private static void testGetResourceLabels( MonitoredResource resource, Map<String, AttributeValue> expectedLabels) { Map<String, AttributeValue> actualLabels = StackdriverV2ExporterHandler.getResourceLabels(resource); assertThat(actualLabels).containsExactlyEntriesIn(expectedLabels); }
@Test public void setResourceForBuilder_K8sInstanceType() { MonitoredResource.Builder monitoredResourceBuilder = DEFAULT_RESOURCE_WITH_PROJECT_ID.clone(); Map<String, String> resourceLabels = new HashMap<String, String>(); resourceLabels.put(GcpGceInstanceResource.ZONE_KEY, "zone1"); resourceLabels.put(GcpGceInstanceResource.INSTANCE_ID_KEY, "instance1"); resourceLabels.put(K8sContainerResource.CLUSTER_NAME_KEY, "cluster1"); resourceLabels.put(K8sContainerResource.CONTAINER_NAME_KEY, "container1"); resourceLabels.put(K8sContainerResource.NAMESPACE_NAME_KEY, "namespace1"); resourceLabels.put(K8sContainerResource.POD_NAME_KEY, "pod1"); resourceLabels.put("extra_key", "must be ignored"); Map<String, String> expectedResourceLabels = new HashMap<String, String>(); expectedResourceLabels.put("project_id", "proj1"); expectedResourceLabels.put("location", "zone1"); expectedResourceLabels.put("instance_id", "instance1"); expectedResourceLabels.put("cluster_name", "cluster1"); expectedResourceLabels.put("namespace_name", "namespace1"); expectedResourceLabels.put("pod_name", "pod1"); expectedResourceLabels.put("container_name", "container1"); Resource resource = Resource.create(K8sContainerResource.TYPE, resourceLabels); StackdriverExportUtils.setResourceForBuilder(monitoredResourceBuilder, resource); assertThat(monitoredResourceBuilder.getType()).isNotNull(); assertThat(monitoredResourceBuilder.getLabelsMap()).isNotEmpty(); assertThat(monitoredResourceBuilder.getType()).isEqualTo("k8s_container"); assertThat(monitoredResourceBuilder.getLabelsMap().size()).isEqualTo(7); assertThat(monitoredResourceBuilder.getLabelsMap()) .containsExactlyEntriesIn(expectedResourceLabels); }
@Test public void setResourceForBuilder_AwsInstanceType() { MonitoredResource.Builder monitoredResourceBuilder = DEFAULT_RESOURCE_WITH_PROJECT_ID.clone(); Map<String, String> resourceLabels = new HashMap<String, String>(); resourceLabels.put(AwsEc2InstanceResource.REGION_KEY, "region1"); resourceLabels.put(AwsEc2InstanceResource.ACCOUNT_ID_KEY, "account1"); resourceLabels.put(AwsEc2InstanceResource.INSTANCE_ID_KEY, "instance1"); resourceLabels.put("extra_key", "must be ignored"); Map<String, String> expectedResourceLabels = new HashMap<String, String>(); expectedResourceLabels.put("project_id", "proj1"); expectedResourceLabels.put("instance_id", "instance1"); expectedResourceLabels.put("region", "region1"); expectedResourceLabels.put("aws_account", "account1"); Resource resource = Resource.create(AwsEc2InstanceResource.TYPE, resourceLabels); StackdriverExportUtils.setResourceForBuilder(monitoredResourceBuilder, resource); assertThat(monitoredResourceBuilder.getType()).isNotNull(); assertThat(monitoredResourceBuilder.getLabelsMap()).isNotEmpty(); assertThat(monitoredResourceBuilder.getType()).isEqualTo("aws_ec2_instance"); assertThat(monitoredResourceBuilder.getLabelsMap().size()).isEqualTo(4); assertThat(monitoredResourceBuilder.getLabelsMap()) .containsExactlyEntriesIn(expectedResourceLabels); }
@Test public void setResourceForBuilder_GcpInstanceType() { MonitoredResource.Builder monitoredResourceBuilder = DEFAULT_RESOURCE_WITH_PROJECT_ID.clone(); Map<String, String> resourceLabels = new HashMap<String, String>(); resourceLabels.put(GcpGceInstanceResource.PROJECT_ID_KEY, "proj1"); resourceLabels.put(GcpGceInstanceResource.INSTANCE_ID_KEY, "inst1"); resourceLabels.put(GcpGceInstanceResource.ZONE_KEY, "zone1"); resourceLabels.put("extra_key", "must be ignored"); Map<String, String> expectedResourceLabels = new HashMap<String, String>(); expectedResourceLabels.put("project_id", "proj1"); expectedResourceLabels.put("instance_id", "inst1"); expectedResourceLabels.put("zone", "zone1"); Resource resource = Resource.create(GcpGceInstanceResource.TYPE, resourceLabels); StackdriverExportUtils.setResourceForBuilder(monitoredResourceBuilder, resource); assertThat(monitoredResourceBuilder.getType()).isNotNull(); assertThat(monitoredResourceBuilder.getLabelsMap()).isNotEmpty(); assertThat(monitoredResourceBuilder.getType()).isEqualTo("gce_instance"); assertThat(monitoredResourceBuilder.getLabelsMap().size()).isEqualTo(3); assertThat(monitoredResourceBuilder.getLabelsMap()) .containsExactlyEntriesIn(expectedResourceLabels); }
@Test public void testMergeHeaders() { Map<String, List<String>> headers1 = createHeaders("key1", "value1", "key1", "value2"); Map<String, List<String>> headers2 = createHeaders("key1", "value2", "key2", "value2"); Map<String, List<String>> mergedHeaders = Headers.mergeHeaders(headers1, headers2); Map<String, List<String>> expectedHeaders = createHeaders("key1", "value1", "key1", "value2", "key1", "value2", "key2", "value2"); assertThat(mergedHeaders).containsExactlyEntriesIn(expectedHeaders); }
@Test public void testMergeHeadersEmpty() { Map<String, List<String>> headers1 = createHeaders(); Map<String, List<String>> headers2 = createHeaders("key1", "value1", "key2", "value2"); Map<String, List<String>> mergedHeaders = Headers.mergeHeaders(headers1, headers2); Map<String, List<String>> expectedHeaders = createHeaders("key1", "value1", "key2", "value2"); assertThat(mergedHeaders).containsExactlyEntriesIn(expectedHeaders); }
@Test public void testWithExtraHeaders() { Map<String, List<String>> extraHeaders = createTestExtraHeaders("key1", "value1", "key1", "value2"); GrpcCallContext ctx = GrpcCallContext.createDefault().withExtraHeaders(extraHeaders); Map<String, List<String>> moreExtraHeaders = createTestExtraHeaders("key1", "value2", "key2", "value2"); ctx = ctx.withExtraHeaders(moreExtraHeaders); Map<String, List<String>> gotExtraHeaders = ctx.getExtraHeaders(); Map<String, List<String>> expectedExtraHeaders = createTestExtraHeaders( "key1", "value1", "key1", "value2", "key1", "value2", "key2", "value2"); Truth.assertThat(gotExtraHeaders).containsExactlyEntriesIn(expectedExtraHeaders); }
@Test public void testMergeWithExtraHeaders() { Map<String, List<String>> extraHeaders1 = createTestExtraHeaders("key1", "value1", "key1", "value2"); GrpcCallContext ctx1 = GrpcCallContext.createDefault().withExtraHeaders(extraHeaders1); Map<String, List<String>> extraHeaders2 = createTestExtraHeaders("key1", "value2", "key2", "value2"); GrpcCallContext ctx2 = GrpcCallContext.createDefault().withExtraHeaders(extraHeaders2); ApiCallContext mergedApiCallContext = ctx1.merge(ctx2); Truth.assertThat(mergedApiCallContext).isInstanceOf(GrpcCallContext.class); GrpcCallContext mergedGrpcCallContext = (GrpcCallContext) mergedApiCallContext; Map<String, List<String>> gotExtraHeaders = mergedGrpcCallContext.getExtraHeaders(); Map<String, List<String>> expectedExtraHeaders = createTestExtraHeaders( "key1", "value1", "key1", "value2", "key1", "value2", "key2", "value2"); Truth.assertThat(gotExtraHeaders).containsExactlyEntriesIn(expectedExtraHeaders); }
/** * Asserts the given {@link GoogleCloudApiDetailsPanel} shows the proper details for the given * {@link TestCloudLibrary} and {@link TestCloudLibraryClient}. * * @param panel the {@link GoogleCloudApiDetailsPanel} for the details view to assert on * @param library the {@link TestCloudLibrary} whose details should be shown. If using {@link * TestCloudLibrary#createEmpty()}, this method will assert the corresponding fields are * empty. * @param client the {@link TestCloudLibraryClient} whose details should be shown. If using {@link * TestCloudLibraryClient#createEmpty()}, this method will assert the corresponding fields are * empty. */ private static void assertDetailsShownForLibrary( GoogleCloudApiDetailsPanel panel, TestCloudLibrary library, TestCloudLibraryClient client) { assertThat(panel.getNameLabel().getText()).isEqualTo(library.name()); assertThat(panel.getDescriptionTextPane().getText()).isEqualTo(library.description()); // TODO to be refactored into maven extension tests for versions. /* String expectedVersion = client.mavenCoordinates().version().isEmpty() ? "" : "Version: " + client.mavenCoordinates().version(); assertThat(panel.getVersionLabel().getText()).isEqualTo(expectedVersion); */ Map<String, String> actualUrlMap = buildActualUrlMap(panel.getLinksTextPane().getText()); Map<String, String> expectedUrlMap = buildExpectedUrlMap(library, client); assertThat(actualUrlMap).containsExactlyEntriesIn(expectedUrlMap); // TODO(nkibler): Consider refactoring the details panel to allow unit tests to inject fake // icons. Until then, this will always be null. assertThat(panel.getIcon().getIcon()).isNull(); }