Commit a541f1ef authored by Suresh Kumar's avatar Suresh Kumar

Internee Test

parent 6c965654
# assignments_new
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.shopingCardSystem</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Shoping Card System</description>
<properties>
<java.version>1.8</java.version>
<junit-platform.version>5.5.2</junit-platform.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.shopingCardSystem.Controller;
import java.util.List;
import com.shopingCardSystem.Model.Cart;
import org.springframework.web.bind.annotation.*;
import com.shopingCardSystem.Service.CartServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
@RestController
@RequestMapping("/cartRecords")
public class CartController {
@Autowired
private CartServiceImpl cartService;
@GetMapping("/carts")
public List<Cart> getAllCart(){
return cartService.getAllItems();
}
@GetMapping("/{cartId}")
public Cart getCardById(@PathVariable("cartId") int cartId){
Cart cart = cartService.getCartgetById(cartId);
return cart;
}
@PostMapping("/cart")
public Cart addCart(@RequestBody Cart cart){
cartService.addCart(cart);
return cart;
}
@PutMapping("/{cartId}")
public Cart updateCardById(@RequestBody Cart cart, @PathVariable("cartId") int cartId){
cartService.updateCartById(cart, cartId);
return cart;
}
@DeleteMapping("/{cartId}")
public void deleteCard(@PathVariable("cartId") int cardId){
cartService.deleteCardById(cardId);
}
}
\ No newline at end of file
package com.shopingCardSystem.Controller;
import java.util.List;
import com.shopingCardSystem.Model.Item;
import com.shopingCardSystem.Service.ItemServiceImpl;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
@RestController
@RequestMapping("/itemRecords")
public class ItemController {
@Autowired
private ItemServiceImpl itemService;
@GetMapping("/items")
public List<Item> getAllItems(){
return itemService.getAllItems();
}
@GetMapping("/{itemId}")
public Item getItemById(@PathVariable("itemId") int itemId){
Item item = this.itemService.getItemById(itemId);
return item;
}
@PostMapping("/item")
public Item addItem(@RequestBody Item item ){
this.itemService.addItem(item);
return item;
}
@PutMapping("{itemId}")
public Item updateItemById(@RequestBody Item item , @PathVariable("itemId") int itemId){
itemService.updateItemById(item, itemId);
return item;
}
@DeleteMapping("{itemId}")
public void deleteById(@PathVariable("itemId") int itemId){
this.itemService.deleteItemById(itemId);
}
}
package com.shopingCardSystem;
import com.shopingCardSystem.Model.Cart;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
System.out.println("Project Started...........!!!");
}
}
package com.shopingCardSystem.Model;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import java.util.List;
import javax.persistence.*;
@Getter
@Setter
@ToString
@NoArgsConstructor
@Entity
@Table(name = "cart")
public class Cart {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int cartId;
private int itemCount;
private double grandTotal;
private double totalDiscount;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="cartId")
private List<Item> items;
public Cart(int cartId, int itemCount, double grandTotal, double totalDiscount){
this.cartId =cartId;
this.itemCount = itemCount;
this.grandTotal = grandTotal;
this.totalDiscount = totalDiscount;
}
}
package com.shopingCardSystem.Model;
import lombok.*;
import javax.persistence.*;
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "items")
public class Item {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int itemId;
private double discount;
private double price;
private int quantity;
private long upc;
}
package com.shopingCardSystem.Repository;
import com.shopingCardSystem.Model.Cart;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CartRepository extends CrudRepository<Cart, Integer> {
Cart findById(int cartId);
}
package com.shopingCardSystem.Repository;
import com.shopingCardSystem.Model.Item;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ItemReository extends CrudRepository<Item , Integer> {
Item findById(int itemId);
}
package com.shopingCardSystem.Service;
import com.shopingCardSystem.Model.Cart;
import java.util.List;
public interface CartService {
public List <Cart> getAllItems();
public Cart getCartgetById(int cartId);
public Cart addCart(Cart cart);
public Cart updateCartById(Cart cart , int cartId);
public void deleteCardById(int cartId);
}
package com.shopingCardSystem.Service;
import java.util.List;
import com.shopingCardSystem.Model.Cart;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.shopingCardSystem.Repository.CartRepository;
import org.springframework.beans.factory.annotation.Autowired;
@Component
@Service
public class CartServiceImpl implements CartService{
@Autowired
private CartRepository cartRepository;
@Override
public List<Cart> getAllItems() {
List <Cart> list = (List<Cart>) this.cartRepository.findAll();
return list;
}
@Override
public Cart getCartgetById(int cartId) {
Cart cart = null;
cart= cartRepository.findById(cartId);
return cart;
}
@Override
public Cart addCart(Cart cart) {
cartRepository.save(cart);
return cart;
}
@Override
public Cart updateCartById(Cart cart, int cartId) {
cart.setCartId(cartId);
cartRepository.save(cart);
return cart;
}
@Override
public void deleteCardById(int cartId) {
cartRepository.deleteById(cartId);
}
}
package com.shopingCardSystem.Service;
import java.util.List;
import com.shopingCardSystem.Model.Item;
public interface ItemService {
public List<Item> getAllItems();
public Item getItemById(int itemId);
public Item addItem(Item item);
public Item updateItemById(Item item , int itemId);
public void deleteItemById(int itemId);
}
package com.shopingCardSystem.Service;
import com.shopingCardSystem.Model.Item;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.shopingCardSystem.Repository.ItemReository;
@Service
public class ItemServiceImpl implements ItemService{
@Autowired
private ItemReository itemReository;
@Override
public List<Item> getAllItems() {
List<Item> list = (List<Item>) itemReository.findAll();
return list;
}
@Override
public Item getItemById(int itemId) {
Item item = null;
item = itemReository.findById(itemId);
return item;
}
// @Override
public Item addItem(Item item) {
itemReository.save(item);
return item;
}
@Override
public Item updateItemById(Item item, int itemId) {
item.setItemId(itemId);
itemReository.save(item);
return item;
}
@Override
public void deleteItemById(int itemId) {
itemReository.deleteById(itemId);
}
}
server.port=8086
spring.datasource.name=InterneeTest
spring.datasource.url=jdbc:mysql://localhost:3306/InterneeTest?characterEncoding=utf8
spring.datasource.username= root
spring.datasource.password=nisum123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL55Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
package com.shopingCardSystem;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class DemoApplicationTests {
@Test
void contextLoads() {
}
}
package com.shopingCardSystem.Service;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.InjectMocks;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import java.util.stream.Collectors;
import static org.mockito.Mockito.*;
import com.shopingCardSystem.Model.Cart;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import com.shopingCardSystem.Repository.CartRepository;
@ExtendWith(MockitoExtension.class)
class CartServiceImplTest {
@InjectMocks private CartServiceImpl cartService;
@Mock private CartRepository cartRepository;
@Test
public void getAllItems() {
when(cartRepository.findAll()).thenReturn(Stream.of(new Cart(1, 255, 54, 50), new Cart(2, 30, 20, 20)).collect(Collectors.toList()));
assertEquals(2, cartService.getAllItems().size());
}
@Test
void getCartgetById() {
Cart cart = new Cart(3, 222, 40, 15);
when(cartRepository.findById(3)).thenReturn(cart);
Cart cartResult = cartService.getCartgetById(3);
assertEquals(cart, cartResult);
}
@Test
void addCart() {
Cart cart = new Cart(3, 222, 40, 15);
when(cartRepository.save(cart)).thenReturn(cart);
Cart cartResult = cartService.addCart(cart);
assertEquals(cart, cartResult);
}
@Test
void updateCartById() {
Cart cart = new Cart(4, 332, 50, 45);
Mockito.when(cartRepository.save(cart)).thenReturn(null);
cart.setItemCount(443);
Mockito.when(cartRepository.save(cart)).thenReturn(cart);
assertEquals(cart, cartService.updateCartById(cart, cart.getCartId()));
}
@Test
void deleteCardById() {
Cart cart = new Cart(5, 225, 60, 30);
cartService.deleteCardById(cart.getCartId());
verify(cartRepository, times(1)).deleteById(5);
}
}
\ No newline at end of file
package com.shopingCardSystem.Service;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.InjectMocks;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import java.util.stream.Collectors;
import static org.mockito.Mockito.*;
import com.shopingCardSystem.Model.Item;
import static org.junit.jupiter.api.Assertions.*;
import org.mockito.junit.jupiter.MockitoExtension;
import org.junit.jupiter.api.extension.ExtendWith;
import com.shopingCardSystem.Repository.ItemReository;
@ExtendWith(MockitoExtension.class)
class ItemServiceImplTest {
@InjectMocks private ItemServiceImpl itemService;
@Mock private ItemReository itemReository;
@Test
public void getAllItems() {
when(itemReository.findAll()).thenReturn(Stream.of(new Item(1, 5, 333, 500, 20), new Item(2, 3, 343, 1000, 30)).collect(Collectors.toList()));
assertEquals(2, itemService.getAllItems().size());
}
@Test
void getItemById() {
Item item = new Item(3, 4, 555, 1000, 40);
when(itemReository.findById(3)).thenReturn(item);
Item itemResult = itemService.getItemById(3);
assertEquals(item, itemResult);
}
@Test
void addItem() {
Item item1 = new Item(5, 5, 2000, 666, 20);
when(itemReository.save(item1)).thenReturn(item1);
Item itemResult = itemService.addItem(item1);
assertEquals(item1, itemResult);
}
@Test
void updateItemById() {
Item item2 = new Item(5, 5, 2000, 666, 20);
Mockito.when(itemReository.save(item2)).thenReturn(null);
item2.setDiscount(10);
Mockito.when(itemReository.save(item2)).thenReturn(item2);
assertEquals(item2, itemService.updateItemById(item2, item2.getItemId()));
}
@Test
void deleteItemById() {
Item item = new Item(6, 5, 6000, 100, 25);
itemService.deleteItemById(item.getItemId());
verify(itemReository, times(1)).deleteById(6);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment