001package ca.uhn.fhir.test; 002 003/*- 004 * #%L 005 * HAPI FHIR Test Utilities 006 * %% 007 * Copyright (C) 2014 - 2023 Smile CDR, Inc. 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023import ca.uhn.fhir.context.FhirContext; 024import ca.uhn.fhir.context.FhirVersionEnum; 025import ca.uhn.fhir.i18n.Msg; 026import ca.uhn.fhir.test.utilities.BaseRestServerHelper; 027import ca.uhn.fhir.test.utilities.RestServerDstu3Helper; 028import ca.uhn.fhir.test.utilities.RestServerR4Helper; 029import ca.uhn.fhir.test.utilities.TlsAuthenticationTestHelper; 030import ca.uhn.fhir.tls.TlsAuthentication; 031import org.hl7.fhir.instance.model.api.IBaseResource; 032import org.junit.jupiter.api.extension.RegisterExtension; 033import org.junit.jupiter.params.provider.Arguments; 034 035import java.util.stream.Stream; 036 037public abstract class BaseFhirVersionParameterizedTest { 038 039 @RegisterExtension 040 public final RestServerR4Helper myRestServerR4Helper = new RestServerR4Helper(true); 041 @RegisterExtension 042 public final RestServerDstu3Helper myRestServerDstu3Helper = new RestServerDstu3Helper(true); 043 @RegisterExtension 044 public TlsAuthenticationTestHelper myTlsAuthenticationTestHelper = new TlsAuthenticationTestHelper(); 045 046 protected final FhirContext myR4FhirContext = FhirContext.forR4(); 047 protected final FhirContext myDstu3FhirContext = FhirContext.forDstu3(); 048 049 protected static Stream<Arguments> baseParamsProvider(){ 050 return Stream.of( 051 Arguments.arguments(FhirVersionEnum.R4), 052 Arguments.arguments(FhirVersionEnum.DSTU3) 053 ); 054 } 055 056 protected FhirVersionParams getFhirVersionParams(FhirVersionEnum theFhirVersion){ 057 switch(theFhirVersion){ 058 case R4: 059 return new FhirVersionParams(myRestServerR4Helper, myR4FhirContext); 060 case DSTU3: 061 return new FhirVersionParams(myRestServerDstu3Helper, myDstu3FhirContext); 062 default: 063 throw new RuntimeException(Msg.code(2114)+"Unknown FHIR Version param provided: " + theFhirVersion); 064 } 065 } 066 067 protected TlsAuthentication getTlsAuthentication(){ 068 return myTlsAuthenticationTestHelper.getTlsAuthentication(); 069 } 070 071 protected class FhirVersionParams { 072 private final BaseRestServerHelper myBaseRestServerHelper; 073 private final FhirContext myFhirContext; 074 private final FhirVersionEnum myFhirVersion; 075 076 public FhirVersionParams(BaseRestServerHelper theBaseRestServerHelper, FhirContext theFhirContext) { 077 myBaseRestServerHelper = theBaseRestServerHelper; 078 myFhirContext = theFhirContext; 079 myFhirVersion = theFhirContext.getVersion().getVersion(); 080 } 081 082 public FhirContext getFhirContext() { 083 return myFhirContext; 084 } 085 086 public FhirVersionEnum getFhirVersion() { 087 return myFhirVersion; 088 } 089 090 public String getBase(){ 091 return myBaseRestServerHelper.getBase(); 092 } 093 094 public String getSecureBase(){ 095 return myBaseRestServerHelper.getSecureBase(); 096 } 097 098 public String getPatientEndpoint(){ 099 return getBase()+"/Patient"; 100 } 101 102 public String getSecuredPatientEndpoint(){ 103 return getSecureBase()+"/Patient"; 104 } 105 106 public IBaseResource parseResource(String json){ 107 return myFhirContext.newJsonParser().parseResource(json); 108 } 109 } 110 111}