Skip to content

Commit

Permalink
Adding DateRangeContainerResource for filter by date usecase (#947)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushagarwalfb authored Jan 11, 2021
1 parent d5c4479 commit 584415f
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
@JsonSubTypes.Type(TaskContainerResource.class),
@JsonSubTypes.Type(PlaylistContainerResource.class),
@JsonSubTypes.Type(SocialActivityContainerResource.class),
@JsonSubTypes.Type(IdOnlyContainerResource.class)
@JsonSubTypes.Type(IdOnlyContainerResource.class),
@JsonSubTypes.Type(DateRangeContainerResource.class)
})
public abstract class ContainerResource extends DataModel {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2019 The Data Transfer Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.datatransferproject.types.common.models;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;

import java.util.Objects;

/** A date range container containing start and end date for filtering. */
@JsonTypeName("org.dataportability:DateRangeContainerResource")
public class DateRangeContainerResource extends ContainerResource {
private final Integer startDate;
private final Integer endDate;

@JsonCreator
public DateRangeContainerResource(
@JsonProperty("startDate") Integer startDate,
@JsonProperty("endDate") Integer endDate) {
this.startDate = startDate;
this.endDate = endDate;
}

public Integer getStartDate() {
return startDate;
}
public Integer getEndDate() { return endDate; }

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DateRangeContainerResource that = (DateRangeContainerResource) o;
return Objects.equals(startDate, that.startDate)
&& Objects.equals(endDate, that.endDate);
}

@Override
public int hashCode() {
return Objects.hash(startDate, endDate);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2019 The Data Transfer Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.datatransferproject.types.common.models;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.truth.Truth;
import org.junit.Test;

public class DateRangeContainerResourceTest {
@Test
public void verifySerializeDeserialize() throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerSubtypes(DateRangeContainerResource.class);

int startDate = 1607784466;
int endDate = 1608648466;

ContainerResource data = new DateRangeContainerResource(startDate, endDate);

String serialized = objectMapper.writeValueAsString(data);

ContainerResource deserializedModel =
objectMapper.readValue(serialized, ContainerResource.class);

Truth.assertThat(deserializedModel).isNotNull();
Truth.assertThat(deserializedModel).isInstanceOf(DateRangeContainerResource.class);
DateRangeContainerResource deserialized = (DateRangeContainerResource) deserializedModel;
Truth.assertThat(deserialized.getStartDate()).isEqualTo(startDate);
Truth.assertThat(deserialized.getEndDate()).isEqualTo(endDate);
Truth.assertThat(deserialized).isEqualTo(data);
}
}

0 comments on commit 584415f

Please sign in to comment.