Best Hikaku code snippet using de.codecentric.hikaku.endpoints.MatrixParameter
HikakuTest.kt
Source:HikakuTest.kt
...747 }748 }749 }750 @Nested751 inner class MatrixParameterNameTests {752 @Test753 fun `matrix parameter names in random order match if the feature is supported by both converters`() {754 //given755 val specificationDummyConverter = object : EndpointConverter {756 override val conversionResult: Set<Endpoint> = setOf(757 Endpoint(758 path = "/todos",759 httpMethod = GET,760 matrixParameters = setOf(761 MatrixParameter("tag"),762 MatrixParameter("done")763 )764 )765 )766 override val supportedFeatures = SupportedFeatures(Feature.MatrixParameters)767 }768 val implementationDummyConverter = object : EndpointConverter {769 override val conversionResult: Set<Endpoint> = setOf(770 Endpoint(771 path = "/todos",772 httpMethod = GET,773 matrixParameters = setOf(774 MatrixParameter("done"),775 MatrixParameter("tag")776 )777 )778 )779 override val supportedFeatures = SupportedFeatures(Feature.MatrixParameters)780 }781 val hikaku = Hikaku(782 specificationDummyConverter,783 implementationDummyConverter,784 HikakuConfig(785 reporters = listOf(NoOperationReporter())786 )787 )788 //when789 hikaku.match()790 }791 @Test792 fun `matrix parameter names are skipped if the feature is not supported by one of the converters`() {793 //given794 val specificationDummyConverter = object : EndpointConverter {795 override val conversionResult: Set<Endpoint> = setOf(796 Endpoint(797 path = "/todos",798 httpMethod = GET,799 matrixParameters = setOf(800 MatrixParameter("done")801 )802 )803 )804 override val supportedFeatures = SupportedFeatures()805 }806 val implementationDummyConverter = object : EndpointConverter {807 override val conversionResult: Set<Endpoint> = setOf(808 Endpoint(809 path = "/todos",810 httpMethod = GET,811 matrixParameters = setOf(812 MatrixParameter("tag")813 )814 )815 )816 override val supportedFeatures = SupportedFeatures(Feature.MatrixParameters)817 }818 val hikaku = Hikaku(819 specificationDummyConverter,820 implementationDummyConverter,821 HikakuConfig(822 reporters = listOf(NoOperationReporter())823 )824 )825 //when826 hikaku.match()827 }828 @Test829 fun `matrix parameter names don't match`() {830 //given831 val specificationDummyConverter = object : EndpointConverter {832 override val conversionResult: Set<Endpoint> = setOf(833 Endpoint(834 path = "/todos",835 httpMethod = GET,836 matrixParameters = setOf(837 MatrixParameter("tag")838 )839 )840 )841 override val supportedFeatures = SupportedFeatures(Feature.MatrixParameters)842 }843 val implementationDummyConverter = object : EndpointConverter {844 override val conversionResult: Set<Endpoint> = setOf(845 Endpoint(846 path = "/todos",847 httpMethod = GET,848 matrixParameters = setOf(849 MatrixParameter("done")850 )851 )852 )853 override val supportedFeatures = SupportedFeatures(Feature.MatrixParameters)854 }855 val hikaku = Hikaku(856 specificationDummyConverter,857 implementationDummyConverter,858 HikakuConfig(859 reporters = listOf(NoOperationReporter())860 )861 )862 //when863 assertFailsWith<AssertionFailedError> {864 hikaku.match()865 }866 }867 @Test868 fun `matrix parameter required matches if the feature is supported by both converters`() {869 //given870 val specificationDummyConverter = object : EndpointConverter {871 override val conversionResult: Set<Endpoint> = setOf(872 Endpoint(873 path = "/todos",874 httpMethod = GET,875 matrixParameters = setOf(876 MatrixParameter("tag", true)877 )878 )879 )880 override val supportedFeatures = SupportedFeatures(Feature.MatrixParameters)881 }882 val implementationDummyConverter = object : EndpointConverter {883 override val conversionResult: Set<Endpoint> = setOf(884 Endpoint(885 path = "/todos",886 httpMethod = GET,887 matrixParameters = setOf(888 MatrixParameter("tag", true)889 )890 )891 )892 override val supportedFeatures = SupportedFeatures(Feature.MatrixParameters)893 }894 val hikaku = Hikaku(895 specificationDummyConverter,896 implementationDummyConverter,897 HikakuConfig(898 reporters = listOf(NoOperationReporter())899 )900 )901 //when902 hikaku.match()903 }904 @Test905 fun `matrix parameter required is skipped if option is not supported by one of the converters`() {906 //given907 val specificationDummyConverter = object : EndpointConverter {908 override val conversionResult: Set<Endpoint> = setOf(909 Endpoint(910 path = "/todos",911 httpMethod = GET,912 matrixParameters = setOf(913 MatrixParameter("tag", true)914 )915 )916 )917 override val supportedFeatures = SupportedFeatures()918 }919 val implementationDummyConverter = object : EndpointConverter {920 override val conversionResult: Set<Endpoint> = setOf(921 Endpoint(922 path = "/todos",923 httpMethod = GET,924 headerParameters = setOf(925 HeaderParameter("tag", false)926 )927 )928 )929 override val supportedFeatures = SupportedFeatures(Feature.MatrixParameters)930 }931 val hikaku = Hikaku(932 specificationDummyConverter,933 implementationDummyConverter,934 HikakuConfig(935 reporters = listOf(NoOperationReporter())936 )937 )938 //when939 hikaku.match()940 }941 @Test942 fun `matrix parameter required don't match`() {943 //given944 val specificationDummyConverter = object : EndpointConverter {945 override val conversionResult: Set<Endpoint> = setOf(946 Endpoint(947 path = "/todos",948 httpMethod = GET,949 matrixParameters = setOf(950 MatrixParameter("allow-cache", true)951 )952 )953 )954 override val supportedFeatures = SupportedFeatures(Feature.MatrixParameters)955 }956 val implementationDummyConverter = object : EndpointConverter {957 override val conversionResult: Set<Endpoint> = setOf(958 Endpoint(959 path = "/todos",960 httpMethod = GET,961 matrixParameters = setOf(962 MatrixParameter("allow-cache", false)963 )964 )965 )966 override val supportedFeatures = SupportedFeatures(Feature.MatrixParameters)967 }968 val hikaku = Hikaku(969 specificationDummyConverter,970 implementationDummyConverter,971 HikakuConfig(972 reporters = listOf(NoOperationReporter())973 )974 )975 //when976 assertFailsWith<AssertionFailedError> {977 hikaku.match()978 }979 }980 }...
SpringConverterMatrixParameterTest.kt
Source:SpringConverterMatrixParameterTest.kt
1package de.codecentric.hikaku.converters.spring.matrixparameters2import de.codecentric.hikaku.converters.spring.SpringConverter3import de.codecentric.hikaku.endpoints.Endpoint4import de.codecentric.hikaku.endpoints.HttpMethod.*5import de.codecentric.hikaku.endpoints.MatrixParameter6import org.assertj.core.api.Assertions.assertThat7import org.junit.jupiter.api.Nested8import org.junit.jupiter.api.Test9import org.springframework.beans.factory.annotation.Autowired10import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration11import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest12import org.springframework.context.ConfigurableApplicationContext13import org.springframework.http.MediaType.APPLICATION_JSON_VALUE14import org.springframework.http.MediaType.TEXT_HTML_VALUE15import kotlin.test.assertFailsWith16class SpringConverterMatrixParameterTest {17 @Nested18 @WebMvcTest(MatrixParameterNamedByVariableController::class, excludeAutoConfiguration = [ErrorMvcAutoConfiguration::class])19 inner class MatrixParameterNamedByVariableTest {20 @Autowired21 lateinit var context: ConfigurableApplicationContext22 @Test23 fun `matrix parameter name defined by variable name`() {24 //given25 val specification: Set<Endpoint> = setOf(26 Endpoint(27 path = "/todos",28 httpMethod = GET,29 matrixParameters = setOf(30 MatrixParameter("tag", true)31 )32 ),33 Endpoint("/todos", OPTIONS),34 Endpoint(35 path = "/todos",36 httpMethod = HEAD,37 matrixParameters = setOf(38 MatrixParameter("tag", true)39 )40 )41 )42 //when43 val implementation = SpringConverter(context)44 //then45 assertThat(implementation.conversionResult).containsExactlyInAnyOrderElementsOf(specification)46 }47 }48 @Nested49 @WebMvcTest(MatrixParameterNamedByValueAttributeController::class, excludeAutoConfiguration = [ErrorMvcAutoConfiguration::class])50 inner class MatrixParameterNamedByValueAttributeTest {51 @Autowired52 lateinit var context: ConfigurableApplicationContext53 @Test54 fun `matrix parameter name defined by attribute 'value'`() {55 //given56 val specification: Set<Endpoint> = setOf(57 Endpoint(58 path = "/todos",59 httpMethod = GET,60 matrixParameters = setOf(61 MatrixParameter("tag", true)62 )63 ),64 Endpoint("/todos", OPTIONS),65 Endpoint(66 path = "/todos",67 httpMethod = HEAD,68 matrixParameters = setOf(69 MatrixParameter("tag", true)70 )71 )72 )73 //when74 val implementation = SpringConverter(context)75 //then76 assertThat(implementation.conversionResult).containsExactlyInAnyOrderElementsOf(specification)77 }78 }79 @Nested80 @WebMvcTest(MatrixParameterNamedByNameAttributeController::class, excludeAutoConfiguration = [ErrorMvcAutoConfiguration::class])81 inner class MatrixParameterNamedByNameAttributeTest {82 @Autowired83 lateinit var context: ConfigurableApplicationContext84 @Test85 fun `matrix parameter name defined by attribute 'name'`() {86 //given87 val specification: Set<Endpoint> = setOf(88 Endpoint(89 path = "/todos",90 httpMethod = GET,91 matrixParameters = setOf(92 MatrixParameter("tag", true)93 )94 ),95 Endpoint("/todos", OPTIONS),96 Endpoint(97 path = "/todos",98 httpMethod = HEAD,99 matrixParameters = setOf(100 MatrixParameter("tag", true)101 )102 )103 )104 //when105 val implementation = SpringConverter(context)106 //then107 assertThat(implementation.conversionResult).containsExactlyInAnyOrderElementsOf(specification)108 }109 }110 @Nested111 @WebMvcTest(MatrixParameterHavingBothNameAndValueAttributeController::class, excludeAutoConfiguration = [ErrorMvcAutoConfiguration::class])112 inner class MatrixParameterHavingBothNameAndValueAttributeTest {113 @Autowired114 lateinit var context: ConfigurableApplicationContext115 @Test116 fun `both 'value' and 'name' attribute defined for matrix parameter`() {117 assertFailsWith<IllegalStateException> {118 SpringConverter(context).conversionResult119 }120 }121 }122 @Nested123 @WebMvcTest(MatrixParameterOptionalController::class, excludeAutoConfiguration = [ErrorMvcAutoConfiguration::class])124 inner class MatrixParameterOptionalTest {125 @Autowired126 lateinit var context: ConfigurableApplicationContext127 @Test128 fun `matrix parameter optional`() {129 //given130 val specification: Set<Endpoint> = setOf(131 Endpoint(132 path = "/todos",133 httpMethod = GET,134 matrixParameters = setOf(135 MatrixParameter("tag", false)136 )137 ),138 Endpoint("/todos", OPTIONS),139 Endpoint(140 path = "/todos",141 httpMethod = HEAD,142 matrixParameters = setOf(143 MatrixParameter("tag", false)144 )145 )146 )147 //when148 val implementation = SpringConverter(context)149 //then150 assertThat(implementation.conversionResult).containsExactlyInAnyOrderElementsOf(specification)151 }152 }153 @Nested154 @WebMvcTest(MatrixParameterOptionalBecauseOfDefaultValueController::class, excludeAutoConfiguration = [ErrorMvcAutoConfiguration::class])155 inner class MatrixParameterOptionalBecauseOfDefaultValueTest {156 @Autowired157 lateinit var context: ConfigurableApplicationContext158 @Test159 fun `matrix parameter optional because of a default value`() {160 //given161 val specification: Set<Endpoint> = setOf(162 Endpoint(163 path = "/todos",164 httpMethod = GET,165 matrixParameters = setOf(166 MatrixParameter("tag", false)167 )168 ),169 Endpoint("/todos", OPTIONS),170 Endpoint(171 path = "/todos",172 httpMethod = HEAD,173 matrixParameters = setOf(174 MatrixParameter("tag", false)175 )176 )177 )178 //when179 val implementation = SpringConverter(context)180 //then181 assertThat(implementation.conversionResult).containsExactlyInAnyOrderElementsOf(specification)182 }183 }184 @Nested185 @WebMvcTest(MatrixParameterOnDefaultErrorEndpointController::class)186 inner class MatrixParameterOnDefaultErrorEndpointTest {187 @Autowired188 lateinit var context: ConfigurableApplicationContext189 @Test190 fun `matrix parameter is not available in default error endpoints`() {191 //given192 val specification: Set<Endpoint> = setOf(193 Endpoint(194 path = "/todos",195 httpMethod = GET,196 matrixParameters = setOf(197 MatrixParameter("tag", true)198 )199 ),200 Endpoint("/todos", OPTIONS),201 Endpoint(202 path = "/todos",203 httpMethod = HEAD,204 matrixParameters = setOf(205 MatrixParameter("tag", true)206 )207 ),208 Endpoint(209 path = "/error",210 httpMethod = GET,211 produces = setOf(APPLICATION_JSON_VALUE)212 ),213 Endpoint(214 path = "/error",215 httpMethod = POST,216 produces = setOf(APPLICATION_JSON_VALUE)217 ),218 Endpoint(219 path = "/error",...
JaxRsConverter.kt
Source:JaxRsConverter.kt
...13 override val supportedFeatures = SupportedFeatures(14 Feature.QueryParameters,15 Feature.PathParameters,16 Feature.HeaderParameters,17 Feature.MatrixParameters,18 Feature.Consumes,19 Feature.Produces,20 Feature.Deprecation21 )22 override fun convert(): Set<Endpoint> {23 if (packageName.isBlank()) {24 throw EndpointConverterException("Package name must not be blank.")25 }26 return ClassLocator.getClasses(packageName)27 .filter { it.getAnnotation(Path::class.java) != null }28 .flatMap { extractEndpoints(it) }29 .toSet()30 }31 private fun extractEndpoints(resource: Class<*>): List<Endpoint> {32 return resource.methods33 .filter { isHttpMethodAnnotationPresent(it) }34 .map { createEndpoint(resource, it) }35 }36 private fun isHttpMethodAnnotationPresent(method: Method): Boolean {37 return when {38 method.isAnnotationPresent(DELETE::class.java) -> true39 method.isAnnotationPresent(GET::class.java) -> true40 method.isAnnotationPresent(HEAD::class.java) -> true41 method.isAnnotationPresent(OPTIONS::class.java) -> true42 method.isAnnotationPresent(PATCH::class.java) -> true43 method.isAnnotationPresent(POST::class.java) -> true44 method.isAnnotationPresent(PUT::class.java) -> true45 else -> false46 }47 }48 private fun createEndpoint(resource: Class<*>, method: Method) = Endpoint(49 path = extractPath(resource, method),50 httpMethod = extractHttpMethod(method),51 pathParameters = extractPathParameters(method),52 queryParameters = extractQueryParameters(method),53 headerParameters = extractHeaderParameters(method),54 matrixParameters = extractMatrixParameters(method),55 produces = extractProduces(resource, method),56 consumes = extractConsumes(resource, method),57 deprecated = isEndpointDeprecated(method)58 )59 private fun extractPath(resource: Class<*>, method: Method): String {60 var pathOnClass = resource.getAnnotation(Path::class.java).value61 val pathOnFunction = if (method.isAnnotationPresent(Path::class.java)) {62 method.getAnnotation(Path::class.java).value63 } else {64 ""65 }66 if (!pathOnClass.startsWith("/")) {67 pathOnClass = "/$pathOnClass"68 }69 val combinedPath = "$pathOnClass/$pathOnFunction".replace(Regex("/+"), "/")70 return if (combinedPath.endsWith('/')) {71 combinedPath.substringBeforeLast('/')72 } else {73 combinedPath74 }75 }76 private fun extractHttpMethod(method: Method): HttpMethod {77 return when {78 method.isAnnotationPresent(DELETE::class.java) -> HttpMethod.DELETE79 method.isAnnotationPresent(GET::class.java) -> HttpMethod.GET80 method.isAnnotationPresent(HEAD::class.java) -> HttpMethod.HEAD81 method.isAnnotationPresent(OPTIONS::class.java) -> HttpMethod.OPTIONS82 method.isAnnotationPresent(PATCH::class.java) -> HttpMethod.PATCH83 method.isAnnotationPresent(POST::class.java) -> HttpMethod.POST84 method.isAnnotationPresent(PUT::class.java) -> HttpMethod.PUT85 else -> throw IllegalStateException("Unable to determine http method. Valid annotation not found.")86 }87 }88 private fun extractProduces(resource: Class<*>, method: Method): Set<String> {89 val annotationValue = when {90 method.isAnnotationPresent(Produces::class.java) -> method.getAnnotation(Produces::class.java).value.toSet()91 resource.isAnnotationPresent(Produces::class.java) -> resource.getAnnotation(Produces::class.java).value.toSet()92 else -> setOf("*/*")93 }94 return if (method.returnType.kotlin.isUnit()) {95 emptySet()96 } else {97 annotationValue98 }99 }100 private fun extractConsumes(resource: Class<*>, method: Method): Set<String> {101 val annotationValue = when {102 method.isAnnotationPresent(Consumes::class.java) -> method.getAnnotation(Consumes::class.java).value.toSet()103 resource.isAnnotationPresent(Consumes::class.java) -> resource.getAnnotation(Consumes::class.java).value.toSet()104 else -> setOf("*/*")105 }106 return if (containsRequestBody(method)) {107 annotationValue108 } else {109 emptySet()110 }111 }112 private fun containsRequestBody(method: Method): Boolean {113 return method.parameters114 .filterNot { it.isAnnotationPresent(BeanParam::class.java) }115 .filterNot { it.isAnnotationPresent(CookieParam::class.java) }116 .filterNot { it.isAnnotationPresent(DefaultValue::class.java) }117 .filterNot { it.isAnnotationPresent(Encoded::class.java) }118 .filterNot { it.isAnnotationPresent(FormParam::class.java) }119 .filterNot { it.isAnnotationPresent(HeaderParam::class.java) }120 .filterNot { it.isAnnotationPresent(MatrixParam::class.java) }121 .filterNot { it.isAnnotationPresent(PathParam::class.java) }122 .filterNot { it.isAnnotationPresent(QueryParam::class.java) }123 .isNotEmpty()124 }125 private fun extractQueryParameters(method: Method): Set<QueryParameter> {126 return method.parameters127 .filter { it.isAnnotationPresent(QueryParam::class.java) }128 .map { it.getAnnotation(QueryParam::class.java) }129 .map { (it as QueryParam).value }130 .map { QueryParameter(it, false) }131 .toSet()132 }133 private fun extractPathParameters(method: Method): Set<PathParameter> {134 return method.parameters135 .filter { it.isAnnotationPresent(PathParam::class.java) }136 .map { it.getAnnotation(PathParam::class.java) }137 .map { (it as PathParam).value }138 .map { PathParameter(it) }139 .toSet()140 }141 private fun extractHeaderParameters(method: Method): Set<HeaderParameter> {142 return method.parameters143 .filter { it.isAnnotationPresent(HeaderParam::class.java) }144 .map { it.getAnnotation(HeaderParam::class.java) }145 .map { (it as HeaderParam).value }146 .map { HeaderParameter(it) }147 .toSet()148 }149 private fun extractMatrixParameters(method: Method): Set<MatrixParameter> {150 return method.parameters151 .filter { it.isAnnotationPresent(MatrixParam::class.java) }152 .map { it.getAnnotation(MatrixParam::class.java) }153 .map { (it as MatrixParam).value }154 .map { MatrixParameter(it) }155 .toSet()156 }157 private fun isEndpointDeprecated(method: Method) =158 method.isAnnotationPresent(Deprecated::class.java)159 || method.declaringClass.isAnnotationPresent(Deprecated::class.java)160}...
WadlConverter.kt
Source:WadlConverter.kt
...29 override val supportedFeatures = SupportedFeatures(30 Feature.QueryParameters,31 Feature.HeaderParameters,32 Feature.PathParameters,33 Feature.MatrixParameters,34 Feature.Produces,35 Feature.Consumes36 )37 private val xPath = XPathFactory38 .newInstance()39 .newXPath()40 override fun convert(): Set<Endpoint> {41 try {42 return parseWadl()43 } catch (throwable: Throwable) {44 throw EndpointConverterException(throwable)45 }46 }47 private fun parseWadl(): Set<Endpoint> {48 val doc = DocumentBuilderFactory49 .newInstance()50 .newDocumentBuilder()51 .parse(InputSource(StringReader(wadl)))52 val resources = xPath.evaluate("//resource", doc, NODESET) as NodeList53 val endpoints = mutableSetOf<Endpoint>()54 for (index in 0 until resources.length) {55 endpoints.addAll(createEndpoints(resources.item(index)))56 }57 return endpoints58 }59 private fun createEndpoints(resourceElement: Node): Set<Endpoint> {60 val path = resourceElement.getAttribute("path")61 val methods = xPath.evaluate("//resource[@path=\"$path\"]//method", resourceElement.childNodes, NODESET) as NodeList62 val endpoints: MutableSet<Endpoint> = mutableSetOf()63 for (i in 0 until methods.length) {64 val method = methods.item(i)65 val httpMethod = HttpMethod.valueOf(method.getAttribute("name"))66 endpoints.add(67 Endpoint(68 path = path,69 httpMethod = httpMethod,70 queryParameters = extractQueryParameters(method),71 headerParameters = extractHeaderParameters(method),72 pathParameters = extractPathParameters(method),73 matrixParameters = extractMatrixParameters(method),74 produces = extractResponseMediaTypes(method),75 consumes = extractConsumesMediaTypes(method)76 )77 )78 }79 return endpoints80 }81 private fun extractResponseMediaTypes(method: Node) = extractMediaTypes(method, "response")82 private fun extractConsumesMediaTypes(method: Node) = extractMediaTypes(method, "request")83 private fun extractMediaTypes(method: Node, xmlBaseElement: String): Set<String> {84 val representations = xPath.evaluate("//$xmlBaseElement/representation", method.childNodes, NODESET) as NodeList85 val mediaTypes: MutableSet<String> = mutableSetOf()86 for (i in 0 until representations.length) {87 val parameter = representations.item(i)88 mediaTypes += parameter.getAttribute("mediaType")89 }90 return mediaTypes91 }92 private fun extractPathParameters(method: Node): Set<PathParameter> {93 return extractParameter(method, "template")94 .entries95 .map { PathParameter(it.key) }96 .toSet()97 }98 private fun extractQueryParameters(method: Node): Set<QueryParameter> {99 return extractParameter(method, "query")100 .entries101 .map { QueryParameter(it.key, it.value) }102 .toSet()103 }104 private fun extractHeaderParameters(method: Node): Set<HeaderParameter> {105 return extractParameter(method, "header")106 .entries107 .map { HeaderParameter(it.key, it.value) }108 .toSet()109 }110 private fun extractMatrixParameters(method: Node): Set<MatrixParameter> {111 return extractParameter(method, "matrix")112 .entries113 .map { MatrixParameter(it.key, it.value) }114 .toSet()115 }116 private fun extractParameter(method: Node, style: String): Map<String, Boolean> {117 val parameters = xPath.evaluate("//param[@style=\"$style\"]", method.childNodes, NODESET) as NodeList118 val parameterMap: MutableMap<String, Boolean> = mutableMapOf()119 for (i in 0 until parameters.length) {120 val parameter = parameters.item(i)121 val parameterName = parameter.getAttribute("name")122 val isParameterRequired = "true" == parameter.getAttribute("required")123 parameterMap[parameterName] = isParameterRequired124 }125 return parameterMap126 }127}...
CommandLineReporter.kt
Source:CommandLineReporter.kt
...36 path += when(it) {37 Feature.QueryParameters -> listQueryParameters(endpoint.queryParameters)38 Feature.PathParameters -> listPathParameters(endpoint.pathParameters)39 Feature.HeaderParameters -> listHeaderParameter(endpoint.headerParameters)40 Feature.MatrixParameters -> listMatrixParameter(endpoint.matrixParameters)41 Feature.Consumes -> listRequestMediaTypes(endpoint.consumes)42 Feature.Produces -> listResponseMediaTypes(endpoint.produces)43 Feature.Deprecation -> if (endpoint.deprecated) " Deprecated" else ""44 }45 }46 println("$path >")47 }48 private fun listQueryParameters(queryParameters: Set<QueryParameter>) =49 " QueryParameters[${queryParameters.joinToString {50 "${it.parameterName} (${if(it.required) "required" else "optional"})"51 }}]"52 private fun listPathParameters(pathParameters: Set<PathParameter>) =53 " PathParameters[${pathParameters.joinToString {54 it.parameterName55 }}]"56 private fun listHeaderParameter(headerParameters: Set<HeaderParameter>) =57 " HeaderParameters[${headerParameters.joinToString {58 "${it.parameterName} (${if(it.required) "required" else "optional"})"59 }}]"60 private fun listMatrixParameter(matrixParameters: Set<MatrixParameter>) =61 " MatrixParameters[${matrixParameters.joinToString {62 "${it.parameterName} (${if(it.required) "required" else "optional"})"63 }}]"64 private fun listRequestMediaTypes(requestMediaTypes: Set<String>) =65 " Consumes[${requestMediaTypes.joinToString()}]"66 private fun listResponseMediaTypes(responseMediaTypes: Set<String>) =67 " Produces[${responseMediaTypes.joinToString()}]"68}...
MatrixParametersSpringExtension.kt
Source:MatrixParametersSpringExtension.kt
1package de.codecentric.hikaku.converters.spring.extensions2import de.codecentric.hikaku.endpoints.MatrixParameter3import org.springframework.web.bind.annotation.MatrixVariable4import org.springframework.web.bind.annotation.ValueConstants5import org.springframework.web.method.HandlerMethod6import kotlin.reflect.KParameter7import kotlin.reflect.jvm.kotlinFunction8internal fun HandlerMethod.hikakuMatrixParameters(): Set<MatrixParameter> {9 val method = this.method.kotlinFunction ?: return emptySet()10 return method.parameters11 .filter { it.annotations.filterIsInstance<MatrixVariable>().any() }12 .map { extractMatrixParameter(it) }13 .toSet()14}15private fun extractMatrixParameter(it: KParameter): MatrixParameter {16 val matrixParameter = it.annotations.find { it is MatrixVariable } as MatrixVariable17 val parameterName = extractMatrixParameterName(matrixParameter, it)18 val isRequired = isMatrixParameterRequired(matrixParameter)19 return MatrixParameter(parameterName, isRequired)20}21private fun isMatrixParameterRequired(matrixParameter: MatrixVariable): Boolean {22 if (matrixParameter.defaultValue == ValueConstants.DEFAULT_NONE) {23 return matrixParameter.required24 }25 return false26}27private fun extractMatrixParameterName(matrixParameter: MatrixVariable, it: KParameter): String {28 check(!(matrixParameter.value.isNotBlank() && matrixParameter.name.isNotBlank())) {29 "Both 'value' and 'name' attribute are provided for matrix parameter '${it.name}'. Only one is permitted."30 }31 return when {32 matrixParameter.value.isNotBlank() -> matrixParameter.value33 matrixParameter.name.isNotBlank() -> matrixParameter.name34 else -> it.name ?: ""35 }36}...
JaxRsConverterMatrixParametersTest.kt
Source:JaxRsConverterMatrixParametersTest.kt
1package de.codecentric.hikaku.converters.jaxrs2import de.codecentric.hikaku.endpoints.Endpoint3import de.codecentric.hikaku.endpoints.HttpMethod.GET4import de.codecentric.hikaku.endpoints.MatrixParameter5import org.assertj.core.api.Assertions.assertThat6import org.junit.jupiter.api.Test7class JaxRsConverterMatrixParametersTest {8 @Test9 fun `matrix parameter on function`() {10 //given11 val specification = setOf(12 Endpoint(13 path = "/todos",14 httpMethod = GET,15 matrixParameters = setOf(16 MatrixParameter("tag")17 )18 )19 )20 //when21 val result = JaxRsConverter("test.jaxrs.matrixparameters.onfunction").conversionResult22 //then23 assertThat(result).containsExactlyInAnyOrderElementsOf(specification)24 }25}
WadlConverterMatrixParameterTest.kt
Source:WadlConverterMatrixParameterTest.kt
1package de.codecentric.hikaku.converters.wadl2import de.codecentric.hikaku.endpoints.MatrixParameter3import org.assertj.core.api.Assertions.assertThat4import org.junit.jupiter.api.Test5import java.nio.file.Paths6class WadlConverterMatrixParameterTest {7 @Test8 fun `check that matrix parameter are extracted correctly`() {9 //given10 val file = Paths.get(this::class.java.classLoader.getResource("matrix_parameters.wadl").toURI())11 val matrixParameters = setOf(12 MatrixParameter("done", false),13 MatrixParameter("tag", true)14 )15 //when16 val specification = WadlConverter(file)17 //then18 val resultingMatrixParameters = specification.conversionResult.toList()[0].matrixParameters19 assertThat(resultingMatrixParameters).containsExactlyInAnyOrderElementsOf(matrixParameters)20 }21}...
MatrixParameter
Using AI Code Generation
1val matrixParameter = MatrixParameter("matrix", "value")2val queryParameter = QueryParameter("query", "value")3val headerParameter = HeaderParameter("header", "value")4val cookieParameter = CookieParameter("cookie", "value")5val bodyParameter = BodyParameter("body")6val requestBody = RequestBody(7val responseBody = ResponseBody(8val endpoint = Endpoint(9 setOf(10 PathParameter("path", "value"),11 MatrixParameter("matrix", "value"),12 QueryParameter("query", "value"),13 HeaderParameter("header", "value"),14 CookieParameter("cookie", "value"),15 BodyParameter("body")16 setOf(17 ResponseBody("application/json", "body")18val endpoints = Endpoints(19 setOf(20 Endpoint(21 setOf(22 PathParameter("path", "value"),23 MatrixParameter("matrix", "value"),24 QueryParameter("query", "value"),25 HeaderParameter("header", "value"),26 CookieParameter("cookie", "value"),27 BodyParameter("body")28 setOf(29 ResponseBody("application/json", "body")30val endpointConverter = EndpointConverter()31val endpointsConverter = EndpointsConverter()
MatrixParameter
Using AI Code Generation
1MatrixParameter("name", "value")2QueryParameter("name", "value")3HeaderParameter("name", "value")4CookieParameter("name", "value")5FormParameter("name", "value")6Body("value")7RequestBody("value")8ResponseBody("value")9Response(200, "OK", "application/json", ResponseBody("value"))10Request("GET", "path", listOf(HeaderParameter("name", "value")), Response(200, "OK", "application/json", ResponseBody("value")))11Endpoint("GET", "path", listOf(HeaderParameter("name", "value")), Response(200, "OK", "application/json", ResponseBody("value")))12Endpoints(listOf(Endpoint("GET", "path", listOf(HeaderParameter("name", "value")), Response(200, "OK", "application/json", ResponseBody("value")))))13EndpointConverter.convert("com.example")14EndpointConverter.convert("com.example")15EndpointConverter.convert("com.example")16EndpointConverter.convert("com.example")
MatrixParameter
Using AI Code Generation
1val matrixParameter = MatrixParameter("matrixParameter", "matrixParameter", "matrixParameter")2val queryParameter = QueryParameter("queryParameter", "queryParameter", "queryParameter")3val header = Header("header", "header", "header")4val requestBody = RequestBody("requestBody", "requestBody", "requestBody")5val responseBody = ResponseBody("responseBody", "responseBody", "responseBody")6val endpoint = Endpoint(7 produces = setOf(ContentType.APPLICATION_JSON),8 consumes = setOf(ContentType.APPLICATION_JSON),9 headers = setOf(header),10 pathParameters = setOf(pathParameter),11 matrixParameters = setOf(matrixParameter),12 queryParameters = setOf(queryParameter),13val endpoints = Endpoints(setOf(endpoint))14val swaggerConverter = SwaggerConverter()15val openApiConverter = OpenAPIConverter()16val ramlConverter = RamlConverter()17val springMvcConverter = SpringMvcConverter()18val springWebFluxConverter = SpringWebFluxConverter()19val jaxRsConverter = JaxRsConverter()
MatrixParameter
Using AI Code Generation
1val parameter = MatrixParameter("id")2val parameter = PathParameter("id")3val parameter = QueryParameter("id")4val requestBody = RequestBody("application/json", "id")5val responseBody = ResponseBody("application/json", "id")6val responseHeader = ResponseHeader("id")7val responseStatus = ResponseStatus(200)8val headerParameter = HeaderParameter("id")9val cookieParameter = CookieParameter("id")10val formParameter = FormParameter("id")11val endpoint = Endpoint(12 consumes = setOf("application/json"),13 produces = setOf("application/json"),14 parameters = setOf(parameter),15 requestHeaders = setOf(headerParameter),16 requestCookies = setOf(cookieParameter),17 requestParameters = setOf(formParameter),18 responseHeaders = setOf(responseHeader),19val specification = Specification(setOf(endpoint))20val specificationConverter = SpecificationConverter(specification)
MatrixParameter
Using AI Code Generation
1val matrixParameter = MatrixParameter("matrixParameter", String::class.java)2val queryParameter = QueryParameter("queryParameter", String::class.java)3val headerParameter = HeaderParameter("headerParameter", String::class.java)4val cookieParameter = CookieParameter("cookieParameter", String::class.java)5val formParameter = FormParameter("formParameter", String::class.java)6val pathParameter = PathParameter("pathParameter", String::class.java)7val bodyParameter = BodyParameter(String::class.java)8val requestBody = RequestBody(String::class.java)9val requestBody = RequestBody(String::class.java)10val requestBody = RequestBody(String::class.java)11val requestBody = RequestBody(String::class.java)
MatrixParameter
Using AI Code Generation
1 val matrixParameter = MatrixParameter("matrixParameter", "matrixParameterDescription", String::class)2 val queryParameter = QueryParameter("queryParameter", "queryParameterDescription", String::class)3 val headerParameter = HeaderParameter("headerParameter", "headerParameterDescription", String::class)4 val cookieParameter = CookieParameter("cookieParameter", "cookieParameterDescription", String::class)5 val body = Body("body", "bodyDescription", String::class)6 val multipleParameters = MultipleParameters(7 val endpoint = Endpoint(8 produces = setOf(ContentType.APPLICATION_JSON),9 val endpoints = Endpoints(setOf(endpoint))10 val specification = EndpointsConverter.convert(endpoints)11}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!