Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add RecordingRule and RecordingRulesUpdate #573

Merged
merged 6 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
249 changes: 249 additions & 0 deletions src/main/java/com/twilio/type/RecordingRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
package com.twilio.type;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;
import com.google.common.base.MoreObjects;
import com.twilio.converter.Promoter;

import java.util.Objects;

@JsonIgnoreProperties(ignoreUnknown = true)
public class RecordingRule {

public enum Type {
INCLUDE("include"),
EXCLUDE("exclude");

private final String value;

Type(final String value) {
this.value = value;
}

@JsonCreator
public static Type forValue(final String value) {
return Promoter.enumFromString(value, Type.values());
}

@JsonValue
public String value() {
return this.value;
}

@Override
public String toString() {
return value;
}
}

public enum Kind {
AUDIO("audio"),
DATA("data"),
VIDEO("video");

private final String value;

Kind(final String value) {
this.value = value;
}

@JsonCreator
public static Kind forValue(final String value) {
return Promoter.enumFromString(value, Kind.values());
}

@JsonValue
public String value() {
return this.value;
}

@Override
public String toString() {
return value;
}
}


private static final RecordingRule recordAll = builder().withType(Type.INCLUDE).withAll().build();
private static final RecordingRule recordNone = builder().withType(Type.EXCLUDE).withAll().build();

@JsonProperty("type")
private final Type type;

@JsonProperty("all")
private final Boolean all;

@JsonProperty("publisher")
private final String publisher;

@JsonProperty("track")
private final String track;

@JsonProperty("kind")
private final Kind kind;

public RecordingRule(@JsonProperty("type") final Type type,
@JsonProperty("all") final Boolean all,
@JsonProperty("publisher") final String publisher,
@JsonProperty("track") final String track,
@JsonProperty("kind") final Kind kind) {
this.type = type;
this.all = all;
this.publisher = publisher;
this.track = track;
this.kind = kind;
}

public RecordingRule() {
this.type = null;
this.all = null;
this.publisher = null;
this.track = null;
this.kind = null;
}

public static BuilderStart builder() {
return new Builder();
}

public interface BuilderStart {
BuilderMiddle withType(final Type type);
}

public interface BuilderMiddle {
BuilderMiddleBuild withPublisher(final String publisher);
BuilderMiddleBuild withKind(final Kind kind);
BuilderMiddleBuild withTrack(final String track);
BuilderBuild withAll();
}

public interface BuilderMiddleBuild {
BuilderMiddleBuild withPublisher(final String publisher);
BuilderMiddleBuild withKind(final Kind kind);
BuilderMiddleBuild withTrack(final String track);
RecordingRule build();
}

public interface BuilderBuild {
RecordingRule build();
}

public static class Builder implements
BuilderStart,
BuilderMiddle,
BuilderMiddleBuild,
BuilderBuild {
private Type type;
private Boolean all;
private String publisher;
private Kind kind;
private String track;

private Builder() {
}

public BuilderMiddle withType(final Type type) {
this.type = type;
return this;
}

public BuilderBuild withAll() {
this.all = true;
return this;
}
public BuilderMiddleBuild withPublisher(final String publisher) {
this.publisher = publisher;
return this;
}
public BuilderMiddleBuild withKind(final Kind kind) {
this.kind = kind;
return this;
}
public BuilderMiddleBuild withTrack(final String track) {
this.track = track;
return this;
}

private boolean hasOneFilter() {
// at least one filter must be set
return this.kind != null
|| this.all != null
|| this.track != null
|| this.publisher != null;
}

private boolean hasType() {
// every rule must have a type
return this.type != null;
}

public RecordingRule build() {
if (!hasType()) {
throw new IllegalArgumentException("Recording Rule must have a type");
}
if (!hasOneFilter()) {
throw new IllegalArgumentException("Recording Rule must have at least one filter");
}

return new RecordingRule(this.type, this.all, this.publisher, this.track, this.kind);
}
}

public Type getType() {
return type;
}

public Boolean getAll() {
return all;
}

public String getPublisher() {
return publisher;
}

public String getTrack() {
return track;
}

public Kind getKind() {
return kind;
}

public static RecordingRule all() {
return recordAll;
}

public static RecordingRule none() {
return recordNone;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof RecordingRule)) return false;
RecordingRule that = (RecordingRule) o;
return getType() == that.getType() &&
Objects.equals(getAll(), that.getAll()) &&
Objects.equals(getPublisher(), that.getPublisher()) &&
Objects.equals(getTrack(), that.getTrack()) &&
getKind() == that.getKind();
}

@Override
public int hashCode() {
return Objects.hash(getType(), getAll(), getPublisher(), getTrack(), getKind());
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("type", this.type)
.add("all", this.all)
.add("publisher", this.publisher)
.add("track", this.track)
.add("kind", this.kind)
.toString();
}
}
43 changes: 43 additions & 0 deletions src/main/java/com/twilio/type/RecordingRulesUpdate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.twilio.type;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.MoreObjects;

import java.util.List;
import java.util.Objects;

@JsonIgnoreProperties(ignoreUnknown = true)
public class RecordingRulesUpdate {

@JsonProperty("rules")
private final List<RecordingRule> rules;

public RecordingRulesUpdate(@JsonProperty("rules") final List<RecordingRule> rules) {
this.rules = rules;
}

public List<RecordingRule> getRules() {
return rules;
}

@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final com.twilio.type.RecordingRulesUpdate that = (com.twilio.type.RecordingRulesUpdate) o;
return Objects.equals(getRules(), that.getRules());
}

@Override
public int hashCode() {
return Objects.hash(getRules());
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("rules", this.rules)
.toString();
}
}
82 changes: 82 additions & 0 deletions src/test/java/com/twilio/type/RecordingRuleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.twilio.type;

import com.google.common.collect.Lists;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.fail;

public class RecordingRuleTest extends TypeTest {
@Test
public void testBuilderOneFilter() {
try {
RecordingRule.builder().withType(RecordingRule.Type.INCLUDE).withPublisher(null).build();
} catch (IllegalArgumentException e) {
return;
}
fail("Rule builder should enforce a filter");
}

@Test
public void testBuilderMustHaveType() {
try {
RecordingRule.builder().withType(null).withPublisher("alice").build();
} catch (IllegalArgumentException e) {
return;
}
fail("Rule builder should enforce setting a type");
}

@Test
public void testAllExclusive() throws IOException {
String json = "{\n" +
" \"type\": \"include\",\n" +
" \"all\": true\n" +
"}";

RecordingRule r = fromJson(json, RecordingRule.class);
Assert.assertEquals(RecordingRule.Type.INCLUDE, r.getType());
Assert.assertEquals(true, r.getAll());

Assert.assertTrue(convertsToAndFromJson(r, RecordingRule.class));
}

@Test
public void testFilters() throws IOException {
String json = "{\n" +
" \"type\": \"exclude\",\n" +
" \"track\": \"screen\",\n" +
" \"kind\": \"video\",\n" +
" \"publisher\": \"alice\"\n" +
"}";

RecordingRule r = fromJson(json, RecordingRule.class);
Assert.assertEquals(RecordingRule.Type.EXCLUDE, r.getType());
Assert.assertEquals(RecordingRule.Kind.VIDEO, r.getKind());
Assert.assertEquals("alice", r.getPublisher());
Assert.assertNull(r.getAll());

Assert.assertTrue(convertsToAndFromJson(r, RecordingRule.class));
}

@Test
public void testUpdate() throws IOException {
final RecordingRule allAudio = RecordingRule.builder()
.withType(RecordingRule.Type.INCLUDE)
.withKind(RecordingRule.Kind.AUDIO)
.build();
final RecordingRule presenterVideo = RecordingRule.builder()
.withType(RecordingRule.Type.INCLUDE)
.withKind(RecordingRule.Kind.VIDEO)
.withPublisher("presenter")
.build();

final RecordingRulesUpdate update = new RecordingRulesUpdate(Lists.newArrayList(
allAudio, presenterVideo
));

Assert.assertTrue(convertsToAndFromJson(update, RecordingRulesUpdate.class));
}
}