Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: PostgreSQL + PostGIS 설정 추가 #3

Merged
merged 2 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion zipsoonbatch/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,19 @@ dependencies {
testImplementation 'org.springframework.batch:spring-batch-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

implementation 'io.github.cdimascio:dotenv-java:3.1.0'
// PostGIS
implementation 'net.postgis:postgis-jdbc:2.5.1'
implementation 'org.locationtech.jts:jts-core:1.19.0'

// Utils
implementation 'me.paulschwarz:spring-dotenv:4.0.0'
implementation 'org.apache.commons:commons-lang3:3.12.0'


testCompileOnly 'org.projectlombok:lombok'
testImplementation 'me.paulschwarz:spring-dotenv:4.0.0'
testAnnotationProcessor 'org.projectlombok:lombok'

}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.zipsoon.zipsoonbatch.config;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableBatchProcessing
public class BatchConfig {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.zipsoon.zipsoonbatch.config;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

import java.time.Duration;

@Configuration
public class RestTemplateConfig {

@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.zipsoon.zipsoonbatch.job;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

@Configuration
public class TestJobConfig {

@Bean
public Job testJob(JobRepository jobRepository, Step testStep) {
return new JobBuilder("testJob", jobRepository)
.start(testStep)
.build();
}

@Bean
public Step testStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
return new StepBuilder("testStep", jobRepository)
.tasklet(
(contribution, chunkContext) -> {
System.out.println("Test Step executed successfully");
return RepeatStatus.FINISHED;
},
transactionManager
)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package com.zipsoon.zipsoonbatch;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

@Slf4j
@SpringBootTest
@ActiveProfiles("test")
class ZipsoonbatchApplicationTests {

@Test
void contextLoads() {
}
@Test
@DisplayName("애플리케이션 컨텍스트가 정상적으로 로드되어야 한다")
void contextLoads() {
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.zipsoon.zipsoonbatch.config;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ActiveProfiles;

import javax.sql.DataSource;

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

@Slf4j
@SpringBootTest
@ActiveProfiles("test")
public class DatabaseConfigTest {

@Autowired
private DataSource dataSource;

@Test
@DisplayName("데이터베이스 연결이 정상적으로 설정되어야 한다")
void databaseIsConnected(@Autowired JdbcTemplate jdbcTemplate) {
String version = jdbcTemplate.queryForObject(
"SELECT version()",
String.class
);
log.info("Database version: {}", version);

assertThat(version)
.isNotNull()
.contains("PostgreSQL");
}

@Test
@DisplayName("PostGIS 확장이 설치되어 있어야 한다")
void postGisExtensionExists(@Autowired JdbcTemplate jdbcTemplate) {
String result = jdbcTemplate.queryForObject(
"SELECT extversion FROM pg_extension WHERE extname = 'postgis'",
String.class
);
log.info("PostGIS version: {}", result);

assertThat(result).isNotNull();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.zipsoon.zipsoonbatch.config;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.*;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.web.client.RestTemplate;

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

@Slf4j
@SpringBootTest
@ActiveProfiles("test")
class RestTemplateConfigTest {

@Autowired
private RestTemplate restTemplate;

@Value("${naver.land.auth-token}")
private String naverAuthToken;

@Test
@DisplayName("RestTemplate이 네이버 부동산 API를 정상적으로 호출할 수 있어야 한다")
void restTemplateCanMakeRequest() throws JsonProcessingException {
HttpHeaders headers = new HttpHeaders();
System.out.println("naverAuthToken: " + naverAuthToken);
headers.set("authorization", "Bearer " + naverAuthToken);
headers.set("referer", "https://new.land.naver.com");

HttpEntity<?> requestEntity = new HttpEntity<>(headers);

ResponseEntity<String> response = restTemplate.exchange(
"https://new.land.naver.com/api/articles?cortarNo=1111018000&page=1",
HttpMethod.GET,
requestEntity,
String.class
);

ObjectMapper objectMapper = new ObjectMapper();
Object json = objectMapper.readValue(response.getBody(), Object.class);
String prettyJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);

log.info("Response from Naver Land API:");
log.info("\n{}", prettyJson);

assertThat(response.getBody()).isNotNull();
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
}
}
10 changes: 10 additions & 0 deletions zipsoonbatch/src/test/resources/application-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
spring:
batch:
job:
enabled: false

logging:
level:
org.springframework.batch: INFO
com.zipsoon.zipsoonbatch: DEBUG

Loading