Skip to content

Commit

Permalink
Working on getting tests to pass
Browse files Browse the repository at this point in the history
  • Loading branch information
danvega committed Aug 20, 2024
1 parent b0875cf commit 58a1aa5
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 21 deletions.
7 changes: 5 additions & 2 deletions src/main/java/com/wafflecorp/store/order/OrderController.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ public OrderController(OrderRepository repository) {
this.repository = repository;
}

/*
You could ask for only the authenticated principal if spring security is on the classpath
allOrders(Principal principal)
*/
@QueryMapping
public List<Order> allOrders(Principal principal) {
log.info("Authenticated Principal: " + principal);
public List<Order> allOrders() {
return repository.findAll();
}

Expand Down
22 changes: 13 additions & 9 deletions src/main/java/com/wafflecorp/store/product/Product.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.wafflecorp.store.product;

import com.wafflecorp.store.order.Order;
import jakarta.persistence.*;

import java.util.HashSet;
import java.util.Set;

@Entity
public class Product {

Expand All @@ -11,8 +15,8 @@ public class Product {
private String title;
@Column(name = "description")
private String desc;
// @OneToMany(mappedBy = "product")
// private Set<Order> orders = new HashSet<>();
@OneToMany(mappedBy = "product")
private Set<Order> orders = new HashSet<>();

public Product() {}

Expand Down Expand Up @@ -51,13 +55,13 @@ public void setDesc(String desc) {
this.desc = desc;
}

// public Set<Order> getOrders() {
// return orders;
// }
//
// public void setOrders(Set<Order> orders) {
// this.orders = orders;
// }
public Set<Order> getOrders() {
return orders;
}

public void setOrders(Set<Order> orders) {
this.orders = orders;
}

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ public class ProductController {

private final ProductRepository repository;
private final OrderRepository orderRepository;
private final ProductService productService;

public ProductController(ProductRepository repository, OrderRepository orderRepository) {
public ProductController(ProductRepository repository, OrderRepository orderRepository, ProductService productService) {
this.repository = repository;
this.orderRepository = orderRepository;
this.productService = productService;
}

@QueryMapping
public List<Product> allProducts() {
return repository.findAll();
}



@QueryMapping
public Optional<Product> getProduct(@Argument Integer id) {
return Optional.ofNullable(repository.findById(id).orElseThrow(ProductNotFoundException::new));
Expand All @@ -36,10 +36,9 @@ public Optional<Product> getProduct(@Argument Integer id) {
@Secured("ROLE_ADMIN")
@MutationMapping
public Product createProduct(@Argument ProductInput productInput) {
return repository.save(new Product(productInput.title(), productInput.desc()));
return productService.create(new Product(productInput.title(), productInput.desc()));
}


// @SchemaMapping
// public List<Order> orders(Product product) {
// return orderRepository.findAllByProductId(product.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ public class ProductService {

private final ProductRepository productRepository;

public ProductService(ProductRepository productRepository) {
ProductService(ProductRepository productRepository) {
this.productRepository = productRepository;
}

@PreAuthorize("hasRole('ADMIN')")
public Product create(Product product) {
Product create(Product product) {
return productRepository.save(product);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wafflecorp.store.order.OrderRepository;
import com.wafflecorp.store.product.Product;
import com.wafflecorp.store.product.ProductController;
import com.wafflecorp.store.product.ProductRepository;
import com.wafflecorp.store.product.ProductService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.graphql.GraphQlTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.graphql.test.tester.GraphQlTester;

import java.io.IOException;
Expand All @@ -25,16 +29,26 @@
class ProductControllerTest {

@Autowired
private GraphQlTester graphQlTester;
GraphQlTester graphQlTester;

@MockBean
ProductRepository productRepository;

@MockBean
private ProductRepository productRepository;
private List<Product> products = new ArrayList<>();
OrderRepository orderRepository;

@MockBean
ProductService productService;

List<Product> products = new ArrayList<>();


@Test
public void contextLoads() {
assertNotNull(graphQlTester);
assertNotNull(productRepository);
assertNotNull(orderRepository);
assertNotNull(productService);
}

@BeforeEach
Expand Down

0 comments on commit 58a1aa5

Please sign in to comment.