From 3adbdcb7dc3ed022d2b598890e957c62afad1891 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Anesa=20Fazlagi=C4=87?= <xfazlag@fi.muni.cz>
Date: Sun, 16 Apr 2023 17:38:31 +0200
Subject: [PATCH] added the findHouseStatisticsByUserTest and findAllByUserTest

---
 .../statistics/StatisticsControllerTest.java  | 74 +++++++++----------
 1 file changed, 36 insertions(+), 38 deletions(-)

diff --git a/statistics/src/test/java/cz/muni/fi/pa165/statistics/StatisticsControllerTest.java b/statistics/src/test/java/cz/muni/fi/pa165/statistics/StatisticsControllerTest.java
index 06bb747..acaa255 100644
--- a/statistics/src/test/java/cz/muni/fi/pa165/statistics/StatisticsControllerTest.java
+++ b/statistics/src/test/java/cz/muni/fi/pa165/statistics/StatisticsControllerTest.java
@@ -1,8 +1,10 @@
 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.*;
-import org.junit.jupiter.api.BeforeEach;
+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;
@@ -10,11 +12,11 @@ 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 java.util.ArrayList;
+import java.util.List;
 
-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.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
@@ -27,9 +29,6 @@ public class StatisticsControllerTest {
     @Autowired
     private ObjectMapper objectMapper;
 
-    @Autowired
-    private UserStatisticsController userStatisticsController;
-
     @Autowired
     private StatisticService statisticService;
 
@@ -37,42 +36,41 @@ public class StatisticsControllerTest {
     private final static String CONTENT_TYPE =  "application/json";
 
     @Test
-    public void a() {
-
-    }
+    void findAllByUserTest() throws Exception {
+        StatisticCreateDto statisticCreateDto = StatisticCreateDto.builder().build();
+        statisticCreateDto.setMonthNum(3);
+        List<UserStatisticsDto> l = new ArrayList<>();
+        assertEquals(statisticService.findAllUserStatistics(statisticCreateDto.getUserId()), l);
 
-    @BeforeEach
-    void setUp() throws Exception {
-        StatisticCreateDto statisticCreateDto1 = StatisticCreateDto.builder().build();
-        statisticCreateDto1.setMonthNum(3);
-        String response = mockMvc.perform(post(URL)
+        String response = mockMvc.perform(get(URL + "/all?userId=" + statisticCreateDto.getUserId())
                         .contentType(CONTENT_TYPE)
-                        .content(objectMapper.writeValueAsString(statisticCreateDto1)))
-                .andExpect(status().isCreated())
+                        .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 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());
+    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);
     }
 }
-- 
GitLab