Skip to content

Commit

Permalink
Added unit tests and integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sainik73 committed Apr 5, 2021
1 parent e440984 commit 3c16dbb
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/resources/helm/students-api/Chart.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dependencies:
- name: mongodb
repository: https://charts.bitnami.com/bitnami
version: 10.7.0
digest: sha256:da7826bc0d23109b4d1dc34352c678835d69d4f690bcf0ce6da22eb98b74ca7f
generated: "2021-03-18T01:07:07.7375873-07:00"
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.example.openapi.students.repository;

import com.example.openapi.students.db.model.Student;
import lombok.extern.slf4j.Slf4j;
import org.junit.AfterClass;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration;
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* Test class for Student Repository to test custom methods
* @author Kiran
* @since 4/3/2021
*/
@Testcontainers
@DataMongoTest(excludeAutoConfiguration = EmbeddedMongoAutoConfiguration.class)
@Slf4j
class StudentsRepositoryTest {
/* Variable to ensure bootstrapping of repository is done only once */
private static boolean bootstrapped = false;
@Container
static MongoDBContainer mongoDBContainer =
new MongoDBContainer("mongo:4.4.4");

@DynamicPropertySource
static void setProperties(DynamicPropertyRegistry registry) {
mongoDBContainer.start();
registry.add("spring.data.mongodb.uri", mongoDBContainer::getReplicaSetUrl);
log.info("spring.data.mongodb.uri -> " + mongoDBContainer.getReplicaSetUrl());
}

@Autowired
private StudentsRepository studentsRepository;

@BeforeEach
public void beforeSetupTest(){
bootstrapRepository();
}

@AfterClass
public static void cleanUp(){
mongoDBContainer.stop();
}

@Test
void testFindByName(){
List<Student> studentList = this.studentsRepository.findByName("Test");
assertEquals(1, studentList.size());
}

@Test
void testFindByAge(){
List<Student> studentList = this.studentsRepository.findByAge(22);
assertEquals(2, studentList.size());
}

/**
* Load the repository with data
*/
private void bootstrapRepository(){
if(!bootstrapped) {
log.info("Before Bootstrapping the repository........");
Student student = new Student();
student.setStudentid(1);
student.setName("Test");
student.setAge(22);
//this.studentsRepository.save(student);

Student student2 = new Student();
student2.setStudentid(2);
student2.setName("Test2");
student2.setAge(22);
List<Student> studentList = Arrays.asList(student, student2);
this.studentsRepository.insert(studentList);
bootstrapped = true;
log.info("Bootstrapped the repository........");
}

}

}
6 changes: 6 additions & 0 deletions src/test/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
spring:
application:
name: students-api
data:
mongodb:
uri: mongodb://localhost:27017/test
42 changes: 42 additions & 0 deletions src/test/resources/logback-spring.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOGS" value="./logs" />
<appender name="Console"
class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
</Pattern>
</layout>
</appender>
<appender name="RollingFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGS}/students-api-logger.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily and when the file reaches 1 MegaBytes -->
<fileNamePattern>${LOGS}/archived/students-api-logger-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>1MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>

<!-- LOG everything at INFO level -->
<root level="info">
<appender-ref ref="RollingFile" />
<appender-ref ref="Console" />
</root>

<logger name="com.example.openapi.students" level="debug" additivity="false">
<appender-ref ref="RollingFile" />
<appender-ref ref="Console" />
</logger>

</configuration>

0 comments on commit 3c16dbb

Please sign in to comment.