Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package io.mosip.commons.packet.test.util;

import io.mosip.commons.packet.audit.AuditLogEntry;
import io.mosip.commons.packet.constants.PacketManagerConstants;
import io.mosip.commons.packet.facade.PacketReader;
import io.mosip.commons.packet.keeper.PacketKeeper;
import io.mosip.commons.packet.util.IdSchemaUtils;
import io.mosip.commons.packet.util.PacketValidator;
import io.mosip.kernel.core.idobjectvalidator.spi.IdObjectValidator;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.core.env.Environment;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class PacketValidatorTest {

@InjectMocks
private PacketValidator packetValidator;

@Mock
private PacketReader reader;

@Mock
private Environment env;

@Mock
private ObjectMapper mapper;

@Mock
private PacketKeeper packetKeeper;

@Mock
private IdObjectValidator idObjectValidator;

@Mock
private IdSchemaUtils idSchemaUtils;

@Mock
private AuditLogEntry auditLogEntry;

@Before
public void setup() {
// env.getProperty(...) returns null
// → validateSchema() returns false
when(env.getProperty(anyString())).thenReturn(null);
}

@Test
public void validateReturnsFalseWhenSchemaValidationFails() throws Exception {
// Arrange: prevent NPE
when(idSchemaUtils.getIdschemaVersionFromMappingJson()).thenReturn("schemaVersion");
when(reader.getField(any(), any(), any(), any(), anyBoolean())).thenReturn("1.0");

// env property null -> schema validation returns false
when(env.getProperty(anyString())).thenReturn(null);

// Act
boolean result = packetValidator.validate("123", "source", "process");

// Assert
assertFalse(result);

verify(auditLogEntry)
.addAudit(contains("failed"), any(), any(), any(), any(), any(), eq("123"));

verify(packetKeeper, never()).getPacket(any());
}

@Test
public void validateReturnsTrueWhenSchemaPassesAndFileValidationPasses() throws Exception {
// ---- prevent earlier NPEs ----
when(idSchemaUtils.getIdschemaVersionFromMappingJson()).thenReturn("schemaVersion");
when(reader.getField(any(), any(), any(), any(), anyBoolean())).thenReturn("1.0");

Map<String, String> fieldsMap = new HashMap<>();
fieldsMap.put(PacketManagerConstants.IDSCHEMA_VERSION, "1.0");

when(idSchemaUtils.getDefaultFields(anyDouble())).thenReturn(new ArrayList<>());
when(reader.getFields(any(), any(), any(), any(), anyBoolean())).thenReturn(fieldsMap);

when(env.getProperty(anyString())).thenReturn("field1,field2");
when(idObjectValidator.validateIdObject(any(), any(), any())).thenReturn(true);

// ---- spy only public method ----
PacketValidator spyValidator = spy(packetValidator);
doReturn(true)
.when(spyValidator)
.fileAndChecksumValidation(any(), any(), any());

// ---- act ----
boolean result = spyValidator.validate("123", "source", "process");

// ---- assert ----
assertTrue(result);
}

@Test
public void validateReturnsFalseWhenFileValidationFails() throws Exception {

// ---- Schema validation MUST pass ----
when(idSchemaUtils.getIdschemaVersionFromMappingJson())
.thenReturn(PacketManagerConstants.IDSCHEMA_VERSION);

when(reader.getField(any(), any(), any(), any(), anyBoolean()))
.thenReturn("1.0");

Map<String, String> fieldsMap = new HashMap<>();
fieldsMap.put(PacketManagerConstants.IDSCHEMA_VERSION, "1.0");

when(idSchemaUtils.getDefaultFields(anyDouble()))
.thenReturn(new ArrayList<>());

when(reader.getFields(any(), any(), any(), any(), anyBoolean()))
.thenReturn(fieldsMap);

when(env.getProperty(anyString()))
.thenReturn("field1,field2");

when(idObjectValidator.validateIdObject(any(), any(), any()))
.thenReturn(true);

// ---- Force file validation to fail ----
PacketValidator spyValidator = spy(packetValidator);
doReturn(false)
.when(spyValidator)
.fileAndChecksumValidation(any(), any(), any());

// ---- Act ----
boolean result = spyValidator.validate("123", "source", "process");

// ---- Assert ----
assertFalse(result);
}
Comment on lines +110 to +146
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Inconsistent audit log verification when file validation fails.

The test stubs fileAndChecksumValidation to return false (line 102) and correctly asserts the result is false (line 110). However, the audit log verification at line 112 checks for contains("successful"), which is inconsistent with a failed validation.

If file validation fails, the audit should log a failure. This appears to be a copy-paste error from the previous test method.

🔎 Proposed fix
         // Assert
         assertFalse(result);
         verify(auditLogEntry)
-                .addAudit(contains("successful"), any(), any(), any(), any(), any(), eq("123"));
+                .addAudit(contains("failed"), any(), any(), any(), any(), any(), eq("123"));
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
@Test
public void validateReturnsFalseWhenFileValidationFails() throws Exception {
// Arrange
doReturn(true)
.when(spyValidator)
.validateSchema(anyString(), anyString(), anyString());
doReturn(false)
.when(spyValidator)
.fileAndChecksumValidation(anyString(), anyString(), anyString());
// Act
boolean result = spyValidator.validate("123", "source", "process");
// Assert
assertFalse(result);
verify(auditLogEntry)
.addAudit(contains("successful"), any(), any(), any(), any(), any(), eq("123"));
}
@Test
public void validateReturnsFalseWhenFileValidationFails() throws Exception {
// Arrange
doReturn(true)
.when(spyValidator)
.validateSchema(anyString(), anyString(), anyString());
doReturn(false)
.when(spyValidator)
.fileAndChecksumValidation(anyString(), anyString(), anyString());
// Act
boolean result = spyValidator.validate("123", "source", "process");
// Assert
assertFalse(result);
verify(auditLogEntry)
.addAudit(contains("failed"), any(), any(), any(), any(), any(), eq("123"));
}
🧰 Tools
🪛 GitHub Actions: Maven Package upon a push

[error] 100-100: validateSchema(java.lang.String,java.lang.String,java.lang.String) has private access in io.mosip.commons.packet.util.PacketValidator

🤖 Prompt for AI Agents
In
@commons-packet/commons-packet-manager/src/test/java/io/mosip/commons/packet/test/util/PacketValidatorTest.java
around lines 95-113, The audit verification in the test
validateReturnsFalseWhenFileValidationFails is wrong: when
fileAndChecksumValidation on spyValidator is stubbed to return false and
validate("123", "source", "process") asserts false, update the
verify(auditLogEntry).addAudit(...) assertion to expect a failure message (e.g.,
use contains("failed") or contains("unsuccessful") instead of
contains("successful")) so the auditLogEntry.addAudit verification matches the
failed validation path.


}
Loading