Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from dlvenable/replicator
Replicator Documents and Connector
- Loading branch information
Showing
5 changed files
with
338 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
218 changes: 218 additions & 0 deletions
218
org.ektorp/src/main/java/org/ektorp/ReplicatorDocument.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
package org.ektorp; | ||
|
||
import org.codehaus.jackson.annotate.JsonAnySetter; | ||
import org.codehaus.jackson.annotate.JsonProperty; | ||
import org.codehaus.jackson.map.annotate.JsonSerialize; | ||
import org.ektorp.support.CouchDbDocument; | ||
|
||
import java.util.Calendar; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Represents Couch documents used by the Couch _replicator database. | ||
*/ | ||
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) | ||
public class ReplicatorDocument extends CouchDbDocument | ||
{ | ||
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) | ||
public static class UserContext | ||
{ | ||
private String name; | ||
private Collection<String> roles; | ||
|
||
private Map<String, Object> unknownFields; | ||
|
||
public String getName() | ||
{ | ||
return name; | ||
} | ||
|
||
public void setName(String name) | ||
{ | ||
this.name = name; | ||
} | ||
|
||
public Collection<String> getRoles() | ||
{ | ||
return roles; | ||
} | ||
|
||
public void setRoles(Collection<String> roles) | ||
{ | ||
this.roles = roles; | ||
} | ||
|
||
@JsonAnySetter | ||
public void setUnknown(String key, Object value) { | ||
unknownFields().put(key, value); | ||
} | ||
|
||
public Map<String, Object> getUnknownFields() { | ||
return unknownFields(); | ||
} | ||
|
||
private Map<String, Object> unknownFields() { | ||
if (unknownFields == null) { | ||
unknownFields = new HashMap<String, Object>(); | ||
} | ||
return unknownFields; | ||
} | ||
} | ||
|
||
private String source; | ||
private String target; | ||
private Boolean createTarget; | ||
private Boolean continuous; | ||
private Collection<String> documentIds; | ||
private String filter; | ||
private Object queryParameters; | ||
private UserContext userContext; | ||
|
||
private String replicationId; | ||
private String replicationState; | ||
private Calendar replicationStateTime; | ||
|
||
private Map<String, Object> unknownFields; | ||
|
||
public String getSource() | ||
{ | ||
return source; | ||
} | ||
|
||
public void setSource(String source) | ||
{ | ||
this.source = source; | ||
} | ||
|
||
public String getTarget() | ||
{ | ||
return target; | ||
} | ||
|
||
public void setTarget(String target) | ||
{ | ||
this.target = target; | ||
} | ||
|
||
@JsonProperty("create_target") | ||
public Boolean getCreateTarget() | ||
{ | ||
return createTarget; | ||
} | ||
|
||
@JsonProperty("create_target") | ||
public void setCreateTarget(Boolean createTarget) | ||
{ | ||
this.createTarget = createTarget; | ||
} | ||
|
||
public Boolean getContinuous() | ||
{ | ||
return continuous; | ||
} | ||
|
||
public void setContinuous(Boolean continuous) | ||
{ | ||
this.continuous = continuous; | ||
} | ||
|
||
@JsonProperty("doc_ids") | ||
public Collection<String> getDocumentIds() | ||
{ | ||
return documentIds; | ||
} | ||
|
||
@JsonProperty("doc_ids") | ||
public void setDocumentIds(Collection<String> documentIds) | ||
{ | ||
this.documentIds = documentIds; | ||
} | ||
|
||
public String getFilter() | ||
{ | ||
return filter; | ||
} | ||
|
||
public void setFilter(String filter) | ||
{ | ||
this.filter = filter; | ||
} | ||
|
||
@JsonProperty("query_params") | ||
public Object getQueryParameters() | ||
{ | ||
return queryParameters; | ||
} | ||
|
||
@JsonProperty("query_params") | ||
public void setQueryParameters(Object queryParameters) | ||
{ | ||
this.queryParameters = queryParameters; | ||
} | ||
|
||
@JsonProperty("user_ctx") | ||
public UserContext getUserContext() | ||
{ | ||
return userContext; | ||
} | ||
|
||
@JsonProperty("user_ctx") | ||
public void setUserContext(UserContext userContext) | ||
{ | ||
this.userContext = userContext; | ||
} | ||
|
||
@JsonProperty("_replication_id") | ||
public String getReplicationId() | ||
{ | ||
return replicationId; | ||
} | ||
|
||
@JsonProperty("_replication_id") | ||
public void setReplicationId(String replicationId) | ||
{ | ||
this.replicationId = replicationId; | ||
} | ||
|
||
@JsonProperty("_replication_state") | ||
public String getReplicationState() | ||
{ | ||
return replicationState; | ||
} | ||
|
||
@JsonProperty("_replication_state") | ||
public void setReplicationState(String replicationState) | ||
{ | ||
this.replicationState = replicationState; | ||
} | ||
|
||
@JsonProperty("_replication_state_time") | ||
public Calendar getReplicationStateTime() | ||
{ | ||
return replicationStateTime; | ||
} | ||
|
||
@JsonProperty("_replication_state_time") | ||
public void setReplicationStateTime(Calendar replicationStateTime) | ||
{ | ||
this.replicationStateTime = replicationStateTime; | ||
} | ||
|
||
@JsonAnySetter | ||
public void setUnknown(String key, Object value) { | ||
unknownFields().put(key, value); | ||
} | ||
|
||
public Map<String, Object> getUnknownFields() { | ||
return unknownFields(); | ||
} | ||
|
||
private Map<String, Object> unknownFields() { | ||
if (unknownFields == null) { | ||
unknownFields = new HashMap<String, Object>(); | ||
} | ||
return unknownFields; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
org.ektorp/src/test/java/org/ektorp/ReplicatorDocumentTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.ektorp; | ||
|
||
import org.codehaus.jackson.map.ObjectMapper; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
import static org.hamcrest.core.Is.is; | ||
import static org.hamcrest.core.IsNull.notNullValue; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class ReplicatorDocumentTest | ||
{ | ||
private ObjectMapper objectMapper; | ||
|
||
@Before | ||
public void setUp() | ||
{ | ||
objectMapper = new ObjectMapper(); | ||
} | ||
|
||
@Test | ||
public void getUnknownFields_should_return_an_empty_map_if_there_were_no_fields_which_were_not_mapped() throws IOException | ||
{ | ||
Map<String, Object> jsonMap = new HashMap<String, Object>(); | ||
jsonMap.put("_id", UUID.randomUUID().toString()); | ||
|
||
String jsonString = objectMapper.writeValueAsString(jsonMap); | ||
|
||
ReplicatorDocument document = objectMapper.readValue(jsonString, ReplicatorDocument.class); | ||
assertThat(document.getUnknownFields(), notNullValue()); | ||
assertThat(document.getUnknownFields().isEmpty(), is(true)); | ||
} | ||
|
||
@Test | ||
public void getUnknownFields_should_return_fields_which_were_not_mapped() throws IOException | ||
{ | ||
Map<String, Object> jsonMap = new HashMap<String, Object>(); | ||
String unknownField = UUID.randomUUID().toString(); | ||
jsonMap.put(unknownField, UUID.randomUUID().toString()); | ||
|
||
String jsonString = objectMapper.writeValueAsString(jsonMap); | ||
|
||
ReplicatorDocument document = objectMapper.readValue(jsonString, ReplicatorDocument.class); | ||
assertThat(document.getUnknownFields(), notNullValue()); | ||
assertThat(document.getUnknownFields().containsKey(unknownField), is(true)); | ||
assertThat(document.getUnknownFields().get(unknownField), is(jsonMap.get(unknownField))); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
org.ektorp/src/test/java/org/ektorp/ReplicatorDocument_UserContextTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.ektorp; | ||
|
||
import org.codehaus.jackson.map.ObjectMapper; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
import static org.hamcrest.core.Is.is; | ||
import static org.hamcrest.core.IsNull.notNullValue; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class ReplicatorDocument_UserContextTest | ||
{ | ||
private ObjectMapper objectMapper; | ||
|
||
@Before | ||
public void setUp() | ||
{ | ||
objectMapper = new ObjectMapper(); | ||
} | ||
|
||
@Test | ||
public void getUnknownFields_should_return_an_empty_map_if_there_were_no_fields_which_were_not_mapped() throws IOException | ||
{ | ||
Map<String, Object> jsonMap = new HashMap<String, Object>(); | ||
jsonMap.put("name", UUID.randomUUID().toString()); | ||
|
||
String jsonString = objectMapper.writeValueAsString(jsonMap); | ||
|
||
ReplicatorDocument.UserContext document = objectMapper.readValue(jsonString, ReplicatorDocument.UserContext.class); | ||
assertThat(document.getUnknownFields(), notNullValue()); | ||
assertThat(document.getUnknownFields().isEmpty(), is(true)); | ||
} | ||
|
||
@Test | ||
public void getUnknownFields_should_return_fields_which_were_not_mapped() throws IOException | ||
{ | ||
Map<String, Object> jsonMap = new HashMap<String, Object>(); | ||
String unknownField = UUID.randomUUID().toString(); | ||
jsonMap.put(unknownField, UUID.randomUUID().toString()); | ||
|
||
String jsonString = objectMapper.writeValueAsString(jsonMap); | ||
|
||
ReplicatorDocument.UserContext document = objectMapper.readValue(jsonString, ReplicatorDocument.UserContext.class); | ||
assertThat(document.getUnknownFields(), notNullValue()); | ||
assertThat(document.getUnknownFields().containsKey(unknownField), is(true)); | ||
assertThat(document.getUnknownFields().get(unknownField), is(jsonMap.get(unknownField))); | ||
} | ||
} |