public String publish(TopicName topic, Map<String, String> attributes, ByteString data)
      throws PubsubResourceManagerException {
    checkIsUsable();

    if (!createdTopics.contains(topic)) {
      throw new IllegalArgumentException(
          "Can not publish to a topic not managed by this instance.");
    }

    LOG.info("Publishing message with {} bytes to topic '{}'", data.size(), topic);

    PubsubMessage pubsubMessage =
        PubsubMessage.newBuilder().putAllAttributes(attributes).setData(data).build();

    try {
      Publisher publisher = publisherFactory.createPublisher(topic);
      String messageId = publisher.publish(pubsubMessage).get();
      LOG.info("Message published with id '{}'", messageId);
      publisher.shutdown();
      return messageId;
    } catch (Exception e) {
      throw new PubsubResourceManagerException("Error publishing message to Pubsub", e);
    }
  }
@Test
  public void testPublishMessageUnmanagedTopicShouldFail() {
    Map<String, String> attributes = ImmutableMap.of("key1", "value1");
    ByteString data = ByteString.copyFromUtf8("valid message");

    IllegalArgumentException exception =
        assertThrows(
            IllegalArgumentException.class,
            () -> testManager.publish(TOPIC_REFERENCE, attributes, data));
    assertThat(exception).hasMessageThat().contains("topic not managed");
  }