Skip to content

Commit

Permalink
End-to-End System Tests Have Been Integrated (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
agitrubard authored Aug 18, 2023
1 parent 60abab3 commit 3c15af3
Show file tree
Hide file tree
Showing 17 changed files with 1,156 additions and 27 deletions.
35 changes: 33 additions & 2 deletions src/main/resources/db/changelog/changes/2-ays-dml.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@
<column name="CREATED_USER" value="AYS"/>
<column name="CREATED_AT" valueDate="now()"/>
</insert>

<!-- ===================== -->
<!-- DML of AYS_ADMIN_USER -->
<!-- ===================== -->
Expand Down Expand Up @@ -147,7 +146,7 @@
<column name="USERNAME" value="232180"/>
<column name="PASSWORD" value="$2a$10$sx4rlwEudbcqHRU28dAg8ORpE4Dr.pCbX5AbxnDqazFcvGAyrvOEq"/>
<column name="FIRST_NAME" value="Afet Yönetim"/>
<column name="LAST_NAME" value="Sistemi"/>
<column name="LAST_NAME" value="Sistemi 1"/>
<column name="COUNTRY_CODE" value="90"/>
<column name="SUPPORT_STATUS" value="IDLE"/>
<column name="LINE_NUMBER" value="1234567890"/>
Expand All @@ -156,6 +155,38 @@
<column name="CREATED_USER" value="AYS"/>
<column name="CREATED_AT" valueDate="now()"/>
</insert>

<insert tableName="AYS_USER">
<column name="ID" value="0c8034d2-f91b-4ef4-bf6c-bdbeda945764"/>
<column name="INSTITUTION_ID" value="77ece256-bf0e-4bbe-801d-173083f8bdcf"/>
<column name="USERNAME" value="930047"/>
<column name="PASSWORD" value="$2a$10$sx4rlwEudbcqHRU28dAg8ORpE4Dr.pCbX5AbxnDqazFcvGAyrvOEq"/>
<column name="FIRST_NAME" value="Afet Yönetim"/>
<column name="LAST_NAME" value="Sistemi 2"/>
<column name="COUNTRY_CODE" value="90"/>
<column name="SUPPORT_STATUS" value="IDLE"/>
<column name="LINE_NUMBER" value="1234567891"/>
<column name="ROLE" value="VOLUNTEER"/>
<column name="STATUS" value="ACTIVE"/>
<column name="CREATED_USER" value="AYS"/>
<column name="CREATED_AT" valueDate="now()"/>
</insert>

<insert tableName="AYS_USER">
<column name="ID" value="edb36891-b898-4c12-bcec-d9aaa5d45190"/>
<column name="INSTITUTION_ID" value="91df7ae9-d5b9-44ae-b54f-d5d55359c4a4"/>
<column name="USERNAME" value="648193"/>
<column name="PASSWORD" value="$2a$10$sx4rlwEudbcqHRU28dAg8ORpE4Dr.pCbX5AbxnDqazFcvGAyrvOEq"/>
<column name="FIRST_NAME" value="Afet Yönetim"/>
<column name="LAST_NAME" value="Sistemi 3"/>
<column name="COUNTRY_CODE" value="90"/>
<column name="SUPPORT_STATUS" value="IDLE"/>
<column name="LINE_NUMBER" value="1234567892"/>
<column name="ROLE" value="VOLUNTEER"/>
<column name="STATUS" value="ACTIVE"/>
<column name="CREATED_USER" value="AYS"/>
<column name="CREATED_AT" valueDate="now()"/>
</insert>
<!-- =============== -->
<!-- DML of AYS_USER -->
<!-- =============== -->
Expand Down
139 changes: 139 additions & 0 deletions src/test/java/com/ays/AbstractSystemTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package com.ays;

import com.ays.admin_user.model.entity.AdminUserEntityBuilder;
import com.ays.auth.config.AysTokenConfigurationParameter;
import com.ays.auth.model.AysToken;
import com.ays.auth.model.enums.AysTokenClaims;
import com.ays.common.util.AysRandomUtil;
import com.ays.parameter.model.AysParameter;
import com.ays.parameter.model.AysParameterBuilder;
import com.ays.parameter.service.AysParameterService;
import com.ays.user.model.entity.UserEntityBuilder;
import com.ays.util.AysTestData;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.commons.lang3.time.DateUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.test.web.servlet.MockMvc;

import java.util.Date;
import java.util.Map;
import java.util.Set;

@SpringBootTest
@ExtendWith(MockitoExtension.class)
@AutoConfigureMockMvc
public abstract class AbstractSystemTest extends AbstractTestContainerConfiguration {

@Autowired
protected MockMvc mockMvc;

protected AysToken adminUserTokenOne;
protected AysToken adminUserTokenTwo;
protected AysToken userTokenOne;
protected AysToken userTokenTwo;
protected AysToken userTokenThree;


@Mock
private AysTokenConfigurationParameter tokenConfiguration;
@Mock
private AysParameterService parameterService;

@BeforeEach
public void initializeAuth() {
Set<AysParameter> parameters = AysParameterBuilder.getParameters();
Mockito.when(parameterService.getParameters(Mockito.anyString()))
.thenReturn(parameters);
this.tokenConfiguration = new AysTokenConfigurationParameter(parameterService);

final Map<String, Object> claimsOfAdminUserOne = new AdminUserEntityBuilder()
.withId(AysTestData.AdminUser.VALID_ID_ONE)
.withUsername(AysTestData.AdminUser.VALID_USERNAME_ONE)
.withEmail(AysTestData.AdminUser.VALID_EMAIL_ONE)
.withInstitutionId(AysTestData.Institution.VALID_ID_ONE)
.build()
.getClaims();
this.adminUserTokenOne = this.generate(claimsOfAdminUserOne);

final Map<String, Object> claimsOfAdminUserTwo = new AdminUserEntityBuilder()
.withId(AysTestData.AdminUser.VALID_ID_TWO)
.withUsername(AysTestData.AdminUser.VALID_USERNAME_TWO)
.withEmail(AysTestData.AdminUser.VALID_EMAIL_TWO)
.withInstitutionId(AysTestData.Institution.VALID_ID_TWO)
.build()
.getClaims();
this.adminUserTokenTwo = this.generate(claimsOfAdminUserTwo);


final Map<String, Object> claimsOfUserOne = new UserEntityBuilder()
.withId(AysTestData.User.VALID_ID_ONE)
.withUsername(AysTestData.User.VALID_USERNAME_ONE)
.withInstitutionId(AysTestData.Institution.VALID_ID_ONE)
.build()
.getClaims();
this.userTokenOne = this.generate(claimsOfUserOne);

final Map<String, Object> claimsOfUserTwo = new UserEntityBuilder()
.withId(AysTestData.User.VALID_ID_TWO)
.withUsername(AysTestData.User.VALID_USERNAME_TWO)
.withInstitutionId(AysTestData.Institution.VALID_ID_ONE)
.build()
.getClaims();
this.userTokenTwo = this.generate(claimsOfUserTwo);

final Map<String, Object> claimsOfUserThree = new UserEntityBuilder()
.withId(AysTestData.User.VALID_ID_THREE)
.withUsername(AysTestData.User.VALID_USERNAME_THREE)
.withInstitutionId(AysTestData.Institution.VALID_ID_TWO)
.build()
.getClaims();
this.userTokenThree = this.generate(claimsOfUserThree);
}

private AysToken generate(Map<String, Object> claims) {
final long currentTimeMillis = System.currentTimeMillis();

final Date tokenIssuedAt = new Date(currentTimeMillis);

final Date accessTokenExpiresAt = DateUtils.addMinutes(new Date(currentTimeMillis), tokenConfiguration.getAccessTokenExpireMinute());
final String accessToken = Jwts.builder()
.setId(AysRandomUtil.generateUUID())
.setIssuer(tokenConfiguration.getIssuer())
.setIssuedAt(tokenIssuedAt)
.setExpiration(accessTokenExpiresAt)
.signWith(tokenConfiguration.getPrivateKey(), SignatureAlgorithm.RS512)
.setHeaderParam(AysTokenClaims.TYPE.getValue(), OAuth2AccessToken.TokenType.BEARER.getValue())
.addClaims(claims)
.compact();

final Date refreshTokenExpiresAt = DateUtils.addDays(new Date(currentTimeMillis), tokenConfiguration.getRefreshTokenExpireDay());
final JwtBuilder refreshTokenBuilder = Jwts.builder();
final String refreshToken = refreshTokenBuilder
.setId(AysRandomUtil.generateUUID())
.setIssuer(tokenConfiguration.getIssuer())
.setIssuedAt(tokenIssuedAt)
.setExpiration(refreshTokenExpiresAt)
.signWith(tokenConfiguration.getPrivateKey(), SignatureAlgorithm.RS512)
.setHeaderParam(AysTokenClaims.TYPE.getValue(), OAuth2AccessToken.TokenType.BEARER.getValue())
.claim(AysTokenClaims.USER_ID.getValue(), claims.get(AysTokenClaims.USER_ID.getValue()))
.claim(AysTokenClaims.USERNAME.getValue(), claims.get(AysTokenClaims.USERNAME.getValue()))
.compact();

return AysToken.builder()
.accessToken(accessToken)
.accessTokenExpiresAt(accessTokenExpiresAt.toInstant().getEpochSecond())
.refreshToken(refreshToken)
.build();
}

}
9 changes: 7 additions & 2 deletions src/test/java/com/ays/AbstractTestContainerConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package com.ays;

import org.junit.jupiter.api.BeforeAll;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

@Testcontainers
abstract class AbstractTestContainerConfiguration {

@Container
static MySQLContainer<?> MYSQL_CONTAINER = new MySQLContainer<>("mysql:8.0.33");

@BeforeAll
static void beforeAll() {
MYSQL_CONTAINER.withReuse(true);
MYSQL_CONTAINER.start();
}

@DynamicPropertySource
private static void overrideProps(DynamicPropertyRegistry dynamicPropertyRegistry) {
dynamicPropertyRegistry.add("ays.db.username", MYSQL_CONTAINER::getUsername);
Expand Down
Loading

0 comments on commit 3c15af3

Please sign in to comment.