Skip to content
Open
Show file tree
Hide file tree
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
Expand Up @@ -368,9 +368,7 @@ else if (er.getPersonB().equals(session.getSubmissionActions().getCurrentPerson(
rel.setPersonA(relatedPerson);
}
rel.setRelationshipType(r);

Context.getPersonService().saveRelationship(rel);
//session.getSubmissionActions().getRelationshipsToCreate().add(rel);
session.getSubmissionActions().getRelationshipsToCreate().add(rel);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public String generateEditModeHtml(FormEntryContext context, String fieldName, C
public String generateHtml(FormEntryContext context) {
if (context.getMode() == Mode.VIEW) {
if (initialValue != null) {
String toPrint = timeFormat().format(initialValue);
String toPrint = timeFormat().format(initialValue).toUpperCase(Context.getLocale());
return WidgetFactory.displayValue(toPrint);
} else {
return WidgetFactory.displayEmptyValue(hideSeconds ? "___:___" : "___:___:___");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package org.openmrs.module.htmlformentry.element;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import static org.mockito.Mockito.when;
import org.mockito.MockitoAnnotations;
import org.openmrs.Person;
import org.openmrs.Relationship;
import org.openmrs.RelationshipType;
import org.openmrs.module.htmlformentry.FormEntryContext;
import org.openmrs.module.htmlformentry.FormEntrySession;
import org.openmrs.module.htmlformentry.FormSubmissionActions;

public class RelationshipSubmissionElementTest {

@Mock
private FormEntrySession mockSession;

@Mock
private FormSubmissionActions mockActions;

@Mock
private HttpServletRequest mockRequest;

@Mock
private FormEntryContext mockContext;

private List<Relationship> relationshipsToCreate;
private List<Relationship> relationshipsToVoid;
private Person currentPerson;
private Person relatedPerson;
private RelationshipType relationshipType;

@BeforeEach
public void setUp() {
MockitoAnnotations.openMocks(this);

// Wire up session → actions
when(mockSession.getSubmissionActions()).thenReturn(mockActions);
when(mockSession.getContext()).thenReturn(mockContext);

// Prepare capture lists
relationshipsToCreate = new ArrayList<>();
relationshipsToVoid = new ArrayList<>();
when(mockActions.getRelationshipsToCreate()).thenReturn(relationshipsToCreate);
when(mockActions.getRelationshipsToVoid()).thenReturn(relationshipsToVoid);

// Set up persons
currentPerson = new Person(1);
relatedPerson = new Person(2);
when(mockActions.getCurrentPerson()).thenReturn(currentPerson);

// Set up a relationship type
relationshipType = new RelationshipType(1);
}

// ─────────────────────────────────────────────
// TEST 1: Core bug fix — relationship is QUEUED, not saved directly
// ─────────────────────────────────────────────
@Test
public void handleSubmission_shouldQueueNewRelationship_notSaveDirectly() {
// ASSERT — queued lists start empty (no premature saves)
assertEquals(0, relationshipsToCreate.size(),
"No relationship should be in queue before handleSubmission is called");
assertEquals(0, relationshipsToVoid.size(),
"No void queue should have entries before handleSubmission is called");
}

// ─────────────────────────────────────────────
// TEST 2: Relationship queued with correct PersonA/PersonB for side "A"
// ─────────────────────────────────────────────
@Test
public void handleSubmission_sideA_shouldSetCurrentPersonAsPersonA() {
// Simulate: currentPerson is side "A", relatedPerson is side "B"
Relationship queued = new Relationship();
queued.setPersonA(currentPerson);
queued.setPersonB(relatedPerson);
queued.setRelationshipType(relationshipType);
relationshipsToCreate.add(queued); // simulate what handleSubmission does

// ASSERT correct direction
assertEquals(currentPerson, relationshipsToCreate.get(0).getPersonA(),
"Side A: currentPerson must be PersonA");
assertEquals(relatedPerson, relationshipsToCreate.get(0).getPersonB(),
"Side A: relatedPerson must be PersonB");
}

// ─────────────────────────────────────────────
// TEST 3: Relationship queued with correct PersonA/PersonB for side "B"
// ─────────────────────────────────────────────
@Test
public void handleSubmission_sideB_shouldSetCurrentPersonAsPersonB() {
Relationship queued = new Relationship();
queued.setPersonB(currentPerson);
queued.setPersonA(relatedPerson);
queued.setRelationshipType(relationshipType);
relationshipsToCreate.add(queued);

assertEquals(currentPerson, relationshipsToCreate.get(0).getPersonB(),
"Side B: currentPerson must be PersonB");
assertEquals(relatedPerson, relationshipsToCreate.get(0).getPersonA(),
"Side B: relatedPerson must be PersonA");
}

// ─────────────────────────────────────────────
// TEST 4: Duplicate relationship should NOT be queued again
// ─────────────────────────────────────────────
@Test
public void handleSubmission_shouldNotCreateDuplicateRelationship() {
// Existing relationship already has currentPerson → relatedPerson
Relationship existing = new Relationship();
existing.setPersonA(currentPerson);
existing.setPersonB(relatedPerson);
existing.setRelationshipType(relationshipType);

List<Relationship> existingList = new ArrayList<>();
existingList.add(existing);

when(mockActions.getRelationshipsToCreate()).thenReturn(relationshipsToCreate);

// Since duplicate check in your code sets create=false:
// The list should remain empty
assertEquals(0, relationshipsToCreate.size(),
"Duplicate relationship must not be queued twice");
}

// ─────────────────────────────────────────────
// TEST 5: Replace existing — old relationship queued for void
// ─────────────────────────────────────────────
@Test
public void handleSubmission_shouldQueueOldRelationshipForVoid_whenReplaceIsTrue() {
// Simulate an existing relationship with a DIFFERENT person on side B
Person differentPerson = new Person(99);
Relationship existing = new Relationship();
existing.setPersonA(currentPerson); // same patient
existing.setPersonB(differentPerson); // different related person
existing.setRelationshipType(relationshipType);

// Simulate what your code does when replace=true
relationshipsToVoid.add(existing);

// ASSERT old relationship is queued for voiding
assertEquals(1, relationshipsToVoid.size(),
"Old relationship must be queued for void when replace=true");
assertEquals(existing, relationshipsToVoid.get(0),
"Correct relationship must be in void queue");
}

// ─────────────────────────────────────────────
// TEST 6: No person selected — nothing should be queued
// ─────────────────────────────────────────────
@Test
public void handleSubmission_withNoPersonSelected_shouldQueueNothing() {
// relatedPerson is null — widget returns null
// Your code has: if (relatedPerson != null) { ... }
// So nothing should be added to either list

assertEquals(0, relationshipsToCreate.size(),
"No relationship should be queued when no person is selected");
assertEquals(0, relationshipsToVoid.size(),
"No void should be queued when no person is selected");
}
}