Skip to content

Commit

Permalink
functionPathName: Rework using pathTemplateIdentifiers() to give bett…
Browse files Browse the repository at this point in the history
…er results for templated paths
  • Loading branch information
kerrykimbrough committed Sep 12, 2019
1 parent 86dfee0 commit e3a5f79
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
import java.util.TreeSet;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import static java.math.RoundingMode.DOWN;
Expand Down Expand Up @@ -2153,15 +2155,33 @@ private <T extends Comparable<T>> T adjustedMinOf( String description, T min, T
/**
* Returns the component of a function name that represents the given API request path.
*/
private String functionPathName( String pathName)
static String functionPathName( String pathName)
{
return
Arrays.stream( pathName.split( "/"))
.flatMap( p -> pathTemplateIdentifiers( p))
.filter( p -> !p.isEmpty())
.map( p -> toIdentifier( p))
.collect( joining( "-"));
}

/**
* Returns the sequence of identifiers defined by the given element of an API request path
*/
static private Stream<String> pathTemplateIdentifiers( String pathElement)
{
Stream.Builder<String> identifiers = Stream.builder();
Matcher idMatcher = pathIdPattern_.matcher( pathElement);
while( idMatcher.find())
{
identifiers.add(
toIdentifier(
pathElement
.substring( idMatcher.start(), idMatcher.end())
.trim()));
}
return identifiers.build();
}

/**
* Returns input variable name for the given media type
*/
Expand Down Expand Up @@ -2958,5 +2978,7 @@ public boolean allRequired()

private final View view_;
private final NotificationContext context_;
private ModelOptions options_;
private ModelOptions options_;

private static final Pattern pathIdPattern_ = Pattern.compile( "[^{}]+|\\{[^}]\\}");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//////////////////////////////////////////////////////////////////////////////
//
// Copyright 2019, Cornutum Project
// www.cornutum.org
//
//////////////////////////////////////////////////////////////////////////////
package org.cornutum.tcases.openapi;

import static org.cornutum.tcases.openapi.InputModeller.functionPathName;

import org.junit.Test;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;

public class FunctionPathNameTest
{
@Test
public void whenSingleElement()
{
// Given...
String pathName = "/someResource";

// When...
String functionPathName = functionPathName( pathName);

// Then...
assertThat( "Function path name", functionPathName, is( "someResource"));
}

@Test
public void whenMultipleTemplates()
{
// Given...
String pathName = "/{service}/{caseId}.{ext}";

// When...
String functionPathName = functionPathName( pathName);

// Then...
assertThat( "Function path name", functionPathName, is( "service-caseId-ext"));
}

@Test
public void whenNonIdentifierChars()
{
// Given...
String pathName = "/What /is {12?}/34{56?}+{78?}90=/?";

// When...
String functionPathName = functionPathName( pathName);

// Then...
assertThat( "Function path name", functionPathName, is( "What-is-12-34-56-78-90"));
}
}

0 comments on commit e3a5f79

Please sign in to comment.