Compare commits
9 Commits
initWishli
...
3b9b0769d1
| Author | SHA1 | Date | |
|---|---|---|---|
| 3b9b0769d1 | |||
| 3e5baa34d1 | |||
|
|
6363f966f6 | ||
|
|
c642f6f87b | ||
|
|
65524d0f25 | ||
|
|
71fdf1640a | ||
| 8fae9f1e55 | |||
| d869a18901 | |||
|
|
1d55f40753 |
34
pom.xml
34
pom.xml
@@ -10,7 +10,7 @@
|
||||
</parent>
|
||||
<groupId>_11.asktpk</groupId>
|
||||
<artifactId>ArtisanConnectBackend</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<version>1.0.0</version>
|
||||
<name>ArtisanConnectBackend</name>
|
||||
<description>ArtisanConnectBackend</description>
|
||||
<url/>
|
||||
@@ -34,31 +34,16 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-rest</artifactId>
|
||||
</dependency>
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.springframework.boot</groupId>-->
|
||||
<!-- <artifactId>spring-boot-starter-oauth2-client</artifactId>-->
|
||||
<!-- </dependency>-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
<scope>runtime</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.springframework.boot</groupId>-->
|
||||
<!-- <artifactId>spring-boot-docker-compose</artifactId>-->
|
||||
<!-- <scope>runtime</scope>-->
|
||||
<!-- <optional>true</optional>-->
|
||||
<!-- </dependency>-->
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
@@ -73,15 +58,10 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.springframework.security</groupId>-->
|
||||
<!-- <artifactId>spring-security-test</artifactId>-->
|
||||
<!-- <scope>test</scope>-->
|
||||
<!-- </dependency>-->
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
<version>4.9.3</version>
|
||||
<version>4.12.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
@@ -93,11 +73,11 @@
|
||||
<artifactId>jakarta.validation-api</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</dependency>
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.springdoc</groupId>-->
|
||||
<!-- <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>-->
|
||||
<!-- <version>2.8.5</version>-->
|
||||
<!-- </dependency>-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -3,6 +3,7 @@ package _11.asktpk.artisanconnectbackend.controller;
|
||||
import _11.asktpk.artisanconnectbackend.dto.RequestResponseDTO;
|
||||
import _11.asktpk.artisanconnectbackend.service.ImageService;
|
||||
import _11.asktpk.artisanconnectbackend.service.NoticeService;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@@ -63,18 +64,17 @@ public class ImageController {
|
||||
}
|
||||
|
||||
@GetMapping("/list/{id}")
|
||||
public ResponseEntity<List<String>> getImagesNamesList(@PathVariable("id") Long noticeId) {
|
||||
if(noticeId == null) {
|
||||
return ResponseEntity.badRequest().body(Collections.singletonList("Notice ID is invalid or does not exist."));
|
||||
}
|
||||
|
||||
public ResponseEntity<?> getImagesNamesList(@PathVariable("id") Long noticeId) {
|
||||
List<String> result;
|
||||
try {
|
||||
noticeService.getNoticeById(noticeId);
|
||||
result = imageService.getImagesList(noticeId);
|
||||
return ResponseEntity.ok(result);
|
||||
} catch (EntityNotFoundException e) {
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new RequestResponseDTO(e.getMessage()));
|
||||
} catch (Exception e) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(Collections.singletonList(e.getMessage()));
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new RequestResponseDTO(e.getMessage()));
|
||||
}
|
||||
return ResponseEntity.ok(result);
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete/{filename}")
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package _11.asktpk.artisanconnectbackend.controller;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.dto.NoticeAdditionDTO;
|
||||
import _11.asktpk.artisanconnectbackend.dto.NoticeBoostDTO;
|
||||
import _11.asktpk.artisanconnectbackend.dto.RequestResponseDTO;
|
||||
import _11.asktpk.artisanconnectbackend.service.ClientService;
|
||||
import _11.asktpk.artisanconnectbackend.service.NoticeService;
|
||||
import _11.asktpk.artisanconnectbackend.dto.NoticeDTO;
|
||||
@@ -29,7 +31,7 @@ public class NoticeController {
|
||||
}
|
||||
|
||||
@GetMapping("/get/{id}")
|
||||
public ResponseEntity getNoticeById(@PathVariable long id) {
|
||||
public ResponseEntity<?> getNoticeById(@PathVariable long id) {
|
||||
if (noticeService.noticeExists(id)) {
|
||||
return ResponseEntity.ok(noticeService.getNoticeById(id));
|
||||
} else {
|
||||
@@ -98,12 +100,22 @@ public class NoticeController {
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete/{id}")
|
||||
public ResponseEntity deleteNotice(@PathVariable("id") long id) {
|
||||
public ResponseEntity<RequestResponseDTO> deleteNotice(@PathVariable("id") long id) {
|
||||
if (noticeService.noticeExists(id)) {
|
||||
noticeService.deleteNotice(id);
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
return ResponseEntity.status(HttpStatus.OK).body(new RequestResponseDTO("Pomyślnie usunięto ogłoszenie o ID: " + id));
|
||||
} else {
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new RequestResponseDTO("Nie znaleziono ogłoszenia o ID: " + id));
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/boost/{id}")
|
||||
public ResponseEntity<RequestResponseDTO> boostNotice(@PathVariable("id") long clientId, @RequestBody NoticeBoostDTO dto) {
|
||||
if (!noticeService.isNoticeOwnedByClient(dto.getNoticeId(), clientId)) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new RequestResponseDTO("Ogłoszenie nie istnieje lub nie należy do zalogowanego klienta."));
|
||||
}
|
||||
noticeService.boostNotice(dto.getNoticeId());
|
||||
|
||||
return ResponseEntity.status(HttpStatus.OK).body(new RequestResponseDTO("Ogłoszenie zostało pomyślnie wypromowane."));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package _11.asktpk.artisanconnectbackend.controller;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.dto.*;
|
||||
import _11.asktpk.artisanconnectbackend.entities.Order;
|
||||
import _11.asktpk.artisanconnectbackend.service.ClientService;
|
||||
import _11.asktpk.artisanconnectbackend.service.OrderService;
|
||||
import _11.asktpk.artisanconnectbackend.service.PaymentService;
|
||||
import _11.asktpk.artisanconnectbackend.utils.Enums;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/orders")
|
||||
public class OrderController {
|
||||
|
||||
private final OrderService orderService;
|
||||
private final PaymentService paymentService;
|
||||
|
||||
public OrderController(OrderService orderService, PaymentService paymentService) {
|
||||
this.orderService = orderService;
|
||||
this.paymentService = paymentService;
|
||||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
public ResponseEntity addClient(@RequestBody OrderDTO orderDTO) {
|
||||
return new ResponseEntity<>(orderService.addOrder(orderDTO), HttpStatus.CREATED);
|
||||
}
|
||||
|
||||
@PutMapping("/changeStatus")
|
||||
public ResponseEntity changeStatus(@RequestBody OrderStatusDTO orderStatusDTO) {
|
||||
return new ResponseEntity<>(orderService.changeOrderStatus(orderStatusDTO.getId(),orderStatusDTO.getStatus()), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PostMapping("/token")
|
||||
public ResponseEntity<?> fetchToken() {
|
||||
Order order = orderService.getOrderById(1L);
|
||||
OAuthPaymentResponseDTO authPaymentDTO= paymentService.getOAuthToken();
|
||||
TransactionPaymentRequestDTO.Payer payer = new TransactionPaymentRequestDTO.Payer(
|
||||
"patryk@test.pl", "Patryk Test");
|
||||
|
||||
String paymentDescription = order.getOrderType() == Enums.OrderType.ACTIVATION ? "Aktywacja ogłoszenia" : "Podbicie ogłoszenia";
|
||||
paymentDescription += order.getNotice().getTitle();
|
||||
TransactionPaymentRequestDTO request = new TransactionPaymentRequestDTO(
|
||||
order.getAmount(), paymentDescription, payer);
|
||||
String response = paymentService.createTransaction(order,authPaymentDTO.getAccess_token(), request);
|
||||
System.out.println(response);
|
||||
|
||||
return ResponseEntity.ok(authPaymentDTO.getAccess_token());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package _11.asktpk.artisanconnectbackend.dto;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter @Setter
|
||||
public class NoticeBoostDTO {
|
||||
private Long noticeId;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package _11.asktpk.artisanconnectbackend.dto;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class OAuthPaymentResponseDTO {
|
||||
private long issued_at;
|
||||
private String scope;
|
||||
private String token_type;
|
||||
private int expires_in;
|
||||
private String client_id;
|
||||
private String access_token;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package _11.asktpk.artisanconnectbackend.dto;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.utils.Enums;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class OrderDTO {
|
||||
private Long clientId;
|
||||
private Long noticeId;
|
||||
private Enums.OrderType orderType;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package _11.asktpk.artisanconnectbackend.dto;
|
||||
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.utils.Enums;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter @Setter
|
||||
public class OrderStatusDTO {
|
||||
public long id;
|
||||
public Enums.OrderStatus status;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package _11.asktpk.artisanconnectbackend.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class TransactionPaymentRequestDTO {
|
||||
private double amount;
|
||||
private String description;
|
||||
private Payer payer;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class Payer {
|
||||
private String email;
|
||||
private String name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package _11.asktpk.artisanconnectbackend.dto;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class TransactionPaymentResponseDTO {
|
||||
private String result;
|
||||
private String requestId;
|
||||
private String transactionId;
|
||||
private String title;
|
||||
private String posId;
|
||||
private String status;
|
||||
private DateInfo date;
|
||||
private double amount;
|
||||
private String currency;
|
||||
private String description;
|
||||
private String hiddenDescription;
|
||||
private Payer payer;
|
||||
private Payments payments;
|
||||
private String transactionPaymentUrl;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public static class DateInfo {
|
||||
private String creation;
|
||||
private String realization;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public static class Payer {
|
||||
private String payerId;
|
||||
private String email;
|
||||
private String name;
|
||||
private String phone;
|
||||
private String address;
|
||||
private String city;
|
||||
private String country;
|
||||
private String postalCode;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public static class Payments {
|
||||
private String status;
|
||||
private String method;
|
||||
private double amountPaid;
|
||||
private DateInfo date;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -33,5 +33,5 @@ public class Client {
|
||||
// private List<Notice> notices;
|
||||
|
||||
@OneToMany(mappedBy = "client", cascade = CascadeType.ALL)
|
||||
private List<Orders> orders;
|
||||
private List<Order> orders;
|
||||
}
|
||||
|
||||
@@ -39,8 +39,8 @@ public class Notice {
|
||||
private List<AttributesNotice> attributesNotices;
|
||||
|
||||
@OneToMany(mappedBy = "notice", cascade = CascadeType.ALL)
|
||||
private List<Orders> orders;
|
||||
private List<Order> orders;
|
||||
|
||||
@OneToMany(mappedBy = "notice", cascade = CascadeType.ALL)
|
||||
private List<Payments> payments;
|
||||
// @OneToMany(mappedBy = "notice", cascade = CascadeType.ALL)
|
||||
// private List<Payment> payment;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package _11.asktpk.artisanconnectbackend.entities;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.utils.Enums;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Entity
|
||||
@Table(name = "orders")
|
||||
@Getter
|
||||
@Setter
|
||||
public class Order {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "id_client")
|
||||
private Client client;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "id_notice")
|
||||
private Notice notice;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
@Column(nullable = false)
|
||||
private Enums.OrderType orderType;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
@Column(nullable = false)
|
||||
private Enums.OrderStatus status;
|
||||
|
||||
@Column(nullable = false)
|
||||
private Double amount;
|
||||
|
||||
@Column(nullable = false)
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
private LocalDateTime updatedAt;
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package _11.asktpk.artisanconnectbackend.entities;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.utils.Enums.Status;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "orders")
|
||||
public class Orders {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long idOrder;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "id_user")
|
||||
private Client client;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "id_notice")
|
||||
private Notice notice;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
private Status status;
|
||||
|
||||
// Getters, setters, and constructors
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package _11.asktpk.artisanconnectbackend.entities;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.utils.Enums;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Entity
|
||||
@Table(name = "payment")
|
||||
@Getter @Setter
|
||||
public class Payment {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long idPayment;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "id_order")
|
||||
private Order order;
|
||||
|
||||
private Double amount;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
@Column(nullable = false)
|
||||
private Enums.PaymentStatus status;
|
||||
|
||||
private String transactionPaymentUrl;
|
||||
|
||||
private String transactionId;
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package _11.asktpk.artisanconnectbackend.entities;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.utils.Enums.Status;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "payments")
|
||||
public class Payments {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long idPayment;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "id_order")
|
||||
private Orders order;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "id_notice")
|
||||
private Notice notice;
|
||||
|
||||
private Double noticePublishPrice;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
private Status status;
|
||||
|
||||
private String sessionId;
|
||||
|
||||
// Getters, setters, and constructors
|
||||
}
|
||||
@@ -5,4 +5,7 @@ import _11.asktpk.artisanconnectbackend.entities.Notice;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface NoticeRepository extends JpaRepository<Notice, Long> {
|
||||
|
||||
boolean existsByIdNoticeAndClientId(long noticeId, long clientId);
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package _11.asktpk.artisanconnectbackend.repository;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.entities.Order;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface OrderRepository extends JpaRepository<Order, Long> {
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package _11.asktpk.artisanconnectbackend.repository;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.entities.Payment;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface PaymentRepository extends JpaRepository<Payment, Long> {
|
||||
|
||||
}
|
||||
@@ -5,25 +5,34 @@ import _11.asktpk.artisanconnectbackend.entities.Notice;
|
||||
import _11.asktpk.artisanconnectbackend.repository.ClientRepository;
|
||||
import _11.asktpk.artisanconnectbackend.repository.NoticeRepository;
|
||||
import _11.asktpk.artisanconnectbackend.dto.NoticeDTO;
|
||||
//import _11.asktpk.artisanconnectbackend.service.WishlistService;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class NoticeService {
|
||||
private static final Logger logger = LogManager.getLogger(NoticeService.class);
|
||||
|
||||
@Value("${file.upload-dir}")
|
||||
private String uploadDir;
|
||||
|
||||
private final NoticeRepository noticeRepository;
|
||||
private final ClientRepository clientRepository;
|
||||
private final WishlistService wishlistService;
|
||||
private final ImageService imageService;
|
||||
|
||||
public NoticeService(NoticeRepository noticeRepository, ClientRepository clientRepository, WishlistService wishlistService) {
|
||||
public NoticeService(NoticeRepository noticeRepository, ClientRepository clientRepository, WishlistService wishlistService, ImageService imageService) {
|
||||
this.noticeRepository = noticeRepository;
|
||||
this.clientRepository = clientRepository;
|
||||
this.wishlistService = wishlistService;
|
||||
this.imageService = imageService;
|
||||
}
|
||||
|
||||
public Notice fromDTO(NoticeDTO dto) {
|
||||
@@ -115,8 +124,40 @@ public class NoticeService {
|
||||
public void deleteNotice(Long id) {
|
||||
if (noticeExists(id)) {
|
||||
noticeRepository.deleteById(id);
|
||||
|
||||
List<String> imagesList = new ArrayList<>();
|
||||
|
||||
try {
|
||||
imagesList = imageService.getImagesList(id);
|
||||
} catch (Exception e) {
|
||||
logger.info("There weren't any images for notice with ID: " + id + ". Skipping deletion of images. Message: " + e.getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
for (String imageName : imagesList) {
|
||||
imageService.deleteImage(uploadDir, imageName);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.info("There were some issues while deleting images for notice with ID: " + id + ". Message: " + e.getMessage());
|
||||
}
|
||||
} else {
|
||||
throw new EntityNotFoundException("Nie znaleziono ogłoszenia o ID: " + id);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isNoticeOwnedByClient(long noticeId, long clientId) {
|
||||
return noticeRepository.existsByIdNoticeAndClientId(noticeId, clientId);
|
||||
}
|
||||
|
||||
public void boostNotice(long noticeId) {
|
||||
Notice notice = noticeRepository.findById(noticeId)
|
||||
.orElseThrow(() -> new EntityNotFoundException("Ogłoszenie o ID " + noticeId + " nie istnieje."));
|
||||
|
||||
notice.setPublishDate(LocalDateTime.now());
|
||||
|
||||
noticeRepository.save(notice);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
package _11.asktpk.artisanconnectbackend.service;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.dto.OrderDTO;
|
||||
import _11.asktpk.artisanconnectbackend.entities.Client;
|
||||
import _11.asktpk.artisanconnectbackend.entities.Notice;
|
||||
import _11.asktpk.artisanconnectbackend.repository.ClientRepository;
|
||||
import _11.asktpk.artisanconnectbackend.repository.NoticeRepository;
|
||||
import _11.asktpk.artisanconnectbackend.repository.OrderRepository;
|
||||
import _11.asktpk.artisanconnectbackend.utils.Enums;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import _11.asktpk.artisanconnectbackend.entities.Order;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Service
|
||||
public class OrderService {
|
||||
|
||||
private final OrderRepository orderRepository;
|
||||
private final ClientRepository clientRepository;
|
||||
private final NoticeRepository noticeRepository;
|
||||
|
||||
@Autowired
|
||||
public OrderService(OrderRepository orderRepository, ClientRepository clientRepository, NoticeRepository noticeRepository) {
|
||||
this.orderRepository = orderRepository;
|
||||
this.clientRepository = clientRepository;
|
||||
this.noticeRepository = noticeRepository;
|
||||
}
|
||||
|
||||
public Order fromDTO(OrderDTO orderDTO) {
|
||||
Order order = new Order();
|
||||
order.setOrderType(orderDTO.getOrderType());
|
||||
order.setStatus(Enums.OrderStatus.PENDING);
|
||||
if(orderDTO.getOrderType() == Enums.OrderType.ACTIVATION){
|
||||
order.setAmount(10.00);
|
||||
}else{
|
||||
order.setAmount(8.00);
|
||||
}
|
||||
|
||||
order.setCreatedAt(LocalDateTime.now()
|
||||
);
|
||||
order.setUpdatedAt(LocalDateTime.now()
|
||||
);
|
||||
|
||||
Client client = clientRepository.findById(orderDTO.getClientId())
|
||||
.orElseThrow(() -> new EntityNotFoundException("Nie znaleziono klienta o ID: " + orderDTO.getClientId()));
|
||||
order.setClient(client);
|
||||
|
||||
Notice notice = noticeRepository.findById(orderDTO.getNoticeId())
|
||||
.orElseThrow(() -> new EntityNotFoundException("Nie znaleziono ogłoszenia o ID: " + orderDTO.getNoticeId()));
|
||||
order.setNotice(notice);
|
||||
|
||||
return order;
|
||||
}
|
||||
|
||||
|
||||
public Long addOrder(OrderDTO orderDTO) {
|
||||
Order order = fromDTO(orderDTO);
|
||||
return orderRepository.save(order).getId();
|
||||
}
|
||||
|
||||
public Long changeOrderStatus(Long id, Enums.OrderStatus status) {
|
||||
Order order = orderRepository.findById(id)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Nie znaleziono zamówienia o ID: " + id));
|
||||
|
||||
order.setStatus(status);
|
||||
|
||||
order = orderRepository.save(order);
|
||||
|
||||
return order.getId();
|
||||
|
||||
}
|
||||
|
||||
public Order getOrderById(Long id) {
|
||||
return orderRepository.findById(id)
|
||||
.orElseThrow(() -> new RuntimeException("Nie znaleziono zamówienia o ID: " + id));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package _11.asktpk.artisanconnectbackend.service;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.dto.OAuthPaymentResponseDTO;
|
||||
import _11.asktpk.artisanconnectbackend.dto.TransactionPaymentRequestDTO;
|
||||
import _11.asktpk.artisanconnectbackend.dto.TransactionPaymentResponseDTO;
|
||||
import _11.asktpk.artisanconnectbackend.entities.Order;
|
||||
import _11.asktpk.artisanconnectbackend.entities.Payment;
|
||||
import _11.asktpk.artisanconnectbackend.repository.PaymentRepository;
|
||||
import _11.asktpk.artisanconnectbackend.utils.Enums;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.reactive.function.BodyInserters;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Service
|
||||
public class PaymentService {
|
||||
private final WebClient webClient;
|
||||
private final String clientId;
|
||||
private final String clientSecret;
|
||||
private final String authUrl;
|
||||
private final String transactionUrl;
|
||||
private final PaymentRepository paymentRepository;
|
||||
|
||||
public PaymentService(
|
||||
WebClient.Builder webClientBuilder,
|
||||
@Value("${tpay.clientId}") String clientId,
|
||||
@Value("${tpay.clientSecret}") String clientSecret,
|
||||
@Value("${tpay.authUrl}") String authUrl,
|
||||
@Value("${tpay.transactionUrl}") String transactionUrl,
|
||||
PaymentRepository paymentRepository
|
||||
) {
|
||||
this.webClient = webClientBuilder.baseUrl(authUrl).build();
|
||||
this.clientId = clientId;
|
||||
this.clientSecret = clientSecret;
|
||||
this.authUrl = authUrl;
|
||||
this.transactionUrl = transactionUrl;
|
||||
this.paymentRepository = paymentRepository;
|
||||
}
|
||||
|
||||
public OAuthPaymentResponseDTO getOAuthToken() {
|
||||
return webClient.post()
|
||||
.uri("")
|
||||
.contentType(MediaType.MULTIPART_FORM_DATA)
|
||||
.header("accept", "application/json")
|
||||
.body(BodyInserters.fromMultipartData("client_id", clientId)
|
||||
.with("client_secret", clientSecret))
|
||||
.retrieve()
|
||||
.bodyToMono(OAuthPaymentResponseDTO.class)
|
||||
.block();
|
||||
}
|
||||
|
||||
public String createTransaction(Order order, String accessToken, TransactionPaymentRequestDTO transactionPaymentRequestDTO) {
|
||||
TransactionPaymentResponseDTO response = webClient.post()
|
||||
.uri(transactionUrl)
|
||||
.header(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.bodyValue(transactionPaymentRequestDTO)
|
||||
.retrieve()
|
||||
.bodyToMono(TransactionPaymentResponseDTO.class)
|
||||
.block();
|
||||
|
||||
if (response != null && "success".equalsIgnoreCase(response.getResult())) {
|
||||
Payment payment = new Payment();
|
||||
payment.setOrder(order);
|
||||
payment.setAmount(response.getAmount());
|
||||
|
||||
payment.setStatus(Enums.PaymentStatus.PENDING);
|
||||
|
||||
payment.setTransactionId(response.getTransactionId());
|
||||
payment.setTransactionPaymentUrl(response.getTransactionPaymentUrl());
|
||||
paymentRepository.save(payment);
|
||||
|
||||
return response.getTransactionPaymentUrl();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -43,4 +43,20 @@ public class Enums {
|
||||
public enum Status {
|
||||
ACTIVE, INACTIVE
|
||||
}
|
||||
|
||||
public enum OrderType {
|
||||
ACTIVATION,
|
||||
BOOST
|
||||
}
|
||||
|
||||
|
||||
public enum OrderStatus {
|
||||
PENDING, COMPLETED, CANCELLED
|
||||
}
|
||||
|
||||
public enum PaymentStatus{
|
||||
PENDING, CORRECT, INCORRECT
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -16,4 +16,10 @@ spring.jpa.hibernate.ddl-auto=create-drop
|
||||
|
||||
file.upload-dir=/Users/andsol/Desktop/uploads
|
||||
spring.servlet.multipart.max-file-size=10MB
|
||||
spring.servlet.multipart.max-request-size=10MB
|
||||
spring.servlet.multipart.max-request-size=10MB
|
||||
|
||||
tpay.clientId = 01JQKC048X62ST9V59HNRSXD92-01JQKC2CQHPYXQFSFX8BKC24BX
|
||||
tpay.clientSecret = 44898642be53381cdcc47f3e44bf5a15e592f5d270fc3a6cf6fb81a8b8ebffb9
|
||||
tpay.authUrl = https://openapi.sandbox.tpay.com/oauth/auth
|
||||
tpay.transactionUrl = https://openapi.sandbox.tpay.com/transactions
|
||||
|
||||
|
||||
@@ -1,33 +1,591 @@
|
||||
package _11.asktpk.artisanconnectbackend;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.dto.CategoriesDTO;
|
||||
import _11.asktpk.artisanconnectbackend.dto.ClientDTO;
|
||||
import _11.asktpk.artisanconnectbackend.dto.NoticeDTO;
|
||||
import _11.asktpk.artisanconnectbackend.dto.WishlistDTO;
|
||||
import _11.asktpk.artisanconnectbackend.entities.Client;
|
||||
import _11.asktpk.artisanconnectbackend.entities.Notice;
|
||||
import _11.asktpk.artisanconnectbackend.entities.Wishlist;
|
||||
import _11.asktpk.artisanconnectbackend.repository.ClientRepository;
|
||||
import _11.asktpk.artisanconnectbackend.repository.NoticeRepository;
|
||||
import _11.asktpk.artisanconnectbackend.repository.WishlistRepository;
|
||||
import _11.asktpk.artisanconnectbackend.service.ClientService;
|
||||
import _11.asktpk.artisanconnectbackend.service.ImageService;
|
||||
import _11.asktpk.artisanconnectbackend.service.NoticeService;
|
||||
import _11.asktpk.artisanconnectbackend.service.WishlistService;
|
||||
import _11.asktpk.artisanconnectbackend.utils.Enums;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.http.*;
|
||||
import _11.asktpk.artisanconnectbackend.entities.Image;
|
||||
import _11.asktpk.artisanconnectbackend.repository.ImageRepository;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@SpringBootTest
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
|
||||
import static _11.asktpk.artisanconnectbackend.utils.Enums.Role.USER;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* Testy dla funkcjonalności klienta w backendzie.
|
||||
*/
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
class ArtisanConnectBackendApplicationTests {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(ArtisanConnectBackendApplicationTests.class);
|
||||
|
||||
// @Test
|
||||
// void testPostgresDatabase() {
|
||||
// postgresDatabase.add(new Notice("Test Notice", "Username", "Test Description"));
|
||||
// Boolean isRecordAvailable = postgresDatabase.get().size() > 0;
|
||||
// if(isRecordAvailable) {
|
||||
// logger.info("The record is available in the database");
|
||||
// } else {
|
||||
// logger.error("The record is not available in the database");
|
||||
// }
|
||||
// assert isRecordAvailable;
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// void getAllNotices() throws IOException {
|
||||
// OkHttpClient client = new OkHttpClient().newBuilder()
|
||||
// .build();
|
||||
// MediaType mediaType = MediaType.parse("text/plain");
|
||||
// Request request = new Request.Builder()
|
||||
// .url("http://localhost:8080/api/v1/notices/all")
|
||||
// .build();
|
||||
// Response response = client.newCall(request).execute();
|
||||
// }
|
||||
}
|
||||
@LocalServerPort
|
||||
private final int port;
|
||||
|
||||
private final ClientService clientService;
|
||||
private final TestRestTemplate restTemplate;
|
||||
|
||||
@Autowired
|
||||
public ArtisanConnectBackendApplicationTests(ClientService clientService, @LocalServerPort int port) {
|
||||
this.clientService = clientService;
|
||||
this.port = port;
|
||||
this.restTemplate = new TestRestTemplate();
|
||||
}
|
||||
|
||||
|
||||
@Nested
|
||||
@DisplayName("Testy jednostkowe ClientService")
|
||||
class ClientServiceTest {
|
||||
|
||||
private final ClientRepository clientRepository;
|
||||
private final ClientService clientService;
|
||||
|
||||
ClientServiceTest() {
|
||||
logger.info("Inicjalizacja mocków dla ClientService");
|
||||
this.clientRepository = mock(ClientRepository.class);
|
||||
this.clientService = new ClientService(clientRepository);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Powinien poprawnie mapować klientów na ClientDTO")
|
||||
void testClientMappingToDTO() {
|
||||
logger.info("Tworzenie danych klientów...");
|
||||
Client client = createTestClient("Jan", "Kowalski");
|
||||
when(clientRepository.findAll()).thenReturn(List.of(client));
|
||||
|
||||
logger.info("Wywołanie metody getAllClients...");
|
||||
List<ClientDTO> clientDTOList = clientService.getAllClients();
|
||||
|
||||
assertThat(clientDTOList).hasSize(1);
|
||||
assertThat(clientDTOList.get(0).getFirstName()).isEqualTo("Jan");
|
||||
verify(clientRepository, times(1)).findAll();
|
||||
logger.info("Test zakończony poprawnie");
|
||||
}
|
||||
|
||||
private Client createTestClient(String firstName, String lastName) {
|
||||
Client client = new Client();
|
||||
client.setFirstName(firstName);
|
||||
client.setLastName(lastName);
|
||||
client.setEmail(firstName.toLowerCase() + "." + lastName.toLowerCase() + "@example.com");
|
||||
client.setRole(USER);
|
||||
return client;
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Testy integracyjne ClientController")
|
||||
class ClientControllerTest {
|
||||
|
||||
private final int port;
|
||||
private final TestRestTemplate restTemplate;
|
||||
private final ClientService clientService;
|
||||
private final NoticeService noticeService;
|
||||
private final NoticeRepository noticeRepository;
|
||||
private final Logger logger = LogManager.getLogger(ClientControllerTest.class);
|
||||
|
||||
@Autowired
|
||||
public ClientControllerTest(
|
||||
@LocalServerPort int port,
|
||||
TestRestTemplate restTemplate,
|
||||
ClientService clientService,
|
||||
NoticeService noticeService,
|
||||
NoticeRepository noticeRepository) {
|
||||
this.port = port;
|
||||
this.restTemplate = restTemplate;
|
||||
this.clientService = clientService;
|
||||
this.noticeService = noticeService;
|
||||
this.noticeRepository = noticeRepository;
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void cleanDatabase() {
|
||||
|
||||
noticeRepository.deleteAll();
|
||||
|
||||
clientService.getAllClients().forEach(client -> {
|
||||
try {
|
||||
clientService.deleteClient(client.getId());
|
||||
} catch (Exception e) {
|
||||
logger.error("Błąd podczas usuwania klienta: {}", e.getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private boolean hasNotices(Long clientId) {
|
||||
return noticeService.getAllNotices().stream()
|
||||
.anyMatch(notice -> notice.getClientId().equals(clientId));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Powinien poprawnie usunąć klienta z powiązanymi ogłoszeniami")
|
||||
void shouldDeleteClientWithNotices() {
|
||||
ClientDTO client = clientService.addClient(createTestDTO("client@example.com", "Jan", "Kowalski"));
|
||||
|
||||
NoticeDTO notice = new NoticeDTO();
|
||||
notice.setClientId(client.getId());
|
||||
notice.setTitle("Test Notice");
|
||||
Long noticeId = noticeService.addNotice(notice);
|
||||
|
||||
ResponseEntity<Void> deleteNoticeResponse = restTemplate.exchange(
|
||||
createURLWithPort("/api/v1/notices/delete/" + noticeId),
|
||||
HttpMethod.DELETE,
|
||||
null,
|
||||
Void.class
|
||||
);
|
||||
assertThat(deleteNoticeResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
|
||||
ResponseEntity<Void> deleteClientResponse = restTemplate.exchange(
|
||||
createURLWithPort("/api/v1/clients/delete/" + client.getId()),
|
||||
HttpMethod.DELETE,
|
||||
null,
|
||||
Void.class
|
||||
);
|
||||
|
||||
assertThat(deleteClientResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
assertThat(clientService.clientExists(client.getId())).isFalse();
|
||||
assertThat(noticeService.noticeExists(noticeId)).isFalse();
|
||||
}
|
||||
@Autowired
|
||||
private ClientRepository clientRepository;
|
||||
|
||||
|
||||
@Test
|
||||
@DisplayName("Powinien zwracać wszystkich klientów")
|
||||
void shouldReturnAllClients() {
|
||||
ClientDTO client1 = clientService.addClient(createTestDTO("client1@example.com", "Anna", "Nowak"));
|
||||
ClientDTO client2 = clientService.addClient(createTestDTO("client2@example.com", "Adam", "Kowalski"));
|
||||
|
||||
ResponseEntity<ClientDTO[]> response = restTemplate.getForEntity(
|
||||
createURLWithPort("/api/v1/clients/get/all"),
|
||||
ClientDTO[].class
|
||||
);
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
assertThat(response.getBody()).isNotNull();
|
||||
assertThat(response.getBody()).hasSize(2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
@DisplayName("Powinien zwrócić błąd przy próbie usunięcia klienta z powiązanymi ogłoszeniami bez kaskady")
|
||||
void shouldFailWhenDeletingClientWithNoticesWithoutCascade() {
|
||||
noticeService.getAllNotices().forEach(n -> noticeService.deleteNotice(n.getNoticeId()));
|
||||
clientService.getAllClients().forEach(c -> clientService.deleteClient(c.getId()));
|
||||
|
||||
ClientDTO client = clientService.addClient(createTestDTO("client@example.com", "Jan", "Kowalski"));
|
||||
|
||||
NoticeDTO notice = new NoticeDTO();
|
||||
notice.setClientId(client.getId());
|
||||
notice.setTitle("Test Notice");
|
||||
noticeService.addNotice(notice);
|
||||
|
||||
try {
|
||||
clientService.deleteClient(client.getId());
|
||||
fail("Powinien zostać rzucony wyjątek DataIntegrityViolationException");
|
||||
} catch (DataIntegrityViolationException e) {
|
||||
// Oczekiwany wyjątek
|
||||
assertThat(e.getMessage()).contains("could not execute statement");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Powinien poprawnie usunąć klienta bez powiązanych ogłoszeń")
|
||||
void shouldDeleteClientWithoutNotices() {
|
||||
ClientDTO client = clientService.addClient(createTestDTO("client@example.com", "Jan", "Kowalski"));
|
||||
|
||||
ResponseEntity<Void> deleteResponse = restTemplate.exchange(
|
||||
createURLWithPort("/api/v1/clients/delete/" + client.getId()),
|
||||
HttpMethod.DELETE,
|
||||
null,
|
||||
Void.class
|
||||
);
|
||||
|
||||
assertThat(deleteResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
assertThat(clientService.clientExists(client.getId())).isFalse();
|
||||
}
|
||||
|
||||
private ClientDTO createTestDTO(String email, String firstName, String lastName) {
|
||||
ClientDTO clientDTO = new ClientDTO();
|
||||
clientDTO.setEmail(email);
|
||||
clientDTO.setFirstName(firstName);
|
||||
clientDTO.setLastName(lastName);
|
||||
clientDTO.setRole(USER);
|
||||
return clientDTO;
|
||||
}
|
||||
|
||||
private String createURLWithPort(String uri) {
|
||||
return "http://localhost:" + port + uri;
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Testy jednostkowe NoticeService")
|
||||
class NoticeServiceUnitTest {
|
||||
|
||||
private final NoticeRepository noticeRepository;
|
||||
private final ClientRepository clientRepository;
|
||||
private final NoticeService noticeService;
|
||||
|
||||
NoticeServiceUnitTest() {
|
||||
this.noticeRepository = mock(NoticeRepository.class);
|
||||
this.clientRepository = mock(ClientRepository.class);
|
||||
this.noticeService = new NoticeService(
|
||||
noticeRepository,
|
||||
clientRepository,
|
||||
null,
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Powinien poprawnie dodać ogłoszenie")
|
||||
void shouldAddNoticeSuccessfully() {
|
||||
Client client = createTestClient("test@example.com", "Anna", "Kowalska");
|
||||
when(clientRepository.findById(1L)).thenReturn(Optional.of(client));
|
||||
|
||||
NoticeDTO noticeDTO = new NoticeDTO();
|
||||
noticeDTO.setClientId(1L);
|
||||
noticeDTO.setTitle("Test Notice");
|
||||
noticeDTO.setDescription("Opis ogłoszenia");
|
||||
noticeDTO.setPrice(100.0);
|
||||
|
||||
Notice notice = new Notice();
|
||||
notice.setIdNotice(1L);
|
||||
|
||||
when(noticeRepository.save(any(Notice.class))).thenReturn(notice);
|
||||
|
||||
Long savedNoticeId = noticeService.addNotice(noticeDTO);
|
||||
|
||||
assertThat(savedNoticeId).isEqualTo(1L);
|
||||
verify(noticeRepository, times(1)).save(any(Notice.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Powinien zwrócić wyjątek, gdy klient dla ogłoszenia nie istnieje")
|
||||
void shouldThrowExceptionWhenClientNotFound() {
|
||||
NoticeDTO noticeDTO = new NoticeDTO();
|
||||
noticeDTO.setClientId(1L);
|
||||
|
||||
when(clientRepository.findById(1L)).thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(EntityNotFoundException.class, () -> noticeService.addNotice(noticeDTO));
|
||||
}
|
||||
|
||||
private Client createTestClient(String email, String firstName, String lastName) {
|
||||
Client client = new Client();
|
||||
client.setId(1L);
|
||||
client.setEmail(email);
|
||||
client.setFirstName(firstName);
|
||||
client.setLastName(lastName);
|
||||
return client;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Nested
|
||||
@DisplayName("Testy integracyjne ImageService")
|
||||
class ImageServiceTest {
|
||||
|
||||
private final ImageRepository imageRepository;
|
||||
private final ImageService imageService;
|
||||
|
||||
ImageServiceTest() throws Exception {
|
||||
this.imageRepository = mock(ImageRepository.class);
|
||||
Constructor<ImageService> constructor = ImageService.class.getDeclaredConstructor(ImageRepository.class);
|
||||
constructor.setAccessible(true);
|
||||
this.imageService = constructor.newInstance(imageRepository);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Powinien poprawnie zapisać obraz w magazynie plików")
|
||||
void shouldSaveImageToStorage() throws IOException {
|
||||
MultipartFile file = mock(MultipartFile.class);
|
||||
when(file.getOriginalFilename()).thenReturn("test.jpg");
|
||||
when(file.getInputStream()).thenReturn(Files.newInputStream(Path.of("src/test/resources/test.jpg")));
|
||||
|
||||
String uploadDirectory = "upload_dir";
|
||||
Path uploadPath = Path.of(uploadDirectory);
|
||||
Files.createDirectories(uploadPath);
|
||||
|
||||
String savedFileName = imageService.saveImageToStorage(uploadDirectory, file);
|
||||
|
||||
assertTrue(savedFileName.contains(".jpg"));
|
||||
assertTrue(Files.exists(uploadPath.resolve(savedFileName)));
|
||||
|
||||
Files.deleteIfExists(uploadPath.resolve(savedFileName));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Powinien poprawnie zapisać nazwę obrazu do bazy danych")
|
||||
void shouldAddImageNameToDB() {
|
||||
String filename = UUID.randomUUID() + "test.jpg";
|
||||
Long noticeId = 1L;
|
||||
|
||||
imageService.addImageNameToDB(filename, noticeId);
|
||||
|
||||
verify(imageRepository, times(1)).save(Mockito.any(Image.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Powinien poprawnie pobrać obraz")
|
||||
void shouldGetImage() throws IOException {
|
||||
Path imagePath = Path.of("src/test/resources/test.jpg");
|
||||
Resource resource = imageService.getImage("src/test/resources", "test.jpg");
|
||||
|
||||
assertNotNull(resource);
|
||||
assertTrue(resource instanceof UrlResource);
|
||||
assertTrue(Files.exists(imagePath));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Powinien zgłosić błąd, gdy obraz nie zostanie znaleziony")
|
||||
void shouldThrowExceptionWhenImageNotFound() {
|
||||
Exception exception = assertThrows(IOException.class, () -> {
|
||||
imageService.getImage("invalid/path", "missing.jpg");
|
||||
});
|
||||
|
||||
assertThat(exception).hasMessageContaining("File not found");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Powinien poprawnie usuwać obraz z magazynu plików i bazy danych")
|
||||
void shouldDeleteImage() throws IOException {
|
||||
Path imagePath = Files.createTempFile("temp-dir", "temp-image.jpg");
|
||||
String imageName = imagePath.getFileName().toString();
|
||||
String imageDirectory = imagePath.getParent().toString();
|
||||
|
||||
Image image = new Image();
|
||||
image.setImageName(imageName);
|
||||
when(imageRepository.existsImageByImageNameEqualsIgnoreCase(imageName)).thenReturn(true);
|
||||
|
||||
imageService.deleteImage(imageDirectory, imageName);
|
||||
|
||||
assertFalse(Files.exists(imagePath));
|
||||
verify(imageRepository, times(1)).deleteByImageNameEquals(imageName);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Powinien poprawnie zwrócić listę nazw obrazów dla podanego ogłoszenia")
|
||||
void shouldGetImagesListForNotice() throws Exception {
|
||||
Long noticeId = 1L;
|
||||
List<Image> images = List.of(
|
||||
createTestImage(1L, noticeId, "image1.jpg"),
|
||||
createTestImage(2L, noticeId, "image2.jpg")
|
||||
);
|
||||
when(imageRepository.findByNoticeId(noticeId)).thenReturn(images);
|
||||
|
||||
List<String> imageNames = imageService.getImagesList(noticeId);
|
||||
|
||||
assertThat(imageNames).hasSize(2);
|
||||
assertThat(imageNames).containsExactly("image1.jpg", "image2.jpg");
|
||||
}
|
||||
|
||||
private Image createTestImage(Long id, Long noticeId, String imageName) {
|
||||
Image image = new Image();
|
||||
image.setId(id);
|
||||
image.setNoticeId(noticeId);
|
||||
image.setImageName(imageName);
|
||||
return image;
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Testy integracyjne WishlistService")
|
||||
class WishlistServiceTest {
|
||||
|
||||
private final WishlistRepository wishlistRepository;
|
||||
private final NoticeService noticeService;
|
||||
private final WishlistService wishlistService;
|
||||
|
||||
WishlistServiceTest() {
|
||||
this.wishlistRepository = mock(WishlistRepository.class);
|
||||
this.noticeService = mock(NoticeService.class);
|
||||
this.wishlistService = new WishlistService(wishlistRepository, noticeService);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Powinien poprawnie zwrócić wishlist dla klienta")
|
||||
void shouldGetWishlistForClient() {
|
||||
Long clientId = 1L;
|
||||
Wishlist wishlist1 = createTestWishlist(1L, clientId, 10L);
|
||||
Wishlist wishlist2 = createTestWishlist(2L, clientId, 20L);
|
||||
|
||||
when(wishlistRepository.findAllByClientId(clientId)).thenReturn(List.of(wishlist1, wishlist2));
|
||||
|
||||
List<WishlistDTO> result = wishlistService.getWishlistForClientId(clientId);
|
||||
|
||||
assertThat(result).hasSize(2);
|
||||
assertThat(result.get(0).getNoticeId()).isEqualTo(10L);
|
||||
verify(wishlistRepository, times(1)).findAllByClientId(clientId);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Powinien poprawnie dodać lub usunąć element z wishlist")
|
||||
void shouldToggleWishlist() {
|
||||
Client client = createTestClient(1L, "test@example.com");
|
||||
Notice notice = createTestNotice(10L);
|
||||
|
||||
// Scenariusz 1: Element istnieje i powinien zostać usunięty
|
||||
when(wishlistRepository.findByClientAndNotice(client, notice)).thenReturn(Optional.of(new Wishlist()));
|
||||
|
||||
boolean removed = wishlistService.toggleWishlist(client, notice);
|
||||
|
||||
assertThat(removed).isFalse();
|
||||
verify(wishlistRepository, times(1)).delete(any(Wishlist.class));
|
||||
|
||||
// Scenariusz 2: Element nie istnieje i powinien zostać dodany
|
||||
when(wishlistRepository.findByClientAndNotice(client, notice)).thenReturn(Optional.empty());
|
||||
|
||||
boolean added = wishlistService.toggleWishlist(client, notice);
|
||||
|
||||
assertThat(added).isTrue();
|
||||
verify(wishlistRepository, times(1)).save(any(Wishlist.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Powinien zwrócić listę ogłoszeń w wishlist klienta")
|
||||
void shouldGetNoticesInWishlist() {
|
||||
Long clientId = 1L;
|
||||
Wishlist wishlist1 = createTestWishlist(1L, clientId, 10L);
|
||||
Wishlist wishlist2 = createTestWishlist(2L, clientId, 20L);
|
||||
|
||||
when(wishlistRepository.findAllByClientId(clientId)).thenReturn(List.of(wishlist1, wishlist2));
|
||||
when(noticeService.getNoticeById(10L)).thenReturn(createNoticeDTO(10L, "Ogłoszenie 1"));
|
||||
when(noticeService.getNoticeById(20L)).thenReturn(createNoticeDTO(20L, "Ogłoszenie 2"));
|
||||
|
||||
List<NoticeDTO> result = wishlistService.getNoticesInWishlist(clientId);
|
||||
|
||||
assertThat(result).hasSize(2);
|
||||
assertThat(result.get(0).getNoticeId()).isEqualTo(10L);
|
||||
assertThat(result.get(1).getNoticeId()).isEqualTo(20L);
|
||||
}
|
||||
|
||||
private Wishlist createTestWishlist(Long id, Long clientId, Long noticeId) {
|
||||
Wishlist wishlist = new Wishlist();
|
||||
wishlist.setId(id);
|
||||
|
||||
Client client = new Client();
|
||||
client.setId(clientId);
|
||||
wishlist.setClient(client);
|
||||
|
||||
Notice notice = new Notice();
|
||||
notice.setIdNotice(noticeId);
|
||||
wishlist.setNotice(notice);
|
||||
|
||||
return wishlist;
|
||||
}
|
||||
|
||||
private Client createTestClient(Long id, String email) {
|
||||
Client client = new Client();
|
||||
client.setId(id);
|
||||
client.setEmail(email);
|
||||
return client;
|
||||
}
|
||||
|
||||
private Notice createTestNotice(Long noticeId) {
|
||||
Notice notice = new Notice();
|
||||
notice.setIdNotice(noticeId);
|
||||
return notice;
|
||||
}
|
||||
|
||||
private NoticeDTO createNoticeDTO(Long noticeId, String title) {
|
||||
NoticeDTO noticeDTO = new NoticeDTO();
|
||||
noticeDTO.setNoticeId(noticeId);
|
||||
noticeDTO.setTitle(title);
|
||||
return noticeDTO;
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Testy dla VariablesController")
|
||||
class VariablesControllerTest {
|
||||
|
||||
private final int port;
|
||||
private final TestRestTemplate restTemplate;
|
||||
|
||||
@Autowired
|
||||
public VariablesControllerTest(@LocalServerPort int port, TestRestTemplate restTemplate) {
|
||||
this.port = port;
|
||||
this.restTemplate = restTemplate;
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Powinien zwrócić kategorie")
|
||||
void shouldGetCategories() {
|
||||
String url = createURLWithPort("/api/v1/vars/categories");
|
||||
|
||||
ResponseEntity<CategoriesDTO[]> response = restTemplate.getForEntity(url, CategoriesDTO[].class);
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
assertThat(response.getBody()).isNotNull().isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Powinien zwrócić statusy")
|
||||
void shouldGetStatuses() {
|
||||
String url = createURLWithPort("/api/v1/vars/statuses");
|
||||
|
||||
ResponseEntity<Enums.Status[]> response = restTemplate.getForEntity(url, Enums.Status[].class);
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
assertThat(response.getBody()).isNotNull().isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Powinien zwrócić role")
|
||||
void shouldGetRoles() {
|
||||
String url = createURLWithPort("/api/v1/vars/roles");
|
||||
|
||||
ResponseEntity<Enums.Role[]> response = restTemplate.getForEntity(url, Enums.Role[].class);
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
assertThat(response.getBody()).isNotNull().isNotEmpty();
|
||||
}
|
||||
|
||||
private String createURLWithPort(String uri) {
|
||||
return "http://localhost:" + port + uri;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
BIN
src/test/resources/test.jpeg
Normal file
BIN
src/test/resources/test.jpeg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 108 KiB |
BIN
src/test/resources/test.jpg
Normal file
BIN
src/test/resources/test.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 62 KiB |
BIN
src/test/resources/test.png
Normal file
BIN
src/test/resources/test.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 435 KiB |
Reference in New Issue
Block a user