Skip to content
Snippets Groups Projects
Commit e79230d0 authored by Anesa Fazlagić's avatar Anesa Fazlagić
Browse files

added tests statistics service and controller and docker for electricity price microservice

parent d8d81942
No related branches found
No related tags found
2 merge requests!79Final merge to main,!41Testing/statistics
Pipeline #
...@@ -38,7 +38,12 @@ services: ...@@ -38,7 +38,12 @@ services:
electricity-price-service: electricity-price-service:
build: build:
context: . context: .
dockerfile: ./microservice4/Dockerfile dockerfile: ./electricityTarifMicroservice/Dockerfile
environment:
- spring.datasource.username=postgres3
- spring.datasource.password=password3
- spring.datasource.url=jdbc:postgresql://electricity-database:5438/postgres
- spring.jpa.hibernate.ddl-auto=create
ports: ports:
- "8088:8088" - "8088:8088"
...@@ -62,6 +67,16 @@ services: ...@@ -62,6 +67,16 @@ services:
ports: ports:
- "5431:5432" - "5431:5432"
electricity-database:
image: postgres:latest
restart: always
environment:
POSTGRES_USER: postgres3
POSTGRES_PASSWORD: password3
POSTGRES_DB: postgres
ports:
- "5433:5438"
adminer: adminer:
image: adminer image: adminer
restart: always restart: always
......
...@@ -41,6 +41,11 @@ ...@@ -41,6 +41,11 @@
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
......
,--. ,--. ,--. ,--. ,--. ,------. ,--. ,---. ,--. .-.
,---. | |,---. ,---.,-' '-.,--.--.`--' ,---.`--',-' '-.,--. ,--.| .--. ',--.--.`--' ,---. ,---. ' .-' ,---. ,--.--.,--. ,--.`--' ,---. ,---. .--.-' / / .-. / .-.
| .-. :| | .-. :| .--''-. .-'| .--',--.| .--',--.'-. .-' \ ' / | '--' || .--',--.| .--'| .-. :`. `-.| .-. :| .--' \ `' / ,--.| .--'| .-. : ( (_) ---/--- .-. ---/--- `-' . ---/--- `-' .-. .
\ --.| \ --.\ `--. | | | | | |\ `--.| | | | \ ' | | --' | | | |\ `--.\ --..-' \ --.| | \ / | |\ `--.\ --. `-. / ( | / / / \ / / ( / \
`----'`--'`----' `---' `--' `--' `--' `---'`--' `--' .-' / `--' `--' `--' `---' `----'`-----' `----'`--' `--' `--' `---' `----' _ ) / `-'-' / _.(__. / ._) / _.(__. `---' / ._)
`---' (_.--' / /
\ No newline at end of file
package cz.muni.fi.pa165.statistics;
import com.fasterxml.jackson.databind.ObjectMapper;
import cz.muni.fi.pa165.statistics.statistics.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
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.test.context.TestPropertySource;
import org.springframework.test.web.servlet.MockMvc;
import java.io.IOException;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc
@TestPropertySource(locations = "classpath:application-test.properties")
public class StatisticsControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@Autowired
private UserStatisticsController userStatisticsController;
@Autowired
private StatisticService statisticService;
private final static String URL = "/api/user-statistics";
private final static String CONTENT_TYPE = "application/json";
@Test
public void a() {
}
@BeforeEach
void setUp() throws Exception {
StatisticCreateDto statisticCreateDto1 = StatisticCreateDto.builder().build();
statisticCreateDto1.setMonthNum(3);
String response = mockMvc.perform(post(URL)
.contentType(CONTENT_TYPE)
.content(objectMapper.writeValueAsString(statisticCreateDto1)))
.andExpect(status().isCreated())
.andReturn()
.getResponse()
.getContentAsString();
}
@Test
void createNonExistingCompanyTest() throws Exception {
// StatisticCreateDto statisticCreateDto = StatisticCreateDto.builder().build();
//
// assertNull(statisticService.findAllUserStatistics(statisticCreateDto.getUserId()));
//
// String response = mockMvc.perform(post(URL)
// .contentType(CONTENT_TYPE)
// .content(objectMapper.writeValueAsString(statisticCreateDto)))
// .andExpect(status().isCreated())
// .andReturn()
// .getResponse()
// .getContentAsString();
//
// UserStatisticsDto userStatisticsDto = objectMapper.readValue(response, UserStatisticsDto.class);
// assertThat(userStatisticsDto.getUserId()).isNotNull();
// assertThat(userStatisticsDto.getUserEmail()).isEqualTo(statisticCreateDto.getUserEmail());
// assertThat(userStatisticsDto.getUserId()).isEqualTo(statisticCreateDto.getUserId());
// assertThat(userStatisticsDto.getYearNum()).isEqualTo(statisticCreateDto.getYearNum());
// assertThat(userStatisticsDto.getMonthNum()).isEqualTo(statisticCreateDto.getMonthNum());
}
}
package cz.muni.fi.pa165.statistics;
import cz.muni.fi.pa165.statistics.statistics.StatisticRepository;
import cz.muni.fi.pa165.statistics.statistics.StatisticService;
import cz.muni.fi.pa165.statistics.statistics.UserStatistic;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.openMocks;
public class StatisticsServiceTest {
@InjectMocks
private StatisticService statisticService;
@Mock
private StatisticRepository statisticRepository;
@BeforeEach
void init() { openMocks(this); }
private static final String userIdTest = "1";
@Test
void shouldGetAllUserStatisticsForUser() {
UserStatistic userStatisticForHouse1ForMonth1 = UserStatistic.builder().userId(userIdTest).userEmail("user123@gmail.com").houseId("1").monthNum(1).monthlyConsumption(45000.0).build();
UserStatistic userStatisticForHouse2ForMonth1 = UserStatistic.builder().userId(userIdTest).userEmail("user123@gmail.com").houseId("2").monthNum(1).monthlyConsumption(45000.0).build();
List<UserStatistic> userStatisticList = List.of(userStatisticForHouse1ForMonth1, userStatisticForHouse2ForMonth1);
when(statisticRepository.findAllByUser(userStatisticForHouse1ForMonth1.getUserId())).thenReturn(userStatisticList);
List<UserStatistic> userStatistics = statisticService.findAllUserStatistics(userIdTest);
assertEquals(userStatisticList, userStatistics);
}
@Test
void shouldGetUserStatisticByUserId() {
UserStatistic userStatistic = UserStatistic.builder().userId(userIdTest).userEmail("user123@gmail.com").houseId("1").monthNum(154).build();
when(statisticRepository.save(userStatistic)).thenReturn(userStatistic);
}
@Test
void shouldFindAllUserStatisticsByHouseEmpty() {
List<UserStatistic> allUserStatisticsByHouse = statisticService.findAllUserStatisticsByHouse("1", "1");
assertEquals(0, allUserStatisticsByHouse.size());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence https://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="jpa.persistence.core" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>cz.muni.fi.pa165.statistics.statistics.UserStatistic</class>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update" /> <!-- create / create-drop / update -->
<property name="hibernate.show_sql" value="true" /> <!-- Show SQL in console -->
<property name="hibernate.format_sql" value="true" /> <!-- Show SQL formatted -->
</properties>
</persistence-unit>
</persistence>
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.show-sql=true
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d %5p %40.40c:%4L - %m%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="console"/>
</root>
<logger name="org.hibernate.SQL" level="DEBUG"/>
</configuration>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment