### Question:
R4FhirCarePlanToEnrollmentTransformer extends AbstractFhirCarePlanToEnrollmentTransformer { @Nonnull @Override protected Reference getProgramRef( @Nonnull FhirToDhisTransformerContext context, @Nonnull RuleInfo<EnrollmentRule> ruleInfo, @Nonnull Map<String, Object> scriptVariables, @Nonnull IBaseResource carePlan ) { final CarePlan fhirCarePlan = (CarePlan) carePlan; String uri = null; if ( !fhirCarePlan.getInstantiatesUri().isEmpty() ) { uri = fhirCarePlan.getInstantiatesUri().get( 0 ).getValueAsString(); } else if ( !fhirCarePlan.getInstantiatesCanonical().isEmpty() ) { uri = fhirCarePlan.getInstantiatesCanonical().get( 0 ).getValueAsString(); } if ( uri == null ) { throw new TransformerDataException( "No reference to a plan definition that is instantiated by this care plan has been given." ); } final IIdType id; try { id = FhirUriUtils.createIdFromUri( uri, FhirResourceType.PLAN_DEFINITION ); } catch ( IllegalArgumentException e ) { throw new TransformerDataException( e.getMessage(), e ); } return Reference.createIdReference( id.getIdPart() ); } R4FhirCarePlanToEnrollmentTransformer( @Nonnull ScriptExecutor scriptExecutor, @Nonnull TrackedEntityMetadataService trackedEntityMetadataService, @Nonnull TrackedEntityService trackedEntityService,
        @Nonnull TrackedEntityRuleRepository trackedEntityRuleRepository, @Nonnull OrganizationUnitService organizationUnitService, @Nonnull ProgramMetadataService programMetadataService, @Nonnull EnrollmentService enrollmentService,
        @Nonnull FhirResourceMappingRepository resourceMappingRepository, @Nonnull FhirDhisAssignmentRepository fhirDhisAssignmentRepository, @Nonnull ScriptExecutionContext scriptExecutionContext, @Nonnull ValueConverter valueConverter ); @Nonnull @Override Set<FhirVersion> getFhirVersions();  }### Answer:
@Test public void getProgramRefUri() { final CarePlan carePlan = new CarePlan(); carePlan.addInstantiatesUri( "PlanDefinition/sj87dhyui82" ); Assert.assertEquals( new org.dhis2.fhir.adapter.dhis.model.Reference( "sj87dhyui82", ReferenceType.ID ), transformer.getProgramRef( transformerContext, new RuleInfo<>( new EnrollmentRule(), Collections.emptyList() ), new HashMap<>(), carePlan ) ); }
@Test public void getProgramRefCanonical() { final CarePlan carePlan = new CarePlan(); carePlan.addInstantiatesCanonical( "PlanDefinition/sj87dhyui82" ); Assert.assertEquals( new org.dhis2.fhir.adapter.dhis.model.Reference( "sj87dhyui82", ReferenceType.ID ), transformer.getProgramRef( transformerContext, new RuleInfo<>( new EnrollmentRule(), Collections.emptyList() ), new HashMap<>(), carePlan ) ); }
@Test public void getProgramRefCanonicalWithoutType() { final CarePlan carePlan = new CarePlan(); carePlan.addInstantiatesCanonical( "sj87dhyui82" ); Assert.assertEquals( new org.dhis2.fhir.adapter.dhis.model.Reference( "sj87dhyui82", ReferenceType.ID ), transformer.getProgramRef( transformerContext, new RuleInfo<>( new EnrollmentRule(), Collections.emptyList() ), new HashMap<>(), carePlan ) ); }
@Test( expected = TransformerDataException.class ) public void getProgramRefNull() { final CarePlan carePlan = new CarePlan(); transformer.getProgramRef( transformerContext, new RuleInfo<>( new EnrollmentRule(), Collections.emptyList() ), new HashMap<>(), carePlan ); }