Skip to content

Commit

Permalink
Merge pull request #17 from ADORSYS-GIS/feat/unit-test-for-otpservice
Browse files Browse the repository at this point in the history
chore:write unit test for compute hash method
  • Loading branch information
Arielpetit authored Jan 9, 2025
2 parents 8d22973 + ad33b7b commit 6f57704
Showing 1 changed file with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package com.adorsys.webank.serviceimpl;
import static org.junit.jupiter.api.Assertions.*;

import com.adorsys.webank.exceptions.HashComputationException;
import org.junit.jupiter.api.Test;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class OtpServiceTest {


OtpServiceImpl otpServiceImpl = new OtpServiceImpl();


@Test
void generateFourDigitOtp() {
String otp = otpServiceImpl.generateOtp();
Expand All @@ -18,4 +25,33 @@ void generateFourDigitOtp() {
assert Integer.parseInt(otp) >= 1000 && Integer.parseInt(otp) <= 9999 : "Otp should be between 1000 and 9999";
}

@Test
void testComputeHashWithValidInputs() throws NoSuchAlgorithmException {
String otp = "1234";
String phoneNumber = "+237654066316";
String publicKey = "public-key-123";
String salt = "unique-salt";
// Expected hash computation
String input = otp + phoneNumber + publicKey + salt;
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hashBytes = digest.digest(input.getBytes(StandardCharsets.UTF_8));
String expectedHash = Base64.getEncoder().encodeToString(hashBytes);
// Compute hash using the method
String actualHash = otpServiceImpl.computeHash(otp, phoneNumber, publicKey, salt);
assertNotNull(actualHash, "Hash should not be null");
assertEquals(expectedHash, actualHash, "Hashes should match");
}

@Test
void testComputeHashWithEmptyInputs() {
String otp = "";
String phoneNumber = "";
String publicKey = "";
String salt = "";
String actualHash = otpServiceImpl.computeHash(otp, phoneNumber, publicKey, salt);
assertNotNull(actualHash, "Hash should not be null");
assertFalse(actualHash.isEmpty(), "Hash should not be empty");
}


}

0 comments on commit 6f57704

Please sign in to comment.