Skip to content

Commit

Permalink
add CommunityMomentCallout_Claim GQL call
Browse files Browse the repository at this point in the history
  • Loading branch information
Rakambda committed Aug 29, 2022
1 parent ad08f89 commit 4b82adc
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import fr.raksrinana.channelpointsminer.miner.api.gql.data.chatroombanstatus.ChatRoomBanStatusOperation;
import fr.raksrinana.channelpointsminer.miner.api.gql.data.claimcommunitypoints.ClaimCommunityPointsData;
import fr.raksrinana.channelpointsminer.miner.api.gql.data.claimcommunitypoints.ClaimCommunityPointsOperation;
import fr.raksrinana.channelpointsminer.miner.api.gql.data.communitymomentcalloutclaim.CommunityMomentCalloutClaimData;
import fr.raksrinana.channelpointsminer.miner.api.gql.data.communitymomentcalloutclaim.CommunityMomentCalloutClaimOperation;
import fr.raksrinana.channelpointsminer.miner.api.gql.data.dropshighlightserviceavailabledrops.DropsHighlightServiceAvailableDropsData;
import fr.raksrinana.channelpointsminer.miner.api.gql.data.dropshighlightserviceavailabledrops.DropsHighlightServiceAvailableDropsOperation;
import fr.raksrinana.channelpointsminer.miner.api.gql.data.dropspageclaimdroprewards.DropsPageClaimDropRewardsData;
Expand Down Expand Up @@ -115,6 +117,11 @@ public Optional<GQLResponse<ClaimCommunityPointsData>> claimCommunityPoints(@Not
return postRequest(new ClaimCommunityPointsOperation(channelId, claimId));
}

@NotNull
public Optional<GQLResponse<CommunityMomentCalloutClaimData>> claimCommunityMoment(@NotNull String momentId){
return postRequest(new CommunityMomentCalloutClaimOperation(momentId));
}

@NotNull
public Optional<GQLResponse<JoinRaidData>> joinRaid(@NotNull String raidId){
return postRequest(new JoinRaidOperation(raidId));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package fr.raksrinana.channelpointsminer.miner.api.gql.data.communitymomentcalloutclaim;

import com.fasterxml.jackson.annotation.JsonProperty;
import fr.raksrinana.channelpointsminer.miner.api.gql.data.types.ClaimCommunityMomentPayload;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.jetbrains.annotations.NotNull;

@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@EqualsAndHashCode
@ToString
public class CommunityMomentCalloutClaimData{
@JsonProperty("claimCommunityMoment")
@NotNull
private ClaimCommunityMomentPayload moment;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package fr.raksrinana.channelpointsminer.miner.api.gql.data.communitymomentcalloutclaim;

import fr.raksrinana.channelpointsminer.miner.api.gql.data.GQLResponse;
import fr.raksrinana.channelpointsminer.miner.api.gql.data.IGQLOperation;
import fr.raksrinana.channelpointsminer.miner.api.gql.data.PersistedQueryExtension;
import kong.unirest.core.GenericType;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import org.jetbrains.annotations.NotNull;

@Getter
@EqualsAndHashCode(callSuper = true)
@ToString
public class CommunityMomentCalloutClaimOperation extends IGQLOperation<CommunityMomentCalloutClaimData>{
public CommunityMomentCalloutClaimOperation(@NotNull String momentId){
super("CommunityMomentCallout_Claim");
addPersistedQueryExtension(new PersistedQueryExtension(1, "e2d67415aead910f7f9ceb45a77b750a1e1d9622c936d832328a0689e054db62"));
addVariable("input", InputData.builder().momentId(momentId).build());
}

@Override
@NotNull
public GenericType<GQLResponse<CommunityMomentCalloutClaimData>> getResponseType(){
return new GenericType<>(){};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package fr.raksrinana.channelpointsminer.miner.api.gql.data.communitymomentcalloutclaim;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@EqualsAndHashCode
@ToString
public class InputData{
@JsonProperty("momentID")
private String momentId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package fr.raksrinana.channelpointsminer.miner.api.gql.data.types;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import fr.raksrinana.channelpointsminer.miner.util.json.UnknownDeserializer;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.jetbrains.annotations.Nullable;

@JsonTypeName("ClaimCommunityMomentPayload")
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@EqualsAndHashCode(callSuper = true)
@ToString
public class ClaimCommunityMomentPayload extends GQLType{
@JsonProperty("moment")
@Nullable
private CommunityMoment moment;
@JsonProperty("error")
@JsonDeserialize(using = UnknownDeserializer.class)
@Nullable
private Object error;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package fr.raksrinana.channelpointsminer.miner.api.gql.data.types;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.jetbrains.annotations.NotNull;

@JsonTypeName("CommunityMoment")
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@EqualsAndHashCode(callSuper = true)
@ToString
public class CommunityMoment extends GQLType{
@JsonProperty("id")
@NotNull
private String id;
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
@JsonSubTypes.Type(value = FollowEdge.class, name = "FollowEdge"),
@JsonSubTypes.Type(value = FollowerEdge.class, name = "FollowerEdge"),
@JsonSubTypes.Type(value = ChatRoomBanStatus.class, name = "ChatRoomBanStatus"),
@JsonSubTypes.Type(value = ClaimCommunityMomentPayload.class, name = "ClaimCommunityMomentPayload"),
@JsonSubTypes.Type(value = CommunityMoment.class, name = "CommunityMoment"),
})
@EqualsAndHashCode
public abstract class GQLType{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package fr.raksrinana.channelpointsminer.miner.api.gql;

import fr.raksrinana.channelpointsminer.miner.api.gql.data.GQLResponse;
import fr.raksrinana.channelpointsminer.miner.api.gql.data.communitymomentcalloutclaim.CommunityMomentCalloutClaimData;
import fr.raksrinana.channelpointsminer.miner.api.gql.data.types.ClaimCommunityMomentPayload;
import fr.raksrinana.channelpointsminer.miner.api.gql.data.types.CommunityMoment;
import fr.raksrinana.channelpointsminer.miner.api.passport.TwitchLogin;
import fr.raksrinana.channelpointsminer.miner.tests.TestUtils;
import fr.raksrinana.channelpointsminer.miner.tests.UnirestMockExtension;
import kong.unirest.core.MockClient;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import java.util.Map;
import static kong.unirest.core.HttpMethod.POST;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
@ExtendWith(UnirestMockExtension.class)
class GQLApiClaimCommunityMomentTest{
private static final String ACCESS_TOKEN = "access-token";
private static final String MOMENT_ID = "moment-id";
private static final String VALID_QUERY = "{\"extensions\":{\"persistedQuery\":{\"sha256Hash\":\"e2d67415aead910f7f9ceb45a77b750a1e1d9622c936d832328a0689e054db62\",\"version\":1}},\"operationName\":\"CommunityMomentCallout_Claim\",\"variables\":{\"input\":{\"momentID\":\"%s\"}}}";

@InjectMocks
private GQLApi tested;

@Mock
private TwitchLogin twitchLogin;

@BeforeEach
void setUp(){
when(twitchLogin.getAccessToken()).thenReturn(ACCESS_TOKEN);
}

@Test
void nominalClaimed(MockClient unirest){
var expected = GQLResponse.<CommunityMomentCalloutClaimData> builder()
.extensions(Map.of(
"durationMilliseconds", 9,
"operationName", "CommunityMomentCallout_Claim",
"requestID", "request-id"
))
.data(CommunityMomentCalloutClaimData.builder()
.moment(ClaimCommunityMomentPayload.builder()
.moment(CommunityMoment.builder()
.id(MOMENT_ID)
.build())
.build())
.build())
.build();

unirest.expect(POST, "https://gql.twitch.tv/gql")
.header("Authorization", "OAuth " + ACCESS_TOKEN)
.body(VALID_QUERY.formatted(MOMENT_ID))
.thenReturn(TestUtils.getAllResourceContent("api/gql/communityMomentCalloutClaim_success.json"))
.withStatus(200);

assertThat(tested.claimCommunityMoment(MOMENT_ID)).isPresent().get().isEqualTo(expected);

unirest.verifyAll();
}

@Test
void invalidCredentials(MockClient unirest){
unirest.expect(POST, "https://gql.twitch.tv/gql")
.header("Authorization", "OAuth " + ACCESS_TOKEN)
.body(VALID_QUERY.formatted(MOMENT_ID))
.thenReturn(TestUtils.getAllResourceContent("api/gql/invalidAuth.json"))
.withStatus(401);

assertThrows(RuntimeException.class, () -> tested.claimCommunityMoment(MOMENT_ID));

unirest.verifyAll();
}

@Test
void invalidRequest(MockClient unirest){
unirest.expect(POST, "https://gql.twitch.tv/gql")
.header("Authorization", "OAuth " + ACCESS_TOKEN)
.body(VALID_QUERY.formatted(MOMENT_ID))
.thenReturn(TestUtils.getAllResourceContent("api/gql/invalidRequest.json"))
.withStatus(200);

assertThat(tested.claimCommunityMoment(MOMENT_ID)).isEmpty();

unirest.verifyAll();
}

@Test
void invalidResponse(MockClient unirest){
unirest.expect(POST, "https://gql.twitch.tv/gql")
.header("Authorization", "OAuth " + ACCESS_TOKEN)
.body(VALID_QUERY.formatted(MOMENT_ID))
.thenReturn()
.withStatus(500);

assertThat(tested.claimCommunityMoment(MOMENT_ID)).isEmpty();

unirest.verifyAll();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"data" : {
"claimCommunityMoment" : {
"moment" : {
"id" : "moment-id",
"__typename" : "CommunityMoment"
},
"error" : null,
"__typename" : "ClaimCommunityMomentPayload"
}
},
"extensions" : {
"durationMilliseconds" : 9,
"operationName" : "CommunityMomentCallout_Claim",
"requestID" : "request-id"
}
}

0 comments on commit 4b82adc

Please sign in to comment.