Skip to content

Commit

Permalink
[feat] #5 mongo db base setup added
Browse files Browse the repository at this point in the history
 - docker-compose.yml added for local development
 -  profiling added
 - connection mongo db established
  • Loading branch information
Ramachandran Nellaiyappan committed Mar 10, 2024
1 parent f6bc830 commit bbe1c2b
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 11 deletions.
33 changes: 33 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
version: "3.8"
services:
mongodb:
image: mongo
container_name: mongo
ports:
- "27017:27017"
volumes:
- data:/data
environment:
- MONGO_INITDB_ROOT_USERNAME=mongodb_user
- MONGO_INITDB_ROOT_PASSWORD=mongodb_pwd

mongo-express:
image: mongo-express
container_name: mongo-express
restart: always
ports:
- "9090:8081"
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=mongodb_user
- ME_CONFIG_MONGODB_ADMINPASSWORD=mongodb_pwd
- ME_CONFIG_MONGODB_SERVER=mongo
- ME_CONFIG_MONGODB_AUTH_USERNAME=admin
- ME_CONFIG_MONGODB_AUTH_PASSWORD=pass

volumes:
data: {}

networks:
default:
name: mongodb_network

51 changes: 43 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,24 @@
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
Expand All @@ -55,17 +55,52 @@
<artifactId>thymeleaf-extras-springsecurity6</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-docker-compose</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mongodb</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
package com.github.nramc.dev.journey.api;

import com.github.nramc.dev.journey.api.data.entity.LocationEntity;
import com.github.nramc.dev.journey.api.data.repository.LocationRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@Slf4j
public class JourneyApiApplication {

public static void main(String[] args) {
SpringApplication.run(JourneyApiApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(JourneyApiApplication.class, args);
}

@Bean
CommandLineRunner commandLineRunner(LocationRepository locationRepository) {
return args -> {
LocationEntity entity = new LocationEntity("Munich");
locationRepository.save(entity);

log.info("Number of documents found: {}", locationRepository.count());

locationRepository.findAll().forEach(location -> log.info("Found: {}", location));
};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.github.nramc.dev.journey.api.data.entity;

import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document("location")
@Data
public class LocationEntity {
@Id
private String id;

private String name;

public LocationEntity(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.nramc.dev.journey.api.data.repository;

import com.github.nramc.dev.journey.api.data.entity.LocationEntity;
import org.springframework.data.mongodb.repository.MongoRepository;

public interface LocationRepository extends MongoRepository<LocationEntity, String> {
}
Empty file.
7 changes: 7 additions & 0 deletions src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
spring:
data:
mongodb:
database: ${DATABASE_NAME}
uri: ${MONGODB_CONNECTION_URL}


4 changes: 4 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
spring:
application:
name: ${app.name}
data:
mongodb:
database: journey-dev
auto-index-creation: true

app:
name: Journey API
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.ApplicationContext;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

@Testcontainers
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class JourneyApiApplicationTests {
@Container
@ServiceConnection
static MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo:latest"));

@Autowired
private ApplicationContext applicationContext;
@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.github.nramc.dev.journey.api.web.resources.mvc.home;

import com.github.nramc.dev.journey.api.config.ApplicationProperties;
import com.github.nramc.dev.journey.api.data.repository.LocationRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
Expand All @@ -15,6 +17,7 @@
@WebMvcTest(HomeResource.class)
@ActiveProfiles({"prod", "test"})
@EnableConfigurationProperties({ApplicationProperties.class})
@MockBean({LocationRepository.class})
class HomeResourceTest {
@Autowired
private MockMvc mvc;
Expand Down

0 comments on commit bbe1c2b

Please sign in to comment.