Skip to content

Commit

Permalink
- Fix junit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rathnapandi committed Dec 1, 2023
1 parent df3a5e4 commit ecebfc3
Show file tree
Hide file tree
Showing 5 changed files with 353 additions and 358 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ private APISpecificationFactory() {

private static final Logger LOG = LoggerFactory.getLogger(APISpecificationFactory.class);

private static final List<APISpecification> specificationTypes = Arrays.asList(new Swagger2xSpecification(), new Swagger1xSpecification(),
new OAS3xSpecification(), new WSDLSpecification(), new GraphqlSpecification(), new WADLSpecification(), new ODataV2Specification(),
new ODataV3Specification(), new ODataV4Specification());


public static APISpecification getAPISpecification(DesiredAPISpecification desiredAPISpec, String configBaseDir, String apiName) throws AppException {
APISpecification spec = getAPISpecification(getAPIDefinitionContent(desiredAPISpec.getResource(), configBaseDir), desiredAPISpec.getResource(), apiName, true, true);
spec.setFilterConfig(desiredAPISpec.getFilter()).filterAPISpecification();
Expand All @@ -51,11 +46,15 @@ public static APISpecification getAPISpecification(byte[] apiSpecificationConten


public static APISpecification getAPISpecification(byte[] apiSpecificationContent, String apiDefinitionFile, String apiName, boolean failOnError, boolean logDetectedVersion) throws AppException {
List<APISpecification> specificationTypes = Arrays.asList(new Swagger2xSpecification(), new Swagger1xSpecification(),
new OAS3xSpecification(), new WSDLSpecification(), new GraphqlSpecification(), new WADLSpecification(), new ODataV2Specification(),
new ODataV3Specification(), new ODataV4Specification());
if (LOG.isDebugEnabled()) {
LOG.debug("Handle API-Specification: {} , apiDefinitionFile: {} , API Name : {} ", getContentStart(apiSpecificationContent), apiDefinitionFile, apiName);
}
for (APISpecification spec : specificationTypes) {
spec.setApiSpecificationFile(apiDefinitionFile);

if (!spec.parse(apiSpecificationContent)) {
LOG.debug("Can't handle API specification with class: {} ", spec.getClass().getName());
} else {
Expand All @@ -68,17 +67,13 @@ public static APISpecification getAPISpecification(byte[] apiSpecificationConten
}
return spec;
}

}
if (!failOnError) {
LOG.error("API: {} has a unknown/invalid API-Specification", apiName);
if (LOG.isDebugEnabled()) {
LOG.debug("Specification {}", getContentStart(apiSpecificationContent));
}
return new UnknownAPISpecification(apiName);
}
if (LOG.isDebugEnabled()) {
LOG.debug("API: {} has a unknown/invalid API-Specification: {}", apiName, getContentStart(apiSpecificationContent));
}
LOG.debug("API: {} has a unknown/invalid API-Specification", apiName);
throw new AppException("Can't handle API specification. No suitable API-Specification implementation available.", ErrorCode.UNSUPPORTED_API_SPECIFICATION);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,26 @@
public class ODataV3Specification extends ODataSpecification {


@Override
public void updateBasePath(String basePath, String host) { // implementation ignored
}
@Override
public void updateBasePath(String basePath, String host) { // implementation ignored
}

@Override
public APISpecType getAPIDefinitionType() throws AppException {
return APISpecType.ODATA_V3;
}
@Override
public APISpecType getAPIDefinitionType() throws AppException {
return APISpecType.ODATA_V3;
}

@Override
public boolean parse(byte[] apiSpecificationContent) throws AppException{
String specStart = new String(apiSpecificationContent, 0, 500).toLowerCase();
if(specStart.contains("edmx") && specStart.contains("3.0")) {
throw new AppException("Detected OData V3 specification, which is not yet supported by the APIM-CLI.\n"
+ " | If you have a need for OData V3 support please upvote the following issue:\n"
+ " | https://github.com/Axway-API-Management-Plus/apim-cli/issues/235", ErrorCode.UNSUPPORTED_API_SPECIFICATION);
}
return false;
}
@Override
public boolean parse(byte[] apiSpecificationContent) throws AppException {
if (apiSpecificationContent.length < 500)
return false;
String specStart = new String(apiSpecificationContent, 0, 500).toLowerCase();
if (specStart.contains("edmx") && specStart.contains("3.0")) {
throw new AppException("Detected OData V3 specification, which is not yet supported by the APIM-CLI.\n"
+ " | If you have a need for OData V3 support please upvote the following issue:\n"
+ " | https://github.com/Axway-API-Management-Plus/apim-cli/issues/235", ErrorCode.UNSUPPORTED_API_SPECIFICATION);
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public boolean parse(byte[] apiSpecificationContent) throws AppException {
if (apiSpecificationFile.toLowerCase().endsWith(".url")) {
apiSpecificationFile = Utils.getAPIDefinitionUriFromFile(apiSpecificationFile);
}
if(apiSpecificationContent.length < 500)
return false;
if (!apiSpecificationFile.toLowerCase().endsWith(".wadl") && !new String(this.apiSpecificationContent, 0, 500).contains("wadl.dev.java.net")) {
LOG.debug("No WADL specification. Specification doesn't contain WADL namespace: wadl.dev.java.net in the first 500 characters.");
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,62 +27,57 @@
import java.security.NoSuchAlgorithmException;

public class HTTPClient implements AutoCloseable {
private static final Logger LOG = LoggerFactory.getLogger(HTTPClient.class);
private static final Logger LOG = LoggerFactory.getLogger(HTTPClient.class);
private final URI url;
private final String password;
private final String username;
private CloseableHttpClient closeableHttpClient = null;
private HttpClientContext clientContext;

private final URI url;
private final String password;
private final String username;

private CloseableHttpClient closeableHttpClient = null;

private HttpClientContext clientContext;

public HTTPClient(String url, String username, String password) throws AppException {
try {
this.url = new URI(url);
this.password = password;
this.username = username;
getClient();
} catch (URISyntaxException e) {
throw new AppException("Error creating HTTP-Client.", ErrorCode.UNXPECTED_ERROR, e);
}
}
public HTTPClient(String url, String username, String password) throws AppException {
try {
this.url = new URI(url);
this.password = password;
this.username = username;
getClient();
} catch (URISyntaxException e) {
throw new AppException("Error creating HTTP-Client.", ErrorCode.UNXPECTED_ERROR, e);
}
}

public void getClient() throws AppException {
try {
SSLContextBuilder builder = SSLContextBuilder.create();
builder.loadTrustMaterial(null, new TrustAllStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build(), new NoopHostnameVerifier());
HttpClientBuilder httpClientBuilder = HttpClients.custom()
.setSSLSocketFactory(sslsf);

if(this.username!=null) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
authCache.put( new HttpHost(url.getHost(), url.getPort(), url.getScheme()), basicAuth );
clientContext = HttpClientContext.create();
clientContext.setAuthCache(authCache);
httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
}
this.closeableHttpClient = httpClientBuilder.build();
} catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
throw new AppException("Error creating HTTP-Client.", ErrorCode.UNXPECTED_ERROR, e);
}
}

public CloseableHttpResponse execute(HttpUriRequest request) throws IOException {
return closeableHttpClient.execute(request, clientContext);
}
public void getClient() throws AppException {
try {
SSLContextBuilder builder = SSLContextBuilder.create();
builder.loadTrustMaterial(null, new TrustAllStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build(), new NoopHostnameVerifier());
HttpClientBuilder httpClientBuilder = HttpClients.custom()
.setSSLSocketFactory(sslsf);
if (this.username != null) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
authCache.put(new HttpHost(url.getHost(), url.getPort(), url.getScheme()), basicAuth);
clientContext = HttpClientContext.create();
clientContext.setAuthCache(authCache);
httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
}
this.closeableHttpClient = httpClientBuilder.build();
} catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
throw new AppException("Error creating HTTP-Client.", ErrorCode.UNXPECTED_ERROR, e);
}
}

public CloseableHttpResponse execute(HttpUriRequest request) throws IOException {
return closeableHttpClient.execute(request, clientContext);
}

@Override
public void close() throws Exception {
try {
this.closeableHttpClient.close();
} catch (IOException e) {
LOG.error("error closing http client", e);
}
}
@Override
public void close() throws Exception {
try {
this.closeableHttpClient.close();
} catch (IOException e) {
LOG.error("error closing http client", e);
}
}
}
Loading

0 comments on commit ecebfc3

Please sign in to comment.