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

added the mapping, repository, dto classes, service config

parent 3d2e9b89
No related branches found
No related tags found
3 merge requests!79Final merge to main,!43Testing user,!36Electricity tarif microservice hibernate add
Pipeline #
Showing
with 316 additions and 125 deletions
......@@ -16,6 +16,16 @@
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.5.3.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
<modelVersion>4.0.0</modelVersion>
......
package cz.muni.fi.pa165.microservice4.electricityprices;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Builder
@Data
public class ElectricityPrice {
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "electricity_price")
public class ElectricityPrice implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "electricity_price_id")
private String id;
@Override
public String toString() {
return "ElectricityPrice{" +
"id='" + id + '\'' +
", companyId='" + companyId + '\'' +
", priceHighTariff=" + priceHighTariff +
", priceLowTariff=" + priceLowTariff +
'}';
}
@Column
private String companyId;
@Column
private double priceHighTariff;
@Column
private double priceLowTariff;
}
package cz.muni.fi.pa165.microservice4.electricityprices;
import cz.muni.fi.pa165.microservice4.electricityprices.dtos.ElectricityPriceGetFullDto;
import cz.muni.fi.pa165.microservice4.electricityprices.dtos.ElectricityPriceHighTariffDto;
import cz.muni.fi.pa165.microservice4.electricityprices.dtos.ElectricityPriceLowTariffDto;
import cz.muni.fi.pa165.microservice4.electricityprices.dtos.ElectricityPricePostDto;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import java.util.List;
@Mapper(componentModel = "spring")
public interface ElectricityPriceMapper {
@Mapping(target = "price", source = "electricityPrice.priceLowTariff")
List<ElectricityPriceLowTariffDto> electricityPriceToElectricityPriceLowTariffDto(List<ElectricityPrice> electricityPrice);
@Mapping(target = "price", source = "electricityPrice.priceHighTariff")
List<ElectricityPriceHighTariffDto> electricityPriceToElectricityPriceHighTariffDto(List<ElectricityPrice> electricityPrice);
List<ElectricityPriceGetFullDto> electricityPriceListToElectricityPriceGetFullDtoList(List<ElectricityPrice> electricityPriceList);
ElectricityPrice electricityPricePostDtoToElectricityPrice(ElectricityPricePostDto electricityPricePostDto);
ElectricityPriceGetFullDto electricityPriceToElectricityPriceGetFullDto(ElectricityPrice electricityPrice);
@Mapping(target = "price", source = "electricityPrice.priceLowTariff")
ElectricityPriceLowTariffDto electricityPriceToElectricityPriceLowTariffDto(ElectricityPrice electricityPrice);
@Mapping(target = "price", source = "electricityPrice.priceHighTariff")
ElectricityPriceHighTariffDto electricityPriceToElectricityPriceHighTariffDto(ElectricityPrice electricityPrice);
}
package cz.muni.fi.pa165.microservice4.electricityprices;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ElectricityPriceRepository extends JpaRepository<ElectricityPrice, String> {
ElectricityPrice findByCompanyId(String companyId);
}
package cz.muni.fi.pa165.microservice4.electricityprices;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
public interface ElectricityPriceService {
public double getElectricityPrice(String companyId); // dependent on time - low or high tariff
double getElectricityPrice(String companyId); // dependent on time - low or high tariff
Page<ElectricityPrice> getAllElectricityPricePagable(Pageable pages);
public List<ElectricityPrice> getAllElectricityPrice();
List<ElectricityPrice> getAllElectricityPrice();
ElectricityPrice setElectricityPrice(ElectricityPriceSetRequest req);
String deleteElectricityPriceForCompany(String companyId);
String deleteElectricityPriceForCompany(String companyId);
}
package cz.muni.fi.pa165.microservice4.electricityprices;
import cz.muni.fi.pa165.microservice4.electricityprices.dtos.ElectricityPriceLowTariffDto;
import jakarta.annotation.PostConstruct;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ExceptionHandler;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ExceptionHandler;
@Service
public class ElectricityPriceServiceImpl implements ElectricityPriceService {
private final ElectricityPriceRepository electricityPriceRepository;
public ElectricityPriceServiceImpl(ElectricityPriceRepository electricityPriceRepository, ElectricityPriceMapper mapStructMapper) {
this.electricityPriceRepository = electricityPriceRepository;
this.mapStructMapper = mapStructMapper;
}
private final ElectricityPriceMapper mapStructMapper;
// to be stored in database in the future
private final List<ElectricityPrice> list = new CopyOnWriteArrayList<>();
@PostConstruct
......@@ -33,12 +44,11 @@ public class ElectricityPriceServiceImpl implements ElectricityPriceService {
@Override
@Transactional(readOnly = true)
public double getElectricityPrice(String companyId) {
return list.stream()
.filter(price -> Objects.equals(companyId, price.getCompanyId()))
.findFirst()
.map(this::getPriceAcordingToDate)
.orElseThrow(() -> new NoElectricityPriceForCompanyException("No electricity price information for that company."));
}
ElectricityPrice electricityPrice = Optional.of(electricityPriceRepository.findByCompanyId(companyId)).orElseThrow(() -> new NoElectricityPriceForCompanyException("No electricity price information for that company."));
ElectricityPriceLowTariffDto electricityPriceLowTariffDto = new ElectricityPriceLowTariffDto();
electricityPriceLowTariffDto.setPrice(getPriceAcordingToDate(electricityPrice));
return 0;
}
private double getPriceAcordingToDate(ElectricityPrice price) {
LocalTime localTime = LocalTime.now();
......@@ -55,7 +65,13 @@ public class ElectricityPriceServiceImpl implements ElectricityPriceService {
@Override
@Transactional(readOnly = true)
public List<ElectricityPrice> getAllElectricityPrice() {
return list;
return electricityPriceRepository.findAll();
}
@Override
@Transactional(readOnly = true)
public Page<ElectricityPrice> getAllElectricityPricePagable(Pageable pages) {
return electricityPriceRepository.findAll(pages);
}
public ElectricityPrice setElectricityPrice(ElectricityPriceSetRequest req) {
......
package cz.muni.fi.pa165.microservice4.electricityprices;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
public class ServiceConfig {
}
package cz.muni.fi.pa165.microservice4.electricityprices.dtos;
public class ElectricityPriceGetFullDto {
private String id;
private String companyId;
private double priceHighTariff;
private double priceLowTariff;
}
package cz.muni.fi.pa165.microservice4.electricityprices.dtos;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class ElectricityPriceHighTariffDto {
private String id;
private String companyId;
private double price;
@Override
public String toString() {
return "ElectricityPriceGetSlimDto{" +
"id='" + id + '\'' +
", companyId='" + companyId + '\'' +
", price=" + price +
'}';
}
}
\ No newline at end of file
package cz.muni.fi.pa165.microservice4.electricityprices.dtos;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class ElectricityPriceLowTariffDto {
private String id;
private String companyId;
private double price;
@Override
public String toString() {
return "ElectricityPriceGetSlimDto{" +
"id='" + id + '\'' +
", companyId='" + companyId + '\'' +
", price=" + price +
'}';
}
}
package cz.muni.fi.pa165.microservice4.electricityprices.dtos;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class ElectricityPricePostDto {
@JsonProperty("id")
private int id;
@NotNull
@JsonProperty("companyId")
private String companyId;
@NotNull
@JsonProperty("priceHighTariff")
private double priceHighTariff;
@NotNull
@JsonProperty("priceLowTariff")
private double priceLowTariff;
}
package cz.muni.fi.pa165.microservice4;
import cz.muni.fi.pa165.microservice4.electricityprices.ElectricityPriceController;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(classes = App.class)
public class AppTest {
@Autowired
ElectricityPriceController electricityPriceController;
@Test
void contextLoads() {
assertThat(electricityPriceController).isNotNull();
}
}
//package cz.muni.fi.pa165.microservice4;
//
//import cz.muni.fi.pa165.microservice4.electricityprices.ElectricityPriceController;
//import org.junit.jupiter.api.Test;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.boot.test.context.SpringBootTest;
//import static org.assertj.core.api.Assertions.assertThat;
//
//@SpringBootTest(classes = App.class)
//public class AppTest {
// @Autowired
// ElectricityPriceController electricityPriceController;
// @Test
// void contextLoads() {
// assertThat(electricityPriceController).isNotNull();
// }
//}
package cz.muni.fi.pa165.microservice4;
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.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalTime;
import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc
class ElectricityPriceControllerTest {
@Autowired
private MockMvc mockMvc;
//dummy data from the list in service class (before using database)
private static final String data = "{\"id\":\"1\",\"companyId\":\"1\",\"priceHighTariff\":300.0,\"priceLowTariff\":200.0}";
@Test
public void shouldReturnEntranceData() throws Exception {
this.mockMvc.perform(get("/api/v1/electricity/electricityprices"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString(data)));
}
@Test
void testGetAllElectricityPrice() throws Exception {
mockMvc.perform(get("/api/v1/electricity/electricityprices")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.accept(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isOk());
}
@Test
public void shouldRegisterElectricityPrice() throws Exception {
this.mockMvc.perform( MockMvcRequestBuilders
.post("/api/v1/electricity/electricityprices")
.content("{\"companyId\":\"2\",\"priceHighTariff\":700.0,\"priceLowTariff\":400.0}")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.jsonPath("$.id").exists());
}
@Test
public void shouldNotThrowElectricityPriceForCompanyException() throws Exception {
this.mockMvc.perform(delete("/api/v1/electricity/electricityprices/2"))
.andDo(print())
.andExpect(status().is(200))
.andExpect(content().string(containsString("deleted electricity prices for company with companyId " + 2)));
}
@Test
public void shouldReturnPriceAccordingToTime() throws Exception {
if(LocalDate.now().getDayOfWeek() == DayOfWeek.SATURDAY || LocalDate.now().getDayOfWeek() == DayOfWeek.SUNDAY)
this.mockMvc.perform(get("/api/v1/electricity/electricityprices/1"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString("200.0")));
else if(LocalTime.now().getHour() > 7 && LocalTime.now().getHour() < 21)
this.mockMvc.perform(get("/api/v1/electricity/electricityprices/1"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString("300.0")));
else
this.mockMvc.perform(get("/api/v1/electricity/electricityprices/1"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString("200.0")));
}
@Test
public void shouldThrowElectricityPriceForCompanyException() throws Exception {
this.mockMvc.perform(delete("/api/v1/electricity/electricityprices/66"))
.andExpect(status().is(400));
}
@Test
public void shouldThrowExceptionForNoElectricityCompany() throws Exception {
this.mockMvc.perform(get("/api/v1/electricity/electricityprices/notanid"))
.andExpect(status().is(400));
}
}
//package cz.muni.fi.pa165.microservice4;
//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.http.MediaType;
//import org.springframework.test.web.servlet.MockMvc;
//import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
//import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
//
//import java.time.DayOfWeek;
//import java.time.LocalDate;
//import java.time.LocalTime;
//
//import static org.hamcrest.Matchers.containsString;
//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
//import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
//@SpringBootTest
//@AutoConfigureMockMvc
//class ElectricityPriceControllerTest {
//
// @Autowired
// private MockMvc mockMvc;
// //dummy data from the list in service class (before using database)
// private static final String data = "{\"id\":\"1\",\"companyId\":\"1\",\"priceHighTariff\":300.0,\"priceLowTariff\":200.0}";
//
// @Test
// public void shouldReturnEntranceData() throws Exception {
// this.mockMvc.perform(get("/api/v1/electricity/electricityprices"))
// .andDo(print())
// .andExpect(status().isOk())
// .andExpect(content().string(containsString(data)));
// }
// @Test
// void testGetAllElectricityPrice() throws Exception {
// mockMvc.perform(get("/api/v1/electricity/electricityprices")
// .contentType(MediaType.APPLICATION_JSON_VALUE)
// .accept(MediaType.APPLICATION_JSON_VALUE))
// .andExpect(status().isOk());
// }
//
// @Test
// public void shouldRegisterElectricityPrice() throws Exception {
// this.mockMvc.perform( MockMvcRequestBuilders
// .post("/api/v1/electricity/electricityprices")
// .content("{\"companyId\":\"2\",\"priceHighTariff\":700.0,\"priceLowTariff\":400.0}")
// .contentType(MediaType.APPLICATION_JSON)
// .accept(MediaType.APPLICATION_JSON))
// .andExpect(MockMvcResultMatchers.jsonPath("$.id").exists());
// }
//
// @Test
// public void shouldNotThrowElectricityPriceForCompanyException() throws Exception {
// this.mockMvc.perform(delete("/api/v1/electricity/electricityprices/2"))
// .andDo(print())
// .andExpect(status().is(200))
// .andExpect(content().string(containsString("deleted electricity prices for company with companyId " + 2)));
// }
// @Test
// public void shouldReturnPriceAccordingToTime() throws Exception {
// if(LocalDate.now().getDayOfWeek() == DayOfWeek.SATURDAY || LocalDate.now().getDayOfWeek() == DayOfWeek.SUNDAY)
// this.mockMvc.perform(get("/api/v1/electricity/electricityprices/1"))
// .andDo(print())
// .andExpect(status().isOk())
// .andExpect(content().string(containsString("200.0")));
// else if(LocalTime.now().getHour() > 7 && LocalTime.now().getHour() < 21)
// this.mockMvc.perform(get("/api/v1/electricity/electricityprices/1"))
// .andDo(print())
// .andExpect(status().isOk())
// .andExpect(content().string(containsString("300.0")));
// else
// this.mockMvc.perform(get("/api/v1/electricity/electricityprices/1"))
// .andDo(print())
// .andExpect(status().isOk())
// .andExpect(content().string(containsString("200.0")));
// }
//
// @Test
// public void shouldThrowElectricityPriceForCompanyException() throws Exception {
// this.mockMvc.perform(delete("/api/v1/electricity/electricityprices/66"))
// .andExpect(status().is(400));
//
// }
//
// @Test
// public void shouldThrowExceptionForNoElectricityCompany() throws Exception {
// this.mockMvc.perform(get("/api/v1/electricity/electricityprices/notanid"))
// .andExpect(status().is(400));
// }
//}
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