Skip to content

Commit

Permalink
fix example, IT and make classes immutable
Browse files Browse the repository at this point in the history
Signed-off-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>
  • Loading branch information
mukundansundar committed Jan 19, 2023
1 parent 89daf58 commit 99639df
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public static void main(String[] args) throws Exception {
put("dataKey", val);
}
});
BulkPublishEntry<CloudEvent<Map<String, String>>> entry = new BulkPublishEntry<>();
entry.setEntryId("" + (i + 1)).setEvent(cloudEvent).setContentType(CloudEvent.CONTENT_TYPE);
BulkPublishEntry<CloudEvent<Map<String, String>>> entry = new BulkPublishEntry<>(
"" + (i + 1), cloudEvent, CloudEvent.CONTENT_TYPE, null);
entries.add(entry);
}
BulkPublishRequest<CloudEvent<Map<String, String>>> request = new BulkPublishRequest<>(PUBSUB_NAME, TOPIC_NAME,
Expand Down
2 changes: 1 addition & 1 deletion examples/src/main/java/io/dapr/examples/pubsub/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ sleep: 15
-->

```bash
dapr run --components-path ./components/pubsub --app-id publisher -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.pubsub.http.Publisher testingtopicbulk
dapr run --components-path ./components/pubsub --app-id publisher -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.pubsub.Publisher testingtopicbulk
```

<!-- END_STEP -->
Expand Down
11 changes: 6 additions & 5 deletions sdk-tests/src/test/java/io/dapr/it/pubsub/http/PubSubIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import static io.dapr.it.TestUtils.assertThrowsDaprException;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -207,7 +208,7 @@ public String getContentType() {
BulkPublishResponse response = previewClient.publishEvents(PUBSUB_NAME, TOPIC_BULK, "", messages).block();
System.out.println(String.format("Published %d messages to topic '%s' pubsub_name '%s'",
NUM_MESSAGES, TOPIC_BULK, PUBSUB_NAME));
Assert.assertNotNull("expected not null bulk publish response", response);
assertNotNull("expected not null bulk publish response", response);
Assert.assertEquals("expected no failures in the response", 0, response.getFailedEntries().size());

//Publishing an object.
Expand All @@ -216,15 +217,15 @@ public String getContentType() {
response = previewClient.publishEvents(PUBSUB_NAME, TOPIC_BULK,
"application/json", Collections.singletonList(object)).block();
System.out.println("Published one object.");
Assert.assertNotNull("expected not null bulk publish response", response);
assertNotNull("expected not null bulk publish response", response);
Assert.assertEquals("expected no failures in the response", 0, response.getFailedEntries().size());

//Publishing a single byte: Example of non-string based content published
previewClient.publishEvents(PUBSUB_NAME, TOPIC_BULK, "",
Collections.singletonList(new byte[]{1})).block();
System.out.println("Published one byte.");

Assert.assertNotNull("expected not null bulk publish response", response);
assertNotNull("expected not null bulk publish response", response);
Assert.assertEquals("expected no failures in the response", 0, response.getFailedEntries().size());

CloudEvent cloudEvent = new CloudEvent();
Expand All @@ -241,7 +242,7 @@ public String getContentType() {

//Publishing a cloud event.
previewClient.publishEvents(req).block();
Assert.assertNotNull("expected not null bulk publish response", response);
assertNotNull("expected not null bulk publish response", response);
Assert.assertEquals("expected no failures in the response", 0, response.getFailedEntries().size());

System.out.println("Published one cloud event.");
Expand Down Expand Up @@ -750,7 +751,7 @@ public void testLongValues() throws Exception {
"messages/testinglongvalues",
null,
HttpExtension.GET, CLOUD_EVENT_LONG_LIST_TYPE_REF).block();
Assert.assertNotNull(messages);
assertNotNull(messages);
for (CloudEvent<ConvertToLong> message : messages) {
actual.add(message.getData());
}
Expand Down
6 changes: 3 additions & 3 deletions sdk/src/main/java/io/dapr/client/DaprClientGrpc.java
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,9 @@ public <T> Mono<BulkPublishResponse<T>> publishEvents(BulkPublishRequest<T> requ
it -> {
List<BulkPublishResponseFailedEntry<T>> entries = new ArrayList<>();
for (DaprProtos.BulkPublishResponseFailedEntry entry : it.getFailedEntriesList()) {
BulkPublishResponseFailedEntry<T> domainEntry = new BulkPublishResponseFailedEntry<T>();
domainEntry.setEntry(entryMap.get(entry.getEntryId()));
domainEntry.setErrorMessage(entry.getError());
BulkPublishResponseFailedEntry<T> domainEntry = new BulkPublishResponseFailedEntry<T>(
entryMap.get(entry.getEntryId()),
entry.getError());
entries.add(domainEntry);
}
if (entries.size() > 0) {
Expand Down
40 changes: 15 additions & 25 deletions sdk/src/main/java/io/dapr/client/domain/BulkPublishEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,40 @@
*
* @param <T> Type of the event that is part of the request.
*/
public class BulkPublishEntry<T> {
public final class BulkPublishEntry<T> {
/**
* The ID uniquely identifying this particular request entry across the request and scoped for this request only.
*/
private String entryId;
private final String entryId;

/**
* The event to be published.
*/
private T event;
private final T event;

/**
* The content type of the event to be published. Uses MIME style content-type values.
*/
private String contentType;
private final String contentType;

/**
* The metadata set for this particular event.
* Any particular values in this metadata overrides the request metadata present in BulkPublishRequest.
*/
private Map<String, String> metadata;
private final Map<String, String> metadata;

/**
* Default constructor for the BulkPublishRequestEntry object.
* Constructor for the BulkPublishRequestEntry object.
*
* @param entryId A request scoped ID uniquely identifying this entry in the BulkPublishRequest.
* @param event Event to be published.
* @param contentType Content Type of the event to be published in MIME format.
*/
public BulkPublishEntry() {
public BulkPublishEntry(String entryId, T event, String contentType) {
this.entryId = entryId;
this.event = event;
this.contentType = contentType;
this.metadata = Collections.unmodifiableMap(new HashMap<>());
}

/**
Expand All @@ -70,35 +78,17 @@ public String getEntryId() {
return entryId;
}

public BulkPublishEntry<T> setEntryId(String entryId) {
this.entryId = entryId;
return this;
}

public T getEvent() {
return event;
}

public BulkPublishEntry<T> setEvent(T event) {
this.event = event;
return this;
}

public String getContentType() {
return contentType;
}

public BulkPublishEntry<T> setContentType(String contentType) {
this.contentType = contentType;
return this;
}

public Map<String, String> getMetadata() {
return metadata;
}

public BulkPublishEntry<T> setMetadata(Map<String, String> metadata) {
this.metadata = metadata == null ? null : Collections.unmodifiableMap(metadata);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*
* @param <T> Type parameter of the event.
*/
public class BulkPublishRequest<T> {
public final class BulkPublishRequest<T> {

/**
* The name of the pubsub to publish to.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class BulkPublishResponse<T> {
/**
* List of {@link BulkPublishResponseFailedEntry} objects that have failed to publish.
*/
private List<BulkPublishResponseFailedEntry<T>> failedEntries;
private final List<BulkPublishResponseFailedEntry<T>> failedEntries;

/**
* Default constructor for class.
Expand All @@ -48,9 +48,4 @@ public BulkPublishResponse(List<BulkPublishResponseFailedEntry<T>> failedEntries
public List<BulkPublishResponseFailedEntry<T>> getFailedEntries() {
return failedEntries;
}

public BulkPublishResponse<T> setFailedEntries(List<BulkPublishResponseFailedEntry<T>> failedEntries) {
this.failedEntries = failedEntries == null ? new ArrayList<>() : Collections.unmodifiableList(failedEntries);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,32 @@
/**
* Class representing the entry that failed to be published using BulkPublishRequest.
*/
public class BulkPublishResponseFailedEntry<T> {
public final class BulkPublishResponseFailedEntry<T> {
/**
* The entry that failed to be published.
*/
private BulkPublishEntry<T> entry;
private final BulkPublishEntry<T> entry;

/**
* Error message as to why the entry failed to publish.
*/
private String errorMessage;
private final String errorMessage;

public BulkPublishEntry<T> getEntry() {
return entry;
/**
* Constructor for BulkPublishResponseFailedEntry.
* @param entry The entry that has failed.
* @param errorMessage The error message for why the entry failed.
*/
public BulkPublishResponseFailedEntry(BulkPublishEntry<T> entry, String errorMessage) {
this.entry = entry;
this.errorMessage = errorMessage;
}

public BulkPublishResponseFailedEntry setEntry(BulkPublishEntry<T> entry) {
this.entry = entry;
return this;
public BulkPublishEntry<T> getEntry() {
return entry;
}

public String getErrorMessage() {
return errorMessage;
}

public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
}
39 changes: 0 additions & 39 deletions sdk/src/test/java/io/dapr/client/domain/BulkPublishEntryTest.java

This file was deleted.

0 comments on commit 99639df

Please sign in to comment.