Skip to content

Commit

Permalink
Merge pull request #3 from nhkhai/main
Browse files Browse the repository at this point in the history
(SIS-19) & (SIS-23): Added basic CI and basic test cases for testing the CI system.
  • Loading branch information
nhkhai authored Mar 19, 2024
2 parents dbea324 + 434c853 commit 38696c2
Show file tree
Hide file tree
Showing 15 changed files with 348 additions and 81 deletions.
61 changes: 61 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# This workflow will build a Java Spring Boot Application with Maven, and run test cases.
name: Build and Test Java Spring Boot Application

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
run-unit-tests:
runs-on: ubuntu-latest

services:
postgres:
image: postgres:latest

env:
POSTGRES_USER: postgres
# POSTGRES_PASSWORD: postgres
POSTGRES_HOST_AUTH_METHOD: trust
POSTGRES_DB: smart_inventory

# Set health checks to wait until postgres has started.
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
# Maps tcp port 5432 on service container to the host.
- 5432:5432

steps:
- name: Pull repository
uses: actions/[email protected]

- name: Set up JDK
uses: actions/[email protected]
with:
java-version: '21'
distribution: 'temurin'
# cache: maven # Uncomment and configure if needed.

- name: Run unit tests
run: mvn test

# Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive.
# - name: Update dependency graph
# uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6

- name: Build Jar file
run: mvn package -DskipTests # Skip tests since they were already run.

- name: Save Jar file
uses: actions/[email protected]
with:
name: smart-inventory-0.0.1-SNAPSHOT
path: target/smart-inventory-0.0.1-SNAPSHOT.jar
retention-days: 1
49 changes: 29 additions & 20 deletions src/main/java/sg/com/smartinventory/DataLoader.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package sg.com.smartinventory;

import org.springframework.beans.factory.annotation.Autowired;
// import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import jakarta.annotation.PostConstruct;
Expand All @@ -13,44 +13,53 @@

@Component
public class DataLoader {
//Inject all repositories
// Inject all repositories.
private CustomerRepository customerRepository;
private ProductRepository productRepository;
private ReviewRepository reviewRepository;

@Autowired
public DataLoader(CustomerRepository customerRepository, ProductRepository productRepository, ReviewRepository reviewRepository){
// @Autowired
public DataLoader(CustomerRepository customerRepository, ProductRepository productRepository,
ReviewRepository reviewRepository) {
this.customerRepository = customerRepository;
this.productRepository = productRepository;
this.reviewRepository = reviewRepository;
}

@PostConstruct
public void loadData(){
//Clear all data
public void loadData() {
// Clear all data.
customerRepository.deleteAll();
productRepository.deleteAll();
reviewRepository.deleteAll();

//Create fake data
customerRepository.save(new Customer("John", "Doe", "USA", "123 Main St", 12345, 1234567890, "[email protected]", 101));
customerRepository.save(new Customer("Alice", "Smith", "Canada", "456 Maple Ave", 54321, 987654321, "[email protected]", 102));
customerRepository.save(new Customer("Michael", "Johnson", "UK", "789 Oak Rd", 67890, 987612345, "[email protected]", 103));
customerRepository.save(new Customer("Emily", "Brown", "Australia", "321 Elm St", 13579, 456789123, "[email protected]", 104));
customerRepository.save(new Customer("David", "Wilson", "Germany", "654 Pine Rd", 98765, 369852147, "[email protected]", 105));
// Create fake data.
customerRepository
.save(new Customer("John", "Doe", "USA", "123 Main St", 123456, 12345678, "[email protected]", 101));
customerRepository.save(
new Customer("Alice", "Smith", "Canada", "456 Maple Ave", 543210, 98765432, "[email protected]", 102));
customerRepository.save(
new Customer("Michael", "Johnson", "UK", "789 Oak Rd", 567890, 98761234, "[email protected]", 103));
customerRepository.save(
new Customer("Emily", "Brown", "Australia", "321 Elm St", 135790, 45678912, "[email protected]", 104));
customerRepository.save(
new Customer("David", "Wilson", "Germany", "654 Pine Rd", 987655, 36985214, "[email protected]", 105));

productRepository.save(new Product("Electronics", "Smartphone", "High-end smartphone with advanced features", 999.99, 100, "101"));
productRepository.save(new Product("Clothing", "Men's T-Shirt", "Comfortable cotton t-shirt for everyday wear", 29.99, 500, "102"));
productRepository.save(new Product("Home & Kitchen", "Coffee Maker", "Automatic coffee maker with programmable settings", 49.99, 50, "103"));
productRepository.save(new Product("Beauty", "Perfume", "Elegant fragrance with floral and citrus notes", 79.99, 200, "104"));
productRepository.save(new Product("Books", "Science Fiction Novel", "Bestselling sci-fi novel set in a dystopian future", 14.99, 300, "105"));
productRepository.save(
new Product("Electronics", "Smartphone", "High-end smartphone with advanced features", 999.99, 100, "101"));
productRepository.save(
new Product("Clothing", "Men's T-Shirt", "Comfortable cotton t-shirt for everyday wear", 29.99, 500, "102"));
productRepository.save(new Product("Home & Kitchen", "Coffee Maker",
"Automatic coffee maker with programmable settings", 49.99, 50, "103"));
productRepository
.save(new Product("Beauty", "Perfume", "Elegant fragrance with floral and citrus notes", 79.99, 200, "104"));
productRepository.save(new Product("Books", "Science Fiction Novel",
"Bestselling sci-fi novel set in a dystopian future", 14.99, 300, "105"));

reviewRepository.save(new Review("Electronics", "Great smartphone with excellent features.", 5, 101, 201));
reviewRepository.save(new Review("Clothing", "Very comfortable t-shirt, fits perfectly.", 4, 102, 202));
reviewRepository.save(new Review("Home & Kitchen", "Makes delicious coffee, easy to use.", 4, 103, 203));
reviewRepository.save(new Review("Beauty", "Lovely fragrance, long-lasting.", 5, 104, 204));
reviewRepository.save(new Review("Books", "Intriguing plot, couldn't put it down.", 5, 105, 205));

}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

@SpringBootApplication
public class SmartInventoryApplication {

public static void main(String[] args) {
SpringApplication.run(SmartInventoryApplication.class, args);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package sg.com.smartinventory.controllers;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.validation.Valid;
import sg.com.smartinventory.entity.Customer;
import sg.com.smartinventory.services.CustomerService;

@RestController
@RequestMapping("/customers")
public class CustomerController {
private CustomerService customerService;

// @Autowired
public CustomerController(CustomerService customerService) {
this.customerService = customerService;
}

// CREATE.
@PostMapping("")
public ResponseEntity<Customer> createCustomer(@Valid @RequestBody Customer customer) {

// if(bindingResult.hasErrors()) {
// return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
// }

Customer newCustomer = customerService.createCustomer(customer);
return new ResponseEntity<>(newCustomer, HttpStatus.CREATED);
}
}
54 changes: 37 additions & 17 deletions src/main/java/sg/com/smartinventory/entity/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,63 @@
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.validation.constraints.Digits;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Builder
@AllArgsConstructor
@Entity
@Table(name="customer")
@Table(name = "customer")
public class Customer {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id; //PK

@Column(name="first_name")
private Long id; // PK.

@NotBlank(message = "First name is mandatory. ")
@Column(name = "first_name")
private String firstName;
@Column(name="last_name")

@Column(name = "last_name")
private String lastName;
@Column(name="country")

@Column(name = "country")
private String country;
@Column(name="address")

@Column(name = "address")
private String address;
@Column(name="postal_code")

@Digits(fraction = 0, integer = 6, message = "Postal code should be 6 digits. ")
@Column(name = "postal_code")
private int postalCode;
@Column(name="mobile_number")

@Digits(fraction = 0, integer = 8, message = "Mobile no should be 8 digits. ")
@Column(name = "mobile_number")
private int mobileNumber;
@Column(name="email")

@Email(message = "Email should be valid. ")
@Column(name = "email")
private String email;
@Column(name="review_id")
private int reviewId; //FK

//Define Constructor for dataLoader
@Column(name = "review_id")
private int reviewId; // FK.

public Customer() {
}

// Define Constructor for DataLoader.
public Customer(String firstName, String lastName, String country, String address, int postalCode, int mobileNumber,
String email, int reviewId) {
this();

this.firstName = firstName;
this.lastName = lastName;
this.country = country;
Expand All @@ -50,6 +72,4 @@ public Customer(String firstName, String lastName, String country, String addres
this.email = email;
this.reviewId = reviewId;
}


}
}
39 changes: 25 additions & 14 deletions src/main/java/sg/com/smartinventory/entity/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,52 @@
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Entity
@Table(name="product")
@Builder
@AllArgsConstructor
@Entity
@Table(name = "product")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
@Column(name = "id")
private Long id;
@Column(name="category")

@Column(name = "category")
private String category;
@Column(name="name")

@Column(name = "name")
private String name;
@Column(name="description")

@Column(name = "description")
private String description;
@Column(name="price")

@Column(name = "price")
private double price;
@Column(name="stock_quantity")

@Column(name = "stock_quantity")
private int stockQuantity;
@Column(name="review_id")
private String reviewId; //FK

//Define Constructor for dataLoader
@Column(name = "review_id")
private String reviewId; // FK.

public Product() {
}

// Define Constructor for DataLoader.
public Product(String category, String name, String description, double price, int stockQuantity, String reviewId) {
this();

this.category = category;
this.name = name;
this.description = description;
this.price = price;
this.stockQuantity = stockQuantity;
this.reviewId = reviewId;
}


}
}
Loading

0 comments on commit 38696c2

Please sign in to comment.