Skip to content

Commit

Permalink
Created Test class for BytesUtil (#783)
Browse files Browse the repository at this point in the history
* Created test class for IdTypeUtil => IdTypeUtilTest

* Created test class for IdTypeUtil => IdTypeUtilTest

* Created test class for IdTypeUtil => IdTypeUtilTest

* Created test class for BytesUtil => BytesUtilTest

* Created test class for BytesUtil => BytesUtilTest

* Created test class for BytesUtil => BytesUtilTest

* Created test class for BytesUtil => BytesUtilTest

* Created test class for BytesUtil => BytesUtilTest

* Created test class for HotlistScheduledCleanupJob => HotlistScheduledCleanupJobTest

Co-authored-by: Vipul Dhurve <[email protected]>
  • Loading branch information
vipuldhurve02 and vipuldhurve authored Jan 18, 2022
1 parent 6deaa5d commit 2c23fd2
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package io.mosip.authentication.core.util;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.context.WebApplicationContext;


@WebMvcTest
@ContextConfiguration(classes = {TestContext.class, WebApplicationContext.class})
@RunWith(SpringRunner.class)
public class BytesUtilTest {

@InjectMocks
private BytesUtil bytesUtil;

/**
* This class tests the prependZeros method
*/
@Test
public void prependZerosTest(){
byte[] str = {(byte)0xe0, 0x4f, (byte)0xd0,
0x20, (byte)0xea};
int n=3;
//[B@39dec536
byte[] expected = {0, 0, 0, -32, 79, -48, 32, -22 };
Assert.assertArrayEquals(expected, bytesUtil.prependZeros(str, n));
}

/**
* This class tests the getXOR method
*/
@Test
public void getXORTest(){
//length of b > leangth of a
String a = "test", b = "sample";
byte[] expected = {115, 97, 25, 21, 31, 17};
Assert.assertArrayEquals(expected, ReflectionTestUtils.invokeMethod(bytesUtil, "getXOR", a, b));
//length of a > leangth of b
a = "sample";
b = "test";
Assert.assertArrayEquals(expected, ReflectionTestUtils.invokeMethod(bytesUtil, "getXOR", a, b));
// length of a == length of b
byte[] exp = {0, 0, 0, 0, 0, 0 };
b="sample";
Assert.assertArrayEquals(exp, ReflectionTestUtils.invokeMethod(bytesUtil, "getXOR", a, b));
}

/**
* This class tests the getLastBytes method
*/
@Test
public void getLastBytesTest(){
byte[] xorBytes = {(byte)0xe0, 0x4f, (byte)0xd0,
0x20, (byte)0xea};
int lastBytesNum = 1;
byte[] ans = {-22};
Assert.assertArrayEquals(ans, ReflectionTestUtils.invokeMethod(bytesUtil, "getLastBytes", xorBytes, lastBytesNum));
}

/**
* This class tests the main method
*/
@Test
public void mainTest(){
String[]args = new String[10];
ReflectionTestUtils.invokeMethod(bytesUtil, "main", (Object) args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.mosip.authentication.internal.service.batch;

import io.mosip.authentication.common.service.repository.HotlistCacheRepository;
import io.mosip.authentication.common.service.transaction.manager.IdAuthSecurityManager;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.context.WebApplicationContext;

@WebMvcTest
@ContextConfiguration(classes ={TestContext.class, WebApplicationContext.class})
@RunWith(SpringRunner.class)
public class HotlistScheduledCleanupJobTest {

@InjectMocks
private HotlistScheduledCleanupJob hotlistScheduledCleanupJob;

@Mock
private IdAuthSecurityManager securityManager;

@Mock
private HotlistCacheRepository hotlistRepo;

@Test
public void cleanupUnblockedIdsTest(){
hotlistScheduledCleanupJob.cleanupUnblockedIds();

ReflectionTestUtils.setField(hotlistScheduledCleanupJob, "hotlistRepo", null);
hotlistScheduledCleanupJob.cleanupUnblockedIds();
}

@Test
public void cleanupExpiredIds(){
hotlistScheduledCleanupJob.cleanupExpiredIds();

ReflectionTestUtils.setField(hotlistScheduledCleanupJob, "hotlistRepo", null);
hotlistScheduledCleanupJob.cleanupExpiredIds();
}
}

0 comments on commit 2c23fd2

Please sign in to comment.