Commit d4633a9d authored by Marek Skácelík's avatar Marek Skácelík 🫠
Browse files

Merge branch 'testing/statistics' into 'milestone-2'

Testing/statistics

See merge request !41
parents 94017967 2b9cf24e
Loading
Loading
Loading
Loading
Loading
+16 −1
Original line number Diff line number Diff line
@@ -38,7 +38,12 @@ services:
  electricity-price-service:
    build:
      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:
      - "8088:8088"

@@ -62,6 +67,16 @@ services:
    ports:
      - "5431:5432"

  electricity-database:
    image: postgres:latest
    restart: always
    environment:
      POSTGRES_USER: postgres3
      POSTGRES_PASSWORD: password3
      POSTGRES_DB: postgres
    ports:
      - "5433:5438"

  adminer:
    image: adminer
    restart: always
+5 −0
Original line number Diff line number Diff line
@@ -41,6 +41,11 @@
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <modelVersion>4.0.0</modelVersion>

+6 −6
Original line number Diff line number Diff line
       ,--.              ,--.         ,--.      ,--.  ,--.           ,------.        ,--.              ,---.                         ,--.
 ,---. |  |,---.  ,---.,-'  '-.,--.--.`--' ,---.`--',-'  '-.,--. ,--.|  .--. ',--.--.`--' ,---. ,---. '   .-' ,---. ,--.--.,--.  ,--.`--' ,---. ,---.
| .-. :|  | .-. :| .--''-.  .-'|  .--',--.| .--',--.'-.  .-' \  '  / |  '--' ||  .--',--.| .--'| .-. :`.  `-.| .-. :|  .--' \  `'  / ,--.| .--'| .-. :
\   --.|  \   --.\ `--.  |  |  |  |   |  |\ `--.|  |  |  |    \   '  |  | --' |  |   |  |\ `--.\   --..-'    \   --.|  |     \    /  |  |\ `--.\   --.
 `----'`--'`----' `---'  `--'  `--'   `--' `---'`--'  `--'  .-'  /   `--'     `--'   `--' `---' `----'`-----' `----'`--'      `--'   `--' `---' `----'
                                                            `---'
         .-.
   .--.-'        /              /       .-.            /       .-.
  (  (_)     ---/---  .-.   ---/---     `-'    .   ---/---     `-'  .-.      .
   `-.         /     (  |     /        /      / \    /        /    (        / \
 _    )       /       `-'-'  /      _.(__.   / ._)  /      _.(__.   `---'  / ._)
(_.--'                                      /                             /
 No newline at end of file
+76 −0
Original line number Diff line number Diff line
package cz.muni.fi.pa165.statistics;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import cz.muni.fi.pa165.statistics.statistics.StatisticCreateDto;
import cz.muni.fi.pa165.statistics.statistics.StatisticService;
import cz.muni.fi.pa165.statistics.statistics.UserStatisticsDto;
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.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
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 StatisticService statisticService;

    private final static String URL = "/api/user-statistics";
    private final static String CONTENT_TYPE =  "application/json";

    @Test
    void findAllByUserTest() throws Exception {
        StatisticCreateDto statisticCreateDto = StatisticCreateDto.builder().build();
        statisticCreateDto.setMonthNum(3);
        List<UserStatisticsDto> l = new ArrayList<>();
        assertEquals(statisticService.findAllUserStatistics(statisticCreateDto.getUserId()), l);

        String response = mockMvc.perform(get(URL + "/all?userId=" + statisticCreateDto.getUserId())
                        .contentType(CONTENT_TYPE)
                        .content(objectMapper.writeValueAsString(statisticCreateDto)))
                .andExpect(status().isOk())
                .andReturn()
                .getResponse()
                .getContentAsString();
        List<UserStatisticsDto> userStatisticsDto = objectMapper.readValue(response, new TypeReference<List<UserStatisticsDto>>() {});

        assertEquals(userStatisticsDto, l);
    }

    @Test
    void findHouseStatisticsByUserTest() throws Exception {
        StatisticCreateDto statisticCreateDto = StatisticCreateDto.builder().build();
        statisticCreateDto.setMonthNum(3);
        List<UserStatisticsDto> l = new ArrayList<>();
        assertEquals(statisticService.findAllUserStatisticsByHouse(statisticCreateDto.getUserId(), statisticCreateDto.getHouseId()), l);

        String response = mockMvc.perform(get(URL + "/user?userId=" + statisticCreateDto.getUserId()
                        + "&houseId=" + statisticCreateDto.getHouseId())
                        .contentType(CONTENT_TYPE)
                        .content(objectMapper.writeValueAsString(statisticCreateDto)))
                .andExpect(status().isOk())
                .andReturn()
                .getResponse()
                .getContentAsString();
        List<UserStatisticsDto> userStatisticsDto = objectMapper.readValue(response, new TypeReference<List<UserStatisticsDto>>() {});

        assertEquals(userStatisticsDto, l);
    }
}
+54 −0
Original line number Diff line number Diff line
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());
    }

}
Loading