|
| 1 | +package org.lucoenergia.conluz.infrastructure.admin.supply.production; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.AfterEach; |
| 4 | +import org.junit.jupiter.api.BeforeEach; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import org.lucoenergia.conluz.domain.admin.supply.Supply; |
| 7 | +import org.lucoenergia.conluz.domain.admin.supply.SupplyMother; |
| 8 | +import org.lucoenergia.conluz.domain.admin.supply.create.CreateSupplyRepository; |
| 9 | +import org.lucoenergia.conluz.domain.admin.user.User; |
| 10 | +import org.lucoenergia.conluz.domain.admin.user.UserMother; |
| 11 | +import org.lucoenergia.conluz.domain.admin.user.create.CreateUserRepository; |
| 12 | +import org.lucoenergia.conluz.domain.shared.UserId; |
| 13 | +import org.lucoenergia.conluz.infrastructure.production.EnergyProductionInfluxLoader; |
| 14 | +import org.lucoenergia.conluz.infrastructure.shared.BaseControllerTest; |
| 15 | +import org.lucoenergia.conluz.infrastructure.shared.security.auth.JwtAuthenticationFilter; |
| 16 | +import org.springframework.beans.factory.annotation.Autowired; |
| 17 | +import org.springframework.http.HttpHeaders; |
| 18 | +import org.springframework.http.HttpStatus; |
| 19 | +import org.springframework.transaction.annotation.Transactional; |
| 20 | + |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.util.UUID; |
| 23 | + |
| 24 | +import static org.hamcrest.Matchers.containsString; |
| 25 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 26 | +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; |
| 27 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
| 28 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 29 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 30 | + |
| 31 | +@Transactional |
| 32 | +class GetSupplyMonthlyProductionControllerTest extends BaseControllerTest { |
| 33 | + |
| 34 | + private static final String URL = "/api/v1/supplies"; |
| 35 | + private static final String START_DATE = "2023-09-01T00:00:00.000+02:00"; |
| 36 | + private static final String END_DATE = "2023-09-01T23:00:00.000+02:00"; |
| 37 | + |
| 38 | + @Autowired |
| 39 | + private CreateUserRepository createUserRepository; |
| 40 | + @Autowired |
| 41 | + private CreateSupplyRepository createSupplyRepository; |
| 42 | + @Autowired |
| 43 | + private EnergyProductionInfluxLoader energyProductionInfluxLoader; |
| 44 | + |
| 45 | + @BeforeEach |
| 46 | + void beforeEach() { |
| 47 | + energyProductionInfluxLoader.loadData(); |
| 48 | + } |
| 49 | + |
| 50 | + @AfterEach |
| 51 | + void afterEach() { |
| 52 | + energyProductionInfluxLoader.clearData(); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void testGetSupplyMonthlyProductionSuccess() throws Exception { |
| 57 | + String authHeader = loginAsDefaultAdmin(); |
| 58 | + |
| 59 | + User user = createUserRepository.create(UserMother.randomUser()); |
| 60 | + Supply supply = createSupplyRepository.create(SupplyMother.random(user).build(), UserId.of(user.getId())); |
| 61 | + |
| 62 | + mockMvc.perform(get(URL + "/" + supply.getId() + "/production/monthly") |
| 63 | + .header(HttpHeaders.AUTHORIZATION, authHeader) |
| 64 | + .queryParam("startDate", START_DATE) |
| 65 | + .queryParam("endDate", END_DATE)) |
| 66 | + .andExpect(status().isOk()) |
| 67 | + .andExpect(content().string(containsString("power"))); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void testGetSupplyMonthlyProductionWithMissingStartDate() throws Exception { |
| 72 | + String authHeader = loginAsDefaultAdmin(); |
| 73 | + |
| 74 | + User user = createUserRepository.create(UserMother.randomUser()); |
| 75 | + Supply supply = createSupplyRepository.create(SupplyMother.random(user).build(), UserId.of(user.getId())); |
| 76 | + |
| 77 | + mockMvc.perform(get(URL + "/" + supply.getId() + "/production/monthly") |
| 78 | + .header(HttpHeaders.AUTHORIZATION, authHeader) |
| 79 | + .queryParam("endDate", END_DATE)) |
| 80 | + .andDo(print()) |
| 81 | + .andExpect(status().isBadRequest()) |
| 82 | + .andExpect(content().string(containsString("\"traceId\":"))) |
| 83 | + .andExpect(content().string(containsString("\"timestamp\":"))) |
| 84 | + .andExpect(content().string(containsString("\"status\":400"))) |
| 85 | + .andExpect(content().string(containsString("\"message\":\"El parámetro con nombre 'startDate' es obligatorio.\""))); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void testGetSupplyMonthlyProductionWithMissingEndDate() throws Exception { |
| 90 | + String authHeader = loginAsDefaultAdmin(); |
| 91 | + |
| 92 | + User user = createUserRepository.create(UserMother.randomUser()); |
| 93 | + Supply supply = createSupplyRepository.create(SupplyMother.random(user).build(), UserId.of(user.getId())); |
| 94 | + |
| 95 | + mockMvc.perform(get(URL + "/" + supply.getId() + "/production/monthly") |
| 96 | + .header(HttpHeaders.AUTHORIZATION, authHeader) |
| 97 | + .queryParam("startDate", START_DATE)) |
| 98 | + .andDo(print()) |
| 99 | + .andExpect(status().isBadRequest()) |
| 100 | + .andExpect(content().string(containsString("\"traceId\":"))) |
| 101 | + .andExpect(content().string(containsString("\"timestamp\":"))) |
| 102 | + .andExpect(content().string(containsString("\"status\":400"))) |
| 103 | + .andExpect(content().encoding(StandardCharsets.UTF_8)) |
| 104 | + .andExpect(content().string(containsString("\"message\":\"El parámetro con nombre 'endDate' es obligatorio.\""))); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void testGetSupplyMonthlyProductionWithUnknownSupply() throws Exception { |
| 109 | + String authHeader = loginAsDefaultAdmin(); |
| 110 | + UUID supplyId = UUID.randomUUID(); |
| 111 | + |
| 112 | + mockMvc.perform(get(URL + "/" + supplyId + "/production/monthly") |
| 113 | + .header(HttpHeaders.AUTHORIZATION, authHeader) |
| 114 | + .queryParam("startDate", START_DATE) |
| 115 | + .queryParam("endDate", END_DATE)) |
| 116 | + .andExpect(status().isNotFound()) |
| 117 | + .andExpect(content().string(containsString("\"traceId\":"))) |
| 118 | + .andExpect(content().string(containsString("\"timestamp\":"))) |
| 119 | + .andExpect(content().string(containsString("\"status\":404"))) |
| 120 | + .andExpect(content().string(containsString(String.format("\"message\":\"El punto de suministro con identificador '%s' no ha sido encontrado. Revise que el identificador sea correcto.\"", supplyId)))); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void testWithMissingToken() throws Exception { |
| 125 | + UUID randomId = UUID.randomUUID(); |
| 126 | + |
| 127 | + mockMvc.perform(get(URL + "/" + randomId + "/production/monthly") |
| 128 | + .queryParam("startDate", START_DATE) |
| 129 | + .queryParam("endDate", END_DATE)) |
| 130 | + .andDo(print()) |
| 131 | + .andExpect(status().isUnauthorized()) |
| 132 | + .andExpect(jsonPath("$.timestamp").isNotEmpty()) |
| 133 | + .andExpect(jsonPath("$.status").value(HttpStatus.UNAUTHORIZED.value())) |
| 134 | + .andExpect(jsonPath("$.message").isNotEmpty()) |
| 135 | + .andExpect(jsonPath("$.traceId").isNotEmpty()); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + void testWithWrongToken() throws Exception { |
| 140 | + UUID randomId = UUID.randomUUID(); |
| 141 | + final String wrongToken = JwtAuthenticationFilter.AUTHORIZATION_HEADER_PREFIX + "wrong"; |
| 142 | + |
| 143 | + mockMvc.perform(get(URL + "/" + randomId + "/production/monthly") |
| 144 | + .queryParam("startDate", START_DATE) |
| 145 | + .queryParam("endDate", END_DATE) |
| 146 | + .header(HttpHeaders.AUTHORIZATION, wrongToken)) |
| 147 | + .andExpect(status().isUnauthorized()) |
| 148 | + .andExpect(jsonPath("$.timestamp").isNotEmpty()) |
| 149 | + .andExpect(jsonPath("$.status").value(HttpStatus.UNAUTHORIZED.value())) |
| 150 | + .andExpect(jsonPath("$.message").isNotEmpty()) |
| 151 | + .andExpect(jsonPath("$.traceId").isNotEmpty()); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + void testWithExpiredToken() throws Exception { |
| 156 | + UUID randomId = UUID.randomUUID(); |
| 157 | + final String expiredToken = JwtAuthenticationFilter.AUTHORIZATION_HEADER_PREFIX + |
| 158 | + "eyJhbGciOiJIUzI1NiJ9.eyJyb2xlIjoiQURNSU4iLCJzdWIiOiJiMTFlMTgxNS1mNzE0LTRmNGEtOGZjMS0yNjQxM2FmM2YzYmIiLCJpYXQiOjE3MDQyNzkzNzIsImV4cCI6MTcwNDI4MTE3Mn0.jO3pgdDj4mg9TnRzL7f8RUL1ytJS7057jAg6zaCcwn0"; |
| 159 | + |
| 160 | + mockMvc.perform(get(URL + "/" + randomId + "/production/monthly") |
| 161 | + .queryParam("startDate", START_DATE) |
| 162 | + .queryParam("endDate", END_DATE) |
| 163 | + .header(HttpHeaders.AUTHORIZATION, expiredToken)) |
| 164 | + .andDo(print()) |
| 165 | + .andExpect(status().isUnauthorized()) |
| 166 | + .andExpect(jsonPath("$.timestamp").isNotEmpty()) |
| 167 | + .andExpect(jsonPath("$.status").value(HttpStatus.UNAUTHORIZED.value())) |
| 168 | + .andExpect(jsonPath("$.message").isNotEmpty()) |
| 169 | + .andExpect(jsonPath("$.traceId").isNotEmpty()); |
| 170 | + } |
| 171 | +} |
0 commit comments