Skip to content

Commit

Permalink
Merge pull request #54 from eyakauleva/49
Browse files Browse the repository at this point in the history
graphQL integration testing
  • Loading branch information
Aliaksei Bialiauski committed Jun 20, 2023
2 parents 04c906e + 83582da commit f8745b2
Show file tree
Hide file tree
Showing 18 changed files with 344 additions and 102 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<elasticsearch.version>5.1.0</elasticsearch.version>
<prometheus.version>1.11.0</prometheus.version>
<graphql.validation.version>20.0</graphql.validation.version>
<graphql.test>1.2.0</graphql.test>
</properties>

<dependencies>
Expand Down Expand Up @@ -167,6 +168,12 @@
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.graphql</groupId>
<artifactId>spring-graphql-test</artifactId>
<version>${graphql.test}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/graphql/mutation.graphqls
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type Mutation {
createUser(user: UserInput!, commandBy: String!): Es
deleteUser(id: ID!, commandBy: String!): Es
}
5 changes: 5 additions & 0 deletions src/main/resources/graphql/query.graphqls
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type Query {
getAllUsers: [User]
findUserById(userId: ID!): User
findByCriteria(criteria: CriteriaInput!, size: Int! @Min(value: 1), page: Int! @Min): [User]
}
83 changes: 0 additions & 83 deletions src/main/resources/graphql/schema.graphqls

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
directive @NotBlank(message : String = "graphql.validation.NotBlank.message")
on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION

directive @Min(value : Int = 0, message : String = "graphql.validation.Min.message")
on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
13 changes: 13 additions & 0 deletions src/main/resources/graphql/typedefinitions/enums.graphqls
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
enum Gender {
MALE
FEMALE
UNSET
}

enum EyesColor {
BLUE
GREEN
BROWN
GREY
UNSET
}
26 changes: 26 additions & 0 deletions src/main/resources/graphql/typedefinitions/inputs.graphqls
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
input UserInput {
firstName: String! @NotBlank
lastName: String! @NotBlank
email: String! @NotBlank
phone: String! @NotBlank
age: Int
gender: Gender
height: Float
weight: Float
eyesColor: EyesColor
startStudyYear: Int
endStudyYear: Int
}

input CriteriaInput {
name: String
phone: String
age: Int
heightFrom: Float
heightTo: Float
weightFrom: Float
weightTo: Float
genders: [Gender]
eyesColors: [EyesColor]
studyYear: Int
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
scalar LocalDateTime
24 changes: 24 additions & 0 deletions src/main/resources/graphql/typedefinitions/types.graphqls
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
type User {
id: ID
firstName: String!
lastName: String!
email: String!
phone: String!
age: Int
gender: Gender
height: Float
weight: Float
eyesColor: EyesColor
startStudyYear: Int
endStudyYear: Int
}

type Es {
id: ID
type: String
time: LocalDateTime
createdBy: String
entityId: String
payload: String
status: String
}
29 changes: 24 additions & 5 deletions src/test/java/com/solvd/micro9/users/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
import com.solvd.micro9.users.domain.criteria.UserCriteria;
import com.solvd.micro9.users.domain.elasticsearch.ESearchUser;
import com.solvd.micro9.users.domain.elasticsearch.StudyYears;
import com.solvd.micro9.users.domain.es.EsStatus;
import com.solvd.micro9.users.domain.es.EsType;
import com.solvd.micro9.users.domain.es.EsUser;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand All @@ -16,6 +20,11 @@

public final class TestUtils {

private static final DateTimeFormatter DATE_FORMATTER =
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
private static final LocalDateTime LOCAL_DATE_TIME =
LocalDateTime.parse("2023-06-01T12:30:25", DATE_FORMATTER);

private TestUtils() {
}

Expand All @@ -25,22 +34,32 @@ public static User getUser() {
50.2f, EyesColor.BLUE, 2010, 2015, false);
}

public static EsUser getEsUserCreated() {
return new EsUser(1L, EsType.USER_CREATED, LocalDateTime.now(),
"Liza", "14121", "", EsStatus.SUBMITTED);
}

public static EsUser getEsUserDeleted() {
return new EsUser(1L, EsType.USER_DELETED, LocalDateTime.now(),
"Liza", "14121", "", EsStatus.PENDING);
}

public static ESearchUser getElstcUser() {
return new ESearchUser("1111", "Liza Ya", "+12345",
20, Gender.FEMALE, 170.5f, 50.2f, EyesColor.BLUE,
new StudyYears(2015, 2018), LocalDateTime.now());
new StudyYears(2015, 2018), LOCAL_DATE_TIME);
}

public static List<ESearchUser> getElstcUsers() {
ESearchUser user1 = new ESearchUser("111", "Liza Ya", "+12345",
20, Gender.FEMALE, 170.5f, 57.2f, EyesColor.BLUE,
new StudyYears(2019, 2024), LocalDateTime.now());
new StudyYears(2019, 2024), LOCAL_DATE_TIME);
ESearchUser user2 = new ESearchUser("234", "Ivan Ivanov", "+8928912",
36, Gender.MALE, 192.4f, 96.2f, EyesColor.GREEN,
new StudyYears(2010, 2014), LocalDateTime.now());
new StudyYears(2010, 2014), LOCAL_DATE_TIME);
ESearchUser user3 = new ESearchUser("43436", "Petr Petrov", "+35232",
27, Gender.UNSET, 179.0f, 85.1f, EyesColor.UNSET,
new StudyYears(2012, 2016), LocalDateTime.now());
new StudyYears(2012, 2016), LOCAL_DATE_TIME);
return List.of(user1, user2, user3);
}

Expand Down Expand Up @@ -83,7 +102,7 @@ public static boolean doesESearchUserSatisfyCriteria(
|| criteria.getEyesColors().contains(user.getEyesColor()));
predicates.add((user, criteria) -> Objects.isNull(criteria.getStudyYear())
|| (user.getStudyYears().getGte() <= criteria.getStudyYear()
&& user.getStudyYears().getLte() >= criteria.getStudyYear()));
&& user.getStudyYears().getLte() >= criteria.getStudyYear()));

BiPredicate<ESearchUser, UserCriteria> predicate
= predicates.stream().reduce(BiPredicate::and).get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.solvd.micro9.users.domain.aggregate.User;
import org.mockito.Mockito;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.graphql.GraphQlAutoConfiguration;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
Expand All @@ -11,7 +12,7 @@
import reactor.core.publisher.Mono;

@TestConfiguration
@EnableAutoConfiguration
@EnableAutoConfiguration(exclude = {GraphQlAutoConfiguration.class})
@ComponentScan("com.solvd.micro9.users.persistence.elastic")
public class ElasticsearchITConfig {

Expand Down
Loading

0 comments on commit f8745b2

Please sign in to comment.