Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into feat/#483
Browse files Browse the repository at this point in the history
  • Loading branch information
xxeol2 committed Oct 5, 2023
2 parents 6995f68 + 8c6e756 commit 639e228
Show file tree
Hide file tree
Showing 13 changed files with 325 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ class SelectSchoolActivity : AppCompatActivity() {
}

private fun initObserve() {
repeatOnStarted {
repeatOnStarted(this) {
vm.uiState.collect { uiState ->
handleUiState(uiState)
}
}
repeatOnStarted {
repeatOnStarted(this) {
vm.event.collect { event ->
handleEvent(event)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class SelectSchoolViewModel @Inject constructor(
viewModelScope.launch {
schoolRepository.loadSchools()
.onSuccess { schools ->
if (uiState.value is SelectSchoolUiState.Success) {
val successState = (uiState.value as SelectSchoolUiState.Success)
_uiState.value = successState.copy(schools = schools)
val state = uiState.value
if (state is SelectSchoolUiState.Success) {
_uiState.value = state.copy(schools = schools)
} else {
_uiState.value = SelectSchoolUiState.Success(schools)
}
Expand All @@ -46,20 +46,18 @@ class SelectSchoolViewModel @Inject constructor(
}

fun selectSchool(schoolId: Long) {
if (uiState.value is SelectSchoolUiState.Success) {
_uiState.value =
(uiState.value as SelectSchoolUiState.Success).copy(selectedSchoolId = schoolId)
val state = uiState.value
if (state is SelectSchoolUiState.Success) {
_uiState.value = state.copy(selectedSchoolId = schoolId)
}
}

fun showStudentVerification() {
viewModelScope.launch {
if (uiState.value is SelectSchoolUiState.Success) {
val success = uiState.value as SelectSchoolUiState.Success
success.selectedSchoolId?.let { schoolId ->
_event.emit(
SelectSchoolEvent.ShowStudentVerification(schoolId)
)
val state = uiState.value
if (state is SelectSchoolUiState.Success) {
state.selectedSchoolId?.let { schoolId ->
_event.emit(SelectSchoolEvent.ShowStudentVerification(schoolId))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ class StudentVerificationActivity : AppCompatActivity() {
}

private fun initObserve() {
repeatOnStarted {
repeatOnStarted(this) {
vm.uiState.collect { uiState ->
handleUiState(uiState)
}
}
repeatOnStarted {
repeatOnStarted(this) {
vm.event.collect { event ->
handleEvent(event)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import kotlinx.coroutines.launch

fun LifecycleOwner.repeatOnStarted(action: suspend () -> Unit) {
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
fun repeatOnStarted(lifecycleOwner: LifecycleOwner, action: suspend () -> Unit) {
lifecycleOwner.lifecycleScope.launch {
lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
action()
}
}
Expand Down
6 changes: 6 additions & 0 deletions backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ dependencies {
// Mockito
testImplementation 'org.mockito:mockito-inline'

// Cucumber
testImplementation 'io.cucumber:cucumber-java:7.13.0'
testImplementation 'io.cucumber:cucumber-spring:7.13.0'
testImplementation 'io.cucumber:cucumber-junit-platform-engine:7.13.0'
testImplementation 'org.junit.platform:junit-platform-suite:1.8.2'

// Flyway
implementation 'org.flywaydb:flyway-core'
implementation 'org.flywaydb:flyway-mysql'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@ public AuthService(MemberRepository memberRepository) {
}

public LoginMemberDto login(UserInfo userInfo) {
LoginMemberDto loginMemberDto = handleLoginRequest(userInfo);
return loginMemberDto;
}

public LoginMemberDto handleLoginRequest(UserInfo userInfo) {
Optional<Member> originMember = memberRepository.findBySocialIdAndSocialType(userInfo.socialId(), userInfo.socialType());
Optional<Member> originMember =
memberRepository.findBySocialIdAndSocialType(userInfo.socialId(), userInfo.socialType());
if (originMember.isPresent()) {
Member member = originMember.get();
return LoginMemberDto.isExists(member);
Expand Down
39 changes: 39 additions & 0 deletions backend/src/test/java/com/festago/acceptance/CucumberClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.festago.acceptance;

import io.cucumber.spring.ScenarioScope;
import io.restassured.response.Response;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Component;

@Component
@ScenarioScope
public class CucumberClient {

private Map<String, Object> dataStorage = new HashMap<>();
private Response response;
private String token;

public CucumberClient() {
}

public void addData(String key, Object value) {
dataStorage.put(key, value);
}

public void addAuthToken(String token) {
this.token = token;
}

public Object getData(String key) {
return dataStorage.get(key);
}

public Response getResponse() {
return response;
}

public String getToken() {
return token;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.festago.acceptance;

import io.cucumber.java.Before;
import io.cucumber.spring.CucumberContextConfiguration;
import io.restassured.RestAssured;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.server.LocalServerPort;

@CucumberContextConfiguration
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
public class CucumberSpringConfiguration {

@LocalServerPort
private int port;

@Autowired
private DataInitializer dataInitializer;

@Before
public void before() {
dataInitializer.execute();
RestAssured.port = port;
}
}
83 changes: 83 additions & 0 deletions backend/src/test/java/com/festago/acceptance/DataInitializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.festago.acceptance;

import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.sql.DataSource;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@Profile("test")
public class DataInitializer implements InitializingBean {

@Autowired
private JdbcTemplate jdbcTemplate;

@Autowired
private DataSource dataSource;

private List<String> tableNames;

private Set<String> metaTableNames = Set.of(
"sys_config",
"flyway_schema_history",
"CONSTANTS",
"ENUM_VALUES",
"INDEXES",
"INDEX_COLUMNS",
"INFORMATION_SCHEMA_CATALOG_NAME",
"IN_DOUBT",
"LOCKS",
"QUERY_STATISTICS",
"RIGHTS",
"ROLES",
"SESSIONS",
"SESSION_STATE",
"SETTINGS",
"SYNONYMS",
"USERS"
);

@Override
public void afterPropertiesSet() {
tableNames = new ArrayList<>();
try {
DatabaseMetaData metaData = dataSource.getConnection().getMetaData();
ResultSet tables = metaData.getTables(null, null, null, new String[]{"TABLE"});
while (tables.next()) {
String tableName = tables.getString("TABLE_NAME");
if (metaTableNames.contains(tableName)) {
continue;
}
tableNames.add(tableName);
}
} catch (Exception e) {
throw new RuntimeException();
}
}

@Transactional
public void execute() {
truncateAllTables();
}

private void truncateAllTables() {
jdbcTemplate.execute("SET foreign_key_checks = 0;");
tableNames.forEach(
tableName -> executeQueryWithTable(tableName)
);
jdbcTemplate.execute("SET foreign_key_checks = 1;");
}

private void executeQueryWithTable(String tableName) {
jdbcTemplate.execute("TRUNCATE TABLE " + tableName);
}
}

16 changes: 16 additions & 0 deletions backend/src/test/java/com/festago/acceptance/EntryPoint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.festago.acceptance;

import static io.cucumber.junit.platform.engine.Constants.GLUE_PROPERTY_NAME;

import org.junit.platform.suite.api.ConfigurationParameter;
import org.junit.platform.suite.api.IncludeEngines;
import org.junit.platform.suite.api.SelectClasspathResource;
import org.junit.platform.suite.api.Suite;

@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("features")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.festago.acceptance")
public class EntryPoint {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.festago.acceptance.steps;

import static org.assertj.core.api.Assertions.assertThat;

import com.festago.acceptance.CucumberClient;
import com.festago.auth.dto.AdminLoginRequest;
import com.festago.auth.dto.RootAdminInitializeRequest;
import com.festago.festival.dto.FestivalCreateRequest;
import com.festago.festival.dto.FestivalDetailResponse;
import com.festago.festival.dto.FestivalResponse;
import com.festago.festival.dto.FestivalsResponse;
import com.festago.school.dto.SchoolCreateRequest;
import com.festago.school.dto.SchoolResponse;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.ExtractableResponse;
import io.restassured.response.Response;
import java.time.LocalDate;
import org.springframework.beans.factory.annotation.Autowired;

public class ExampleStep {

@Autowired
CucumberClient cucumberClient;

@Given("๋กœ๊ทธ์ธ์„ ํ•œ ์ƒํƒœ์—์„œ")
public void login() {
String password = "password";
RestAssured.given()
.contentType(ContentType.JSON)
.body(new RootAdminInitializeRequest(password))
.post("admin/initialize")
.then()
.statusCode(200);

ExtractableResponse<Response> response = RestAssured.given()
.contentType(ContentType.JSON)
.body(new AdminLoginRequest("admin", password))
.post("admin/login")
.then()
.extract();

cucumberClient.addAuthToken(response.cookie("token"));
}

@Given("์ถ•์ œ๋ฅผ ์ƒ์„ฑํ•˜๊ณ ")
public void given() {
SchoolResponse schoolResponse = (SchoolResponse) cucumberClient.getData("schoolResponse");
FestivalCreateRequest request = new FestivalCreateRequest("ํ‘ธ์šฐ ์ถ•์ œ", LocalDate.now(),
LocalDate.now().plusDays(1), "thumnail", schoolResponse.id());
FestivalResponse response = RestAssured.given()
.contentType(ContentType.JSON)
.cookie("token", cucumberClient.getToken())
.body(request)
.post("admin/festivals")
.then()
.extract()
.body()
.as(FestivalResponse.class);
cucumberClient.addData("festivalData", response);
}

@Given("{string}๋ฅผ ์ƒ์„ฑํ•˜๊ณ ")
public void makeSchool(String schoolName) {
SchoolCreateRequest request = new SchoolCreateRequest(schoolName, "domain.com");
SchoolResponse response = RestAssured.given()
.contentType(ContentType.JSON)
.cookie("token", cucumberClient.getToken())
.body(request)
.post("admin/schools")
.then()
.extract()
.body()
.as(SchoolResponse.class);
cucumberClient.addData("schoolResponse", response);
}

@Then("์ถ•์ œ๊ฐ€ ์žˆ๋‹ค")
public void then() {
FestivalDetailResponse response = (FestivalDetailResponse) cucumberClient.getData("searchResult");
assertThat(response.name()).isEqualTo("ํ‘ธ์šฐ ์ถ•์ œ");
}

@Then("์ „ ์‹œ๋‚˜๋ฆฌ์˜ค์—์„œ ์ƒ์„ฑ๋œ ๋ฐ์ดํ„ฐ๋Š” ์—†์–ด์ง„๋‹ค")
public void exist() {
FestivalsResponse as = RestAssured.given()
.when()
.get("/festivals")
.then()
.extract()
.as(FestivalsResponse.class);

assertThat(as.festivals().size()).isEqualTo(0);
}

}
Loading

0 comments on commit 639e228

Please sign in to comment.