Skip to content

Commit

Permalink
Merge pull request #4 from neuefische/feat/docker-test
Browse files Browse the repository at this point in the history
Feat/docker test
  • Loading branch information
Olgasyla authored Aug 20, 2024
2 parents 898bdf1 + f91ed61 commit ad346c8
Show file tree
Hide file tree
Showing 21 changed files with 486 additions and 45 deletions.
89 changes: 89 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
name: "grocery app"

on:
push:
branches:
- main

jobs:
build-frontend:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: '20'

- name: Build Frontend
working-directory: frontend
run: |
npm install
npm run build
- uses: actions/upload-artifact@v4
with:
name: frontend-build
path: frontend/dist/

build-backend:
runs-on: ubuntu-latest
needs: build-frontend
steps:
- uses: actions/checkout@v4

- uses: actions/download-artifact@v4
with:
name: frontend-build
path: backend/src/main/resources/static

- name: Set up JDK
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'
cache: 'maven'

- name: Build with maven
run: mvn -B package --file backend/pom.xml

- uses: actions/upload-artifact@v4
with:
name: app.jar
path: backend/target/groceries-app.jar

push-to-docker-hub:
runs-on: ubuntu-latest
needs: build-backend
steps:
- uses: actions/checkout@v4

- uses: actions/download-artifact@v4
with:
name: app.jar
path: backend/target

- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}

- name: Build and push
uses: docker/build-push-action@v5
with:
push: true
tags: ${{ secrets.DOCKERHUB_TAG }}
context: .

deploy:
name: deploy-to-render
runs-on: ubuntu-latest
needs: push-to-docker-hub
environment:
name: team-project
url: https://groceries-app-bsu2.onrender.com/
steps:
- name: Trigger Render.com Deployment
run: |
curl -X POST ${{ secrets.RENDER_DEPLOY }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules/
6 changes: 6 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM --platform=linux/amd64 openjdk:21
EXPOSE 8080
ADD backend/target/groceries-app.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]


3 changes: 2 additions & 1 deletion backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<groupId>org.example</groupId>
<artifactId>backend</artifactId>
<version>0.0.1-SNAPSHOT</version>

<name>backend</name>
<description>backend</description>
<url/>
Expand Down Expand Up @@ -67,8 +68,8 @@
<scope>test</scope>
</dependency>
</dependencies>

<build>
<finalName>groceries-app</finalName>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -16,4 +17,10 @@ public class ProductController {
public List<Product> getAllGroceries() {
return productService.findAllGroceries();
}
@GetMapping("{id}")
public Product getGroceryProductById(@PathVariable String id){
return productService.findGroceriesById(id);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
@Repository
public interface ProductRepository extends MongoRepository<Product, String> {


}
7 changes: 7 additions & 0 deletions backend/src/main/java/org/example/backend/ProductService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.NoSuchElementException;


@RequiredArgsConstructor
Expand All @@ -17,4 +18,10 @@ public List<Product> findAllGroceries(){
}


public Product findGroceriesById(String id) {
return productRepository.findById(id)
.orElseThrow(() -> new NoSuchElementException("Product with id: " + id + " not found!"));
}


}
45 changes: 45 additions & 0 deletions backend/src/main/resources/static/assets/index-W6eCT1eM.js

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions backend/src/main/resources/static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
<script type="module" crossorigin src="/assets/index-W6eCT1eM.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-zl8kUJ8_.css">
</head>
<body>
<div id="root"></div>
</body>
</html>
Expand Down
1 change: 1 addition & 0 deletions backend/src/main/resources/static/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
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.test.annotation.DirtiesContext;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import static org.junit.jupiter.api.Assertions.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

Expand All @@ -21,7 +21,7 @@ class ProductControllerTest {

@Autowired
ProductRepository productRepository;

@DirtiesContext
@Test
void getAllGroceries() throws Exception {
//GIVEN
Expand All @@ -34,4 +34,27 @@ void getAllGroceries() throws Exception {
[]
"""));
}
}

@Test
@DirtiesContext
void getGroceryProductById() throws Exception {
//GIVEN
Product newProduct = new Product("21","apple",24);
productRepository.save(newProduct);
//WHEN
mockMvc.perform(MockMvcRequestBuilders.get("/api/products/21"))
//THEN
//THEN
.andExpect(status().isOk())
.andExpect(content().json("""
{
"id": "21",
"name": "apple",
"amount": 24
}
"""));

}
}


14 changes: 14 additions & 0 deletions backend/src/test/java/org/example/backend/ProductServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
Expand All @@ -26,4 +27,17 @@ void findAllGroceries() {
verify(productRepository).findAll();
assertEquals(products, actual);
}

@Test
void findGroceriesById() {
//GIVEN
String id = "4";
Product product = new Product("4","apple", 7);
when(productRepository.findById(id)).thenReturn(Optional.of(product));
//WHEN
Product actual = productService.findGroceriesById(id);
//THEN
verify(productRepository).findById(id);
assertEquals(product, actual);
}
}
Loading

0 comments on commit ad346c8

Please sign in to comment.