Skip to content

Commit

Permalink
[#1228] Scope templates list by source type
Browse files Browse the repository at this point in the history
  • Loading branch information
lucapette committed Mar 12, 2021
1 parent b8087fc commit 6cd6602
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,8 @@ ResponseEntity<?> deleteTemplate(@RequestBody @Valid DeleteTemplateRequestPayloa
@PostMapping("/templates.list")
ResponseEntity<?> listTemplates(@RequestBody @Valid ListTemplatesRequestPayload payload) {
final List<TemplatePayload> response = stores.getTemplates().stream()
.filter(t -> {
if (payload.getName() == null || payload.getName().isEmpty()) {
return true;
}
return t.getName().contains(payload.getName());
})
.filter(t-> t.getSourceType().equals(payload.getSourceType()))
.filter(t -> (payload.getName() == null || payload.getName().isEmpty()) || t.getName().contains(payload.getName()))
.map(TemplatePayload::fromTemplate)
.collect(toList());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotNull;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ListTemplatesRequestPayload {
private String name;
@NotNull
private String sourceType;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.util.concurrent.TimeUnit;

import static co.airy.test.Timing.retryOnException;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.not;
Expand Down Expand Up @@ -93,23 +92,13 @@ void canManageTemplates() throws Exception {

assertThat(templateId, is(not(nullValue())));

//TODO wait for template to be there
TimeUnit.SECONDS.sleep(5);

webTestHelper.post("/templates.info", "{\"id\":\"" + templateId + "\"}", "user-id")
retryOnException(() -> webTestHelper.post("/templates.info", "{\"id\":\"" + templateId + "\"}", "user-id")
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(is(templateId)))
.andExpect(jsonPath("$.content").value(is(content)))
.andExpect(jsonPath("$.variables.en.salutation").value(is("Hello")))
.andExpect(jsonPath("$.source_type").value(is(sourceType)))
.andExpect(jsonPath("$.name").value(is(name)));

webTestHelper.post("/templates.list", "{\"name\":\"" + name.substring(0, 3) + "\"}", "user-id")
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.length()", is(1)))
.andExpect(jsonPath("$.data[0].id").value(is(templateId)))
.andExpect(jsonPath("$.data[0].source_type").value(is(sourceType)))
.andExpect(jsonPath("$.data[0].name").value(is(name)));
.andExpect(jsonPath("$.name").value(is(name))), "could not find template");

webTestHelper.post("/templates.update", "{\"id\":\"" + templateId + "\", \"name\": \"new-template-name\", \"source_type\": \"google\", \"content\": " + content + "}", "user-id")
.andExpect(status().isOk())
Expand All @@ -119,14 +108,61 @@ void canManageTemplates() throws Exception {
.andExpect(jsonPath("$.variables.en.salutation").value(is("Hello")))
.andExpect(jsonPath("$.name").value(is("new-template-name")));

webTestHelper.post("/templates.info", "{\"id\":\"" + templateId + "\"}", "user-id")
retryOnException(() -> webTestHelper.post("/templates.info", "{\"id\":\"" + templateId + "\"}", "user-id")
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value(is("new-template-name")));
.andExpect(jsonPath("$.name").value(is("new-template-name"))), "could not update template");

webTestHelper.post("/templates.list", "{}", "user-id")
webTestHelper.post("/templates.delete", "{\"id\": \"" + templateId + "\"}", "user-id").andExpect(status().isOk());
}

@Test
void canListTemplates() throws Exception {
for (int i = 0; i < 10; i++) {
final String name = "awesome-template-" + i;
final String sourceType = "google";
final String content = "{\"blueprint\":\"text\",\"payload\":\"[[salutation]]!\"}";
final String payload = "{\"name\":\"" + name + "\",\"source_type\": \"" + sourceType + "\",\"content\":" + content + ",\"variables\": { \"en\": {\"salutation\": \"Hello\"}}}";

webTestHelper.post("/templates.create", payload, "user-id").andExpect(status().isCreated());
}

for (int i = 0; i < 5; i++) {
final String name = "awesome-template-" + i;
final String sourceType = "twilio.sms";
final String content = "{\"blueprint\":\"text\",\"payload\":\"[[salutation]]!\"}";
final String payload = "{\"name\":\"" + name + "\",\"source_type\": \"" + sourceType + "\",\"content\":" + content + ",\"variables\": { \"en\": {\"salutation\": \"Hello\"}}}";

webTestHelper.post("/templates.create", payload, "user-id").andExpect(status().isCreated());
}

retryOnException(() -> webTestHelper.post("/templates.list",
"{\"source_type\": \"google\"}", "user-id")
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.length()", is(1)));
.andExpect(jsonPath("$.data.length()", is(10))), "could not list google templates");

webTestHelper.post("/templates.delete", "{\"id\": \"" + templateId + "\"}", "user-id").andExpect(status().isOk());
retryOnException(() -> webTestHelper.post("/templates.list",
"{\"source_type\": \"google\", \"name\": \"awesome-template-\"}", "user-id")
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.length()", is(10))), "could not list google templates by name");

retryOnException(() -> webTestHelper.post("/templates.list",
"{\"source_type\": \"google\", \"name\": \"awesome-template-1\"}", "user-id")
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.length()", is(1))), "could not list google templates by name");

retryOnException(() -> webTestHelper.post("/templates.list",
"{\"source_type\": \"twilio.sms\"}", "user-id")
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.length()", is(5))), "could not list twilio templates");

retryOnException(() -> webTestHelper.post("/templates.list",
"{\"source_type\": \"twilio.sms\", \"name\": \"awesome-template-\"}", "user-id")
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.length()", is(5))), "could not list twilio templates by name");

retryOnException(() -> webTestHelper.post("/templates.list",
"{\"source_type\": \"twilio.sms\", \"name\": \"awesome-template-1\"}", "user-id")
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.length()", is(1))), "could not list twilio templates by name");
}
}

0 comments on commit 6cd6602

Please sign in to comment.