Last active
October 27, 2022 02:19
-
-
Save ekosutrisno/34606dc2093872a14ad1c7afcc3cbf35 to your computer and use it in GitHub Desktop.
Quarkus Integration Test Sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.briix.api.admin; | |
import com.briix.model.FixedIncomeBank; | |
import com.briix.model.Media; | |
import com.briix.model.enumeration.BankStatus; | |
import com.briix.model.enumeration.MediaKind; | |
import com.fasterxml.jackson.core.JsonProcessingException; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import io.quarkus.test.junit.QuarkusTest; | |
import io.restassured.http.ContentType; | |
import io.restassured.response.ValidatableResponse; | |
import org.junit.jupiter.api.*; | |
import javax.inject.Inject; | |
import javax.ws.rs.core.MediaType; | |
import javax.ws.rs.core.Response; | |
import java.io.File; | |
import java.util.List; | |
import static io.restassured.RestAssured.given; | |
import static org.hamcrest.Matchers.equalTo; | |
import static org.hamcrest.Matchers.hasItems; | |
@QuarkusTest | |
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) | |
class FixedIncomeBankAdminApiV1Test { | |
String token = "Bearer {JWT_TOKEN_HERE}"; | |
@Inject | |
ObjectMapper mapper; | |
@Test | |
@Order(0) | |
@DisplayName("IT: Can Get List Of Bank") | |
void list() { | |
// Given | |
var logo = new Media(MediaKind.PNG, "Arthagraha.png", "images/2KSSBDHiIJo1lnGES1nMku.png"); | |
ValidatableResponse response = given() | |
.header("Authorization", token) | |
.when().get("/api/admin/v1/fixed-income-banks") | |
.then() | |
.statusCode(200) | |
.body("id", hasItems(-2)) | |
.body("code", hasItems("ARTHA")) | |
.body("name", hasItems("ARTHA")) | |
.body("description", hasItems("BANK ARTHA GRAHA INTERNASIONAL")) | |
.body("status", hasItems(200)) | |
.body("logo[1].filename", equalTo(logo.getFilename())) | |
.statusCode(Response.Status.OK.getStatusCode()); | |
} | |
@Test | |
@Order(1) | |
@DisplayName("IT: Can Get Single Bank") | |
void get() { | |
var fixCode = "BCA"; | |
var fixId = -3; | |
given() | |
.header("Authorization", token) | |
.when() | |
.get("/api/admin/v1/fixed-income-banks/{code}", fixCode) | |
.then() | |
.body("id", equalTo(fixId)) | |
.body("code", equalTo("BCA")) | |
.body("name", equalTo("BCA")) | |
.body("logo.filename", equalTo("BCA.png")) | |
.body("description", equalTo("BANK CENTRAL ASIA")) | |
.body("status", equalTo(200)) | |
.statusCode(Response.Status.OK.getStatusCode()); | |
} | |
@Test | |
@Order(2) | |
@DisplayName("IT: Can Add New Bank") | |
void add() throws JsonProcessingException { | |
// Given Logo | |
var logo = new Media(MediaKind.PNG, "Arthagraha.png", "images/2KSSBDHiIJo1lnGES1nMku.png"); | |
// Given FixedIncomeBank | |
var fixedIncomeBank = new FixedIncomeBank(); | |
fixedIncomeBank.setCode("JTRUST"); | |
fixedIncomeBank.setName("JTRUST"); | |
fixedIncomeBank.setDescription("BANK JTRUST INDONESIA"); | |
fixedIncomeBank.setLogo(logo); | |
fixedIncomeBank.setStatus(BankStatus.DRAFT); | |
// Then | |
given() | |
.header("Authorization", token) | |
.contentType(MediaType.APPLICATION_JSON) | |
.body(mapper.writeValueAsString(List.of(fixedIncomeBank))) | |
.when() | |
.post("/api/admin/v1/fixed-income-banks") | |
.then() | |
.body("id", equalTo(List.of(1))) | |
.statusCode(Response.Status.OK.getStatusCode()); | |
} | |
@Test | |
@Order(3) | |
@DisplayName("IT: Can Update Bank") | |
void update() throws JsonProcessingException { | |
// Given FixedIncomeBank | |
var fixedIncomeBank = new FixedIncomeBank(); | |
fixedIncomeBank.setCode("ARTHA"); | |
fixedIncomeBank.setName("ARTHA BANK"); | |
fixedIncomeBank.setDescription("BANK ARTHA GRAHA INTERNASIONAL UPDATED"); | |
fixedIncomeBank.setStatus(BankStatus.ACTIVE); | |
given() | |
.header("Authorization", token) | |
.contentType(MediaType.APPLICATION_JSON) | |
.body(mapper.writeValueAsString(List.of(fixedIncomeBank))) | |
.when() | |
.put("/api/admin/v1/fixed-income-banks") | |
.then() | |
.statusCode(Response.Status.OK.getStatusCode()) | |
.body("find { it.code == 'ARTHA' }.id", equalTo(-2)) | |
.body("find { it.code == 'ARTHA' }.name", equalTo(fixedIncomeBank.getName())) | |
.body("find { it.code == 'ARTHA' }.description", equalTo(fixedIncomeBank.getDescription())) | |
.body("find { it.code == 'ARTHA' }.status", equalTo(BankStatus.ACTIVE.getCode())); | |
} | |
@Test | |
@Order(4) | |
@DisplayName("IT: Can Delete Bank & Verify") | |
void delete() throws JsonProcessingException { | |
// Given FixedIncomeBank | |
var fixedIncomeBank = new FixedIncomeBank(); | |
fixedIncomeBank.setCode("BCA"); | |
// Delete and Make Sure Data Deleted with return status NO_CONTENT | |
given() | |
.header("Authorization", token) | |
.contentType(MediaType.APPLICATION_JSON) | |
.body(mapper.writeValueAsString(List.of(fixedIncomeBank))) | |
.when() | |
.delete("/api/admin/v1/fixed-income-banks") | |
.then() | |
.statusCode(Response.Status.NO_CONTENT.getStatusCode()); | |
// Validating data should not exist with return status NO_CONTENT | |
given() | |
.header("Authorization", token) | |
.when() | |
.get("/api/admin/v1/fixed-income-banks/{code}", fixedIncomeBank.getCode()) | |
.then() | |
.statusCode(422); | |
} | |
@Test | |
@Order(5) | |
@DisplayName("IT: Can Add Bank Logo") | |
@Disabled | |
void addLogo() { | |
given() | |
.header("Authorization", token) | |
.multiPart("file", new File("/Users/sds.mac.007/Downloads/bts/BTS.jpg")) | |
.accept(ContentType.MULTIPART) | |
.when() | |
.post("/api/admin/v1/fixed-income-banks/-3/logo") | |
.then() | |
.statusCode(200) | |
.body("success", equalTo(true)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Compressor from "compressorjs"; | |
import { ref } from "vue"; | |
/** | |
* Format Size file function | |
* By. Eko Sutrisno | |
*/ | |
export const formatBytes = (bytes: number, decimals = 2) => { | |
if (bytes === 0) return "0 Bytes"; | |
const k = 1024; | |
const dm = decimals < 0 ? 0 : decimals; | |
const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; | |
const i = Math.floor(Math.log(bytes) / Math.log(k)); | |
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i]; | |
}; | |
/** | |
* @param {File[]} rawFiles | |
* This compossable function will compress and reduce the size of image when selected from local. | |
* and then will sending to the API with size optimized images. | |
* By. Eko Sutrisno | |
*/ | |
export const compressImages = async (rawFiles: File[]) => { | |
const filesPromises = ref<File[]>([]); | |
const promises: Promise<void>[] = []; | |
for (let file of rawFiles) { | |
promises.push(new Promise(function (resolve, reject) { | |
new Compressor(file, { | |
quality: 0.6, | |
success(result) { | |
const finalFile = new File([result], file.name, { type: result.type }); | |
console.log(`${file.name} => [Original: ${formatBytes(file.size)}, Optimized: ${formatBytes(finalFile.size)},]`); | |
filesPromises.value.push(finalFile) | |
resolve() | |
}, | |
error(err) { | |
console.log(err.message) | |
reject() | |
}, | |
}) | |
})) | |
} | |
await Promise.all(promises); | |
return filesPromises; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment