Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ service/pom.xml.versionsBackup
pom.xml.versionsBackup
model/pom.xml.versionsBackup
commons/pom.xml.versionsBackup
*
**/surefire~
29 changes: 13 additions & 16 deletions acs-integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -379,32 +379,29 @@
<appStartupTimeout>120</appStartupTimeout>
<org>${cf.org}</org>
<space>${cf.space}</space>
<appname>${acs.appname}</appname>
<memory>1024</memory>
<buildpack>java-buildpack</buildpack>
<env>
<SPRING_PROFILES_ACTIVE>integration,predix,cloud-redis,cloudDbConfig</SPRING_PROFILES_ACTIVE>
</env>
<services>
<service>
<name>${acs.db}</name>
<label>postgres</label>
<plan>shared-nr</plan>
</service>
<service>
<name>${acs.redis}</name>
<label>redis</label>
<plan>shared-vm</plan>
</service>
<!-- <service> -->
<!-- <name>acs-logstash</name> -->
<!-- </service> -->
</services>
</configuration>
<executions>
<execution>
<id>push</id>
<configuration>
<appname>${acs.appname}</appname>
<services>
<service>
<name>${acs.db}</name>
<label>postgres</label>
<plan>shared-nr</plan>
</service>
<service>
<name>${acs.redis}</name>
<label>redis</label>
<plan>shared-vm</plan>
</service>
</services>
<path>${local.acs.artifact}</path>
<artifact>${maven.acs.artifact}</artifact>
<urls>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
package com.ge.predix.integration.test;

import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.ge.predix.acs.model.Effect;
import com.ge.predix.acs.rest.AttributeAdapterConnection;
import com.ge.predix.acs.rest.AttributeConnector;
import com.ge.predix.acs.rest.PolicyEvaluationRequestV1;
import com.ge.predix.acs.rest.PolicyEvaluationResult;
import com.ge.predix.acs.rest.Zone;
import com.ge.predix.test.utils.ACSRestTemplateFactory;
import com.ge.predix.test.utils.PolicyHelper;
import com.ge.predix.test.utils.UaaTestUtil;
import com.ge.predix.test.utils.ZacTestUtil;
import com.ge.predix.test.utils.ZoneHelper;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

@ContextConfiguration("classpath:integration-test-spring-context.xml")
public class PolicyEvaluationWithAttributeConnectorIT extends AbstractTestNGSpringContextTests {

@Value("${ADAPTER_ENDPOINT}")
private String adapterEndpoint;

@Value("${ZONE1_NAME:testzone1}")
private String acsZone1Name;

@Value("${ACS_UAA_URL}")
private String acsUaaUrl;

@Value("${ASSET_TOKEN_URL}")
private String assetTokenUrl;

@Value("${ASSET_CLIENT_ID}")
private String assetClientId;

@Value("${ASSET_CLIENT_SECRET}")
private String assetClientSecret;

@Value("${ASSET_ZONE_ID}")
private String assetZoneId;

@Value("${ASSET_URL}")
private String assetUrl;

@Value("${ADAPTER_UAA_TOKEN_URL:${ASSET_TOKEN_URL}}")
private String adapterUaaTokenUrl;

@Value("${ADAPTER_UAA_CLIENT_ID:${ASSET_CLIENT_ID}}")
private String adapterUaaClientId;

@Value("${ADAPTER_UAA_CLIENT_SECRET:${ASSET_CLIENT_SECRET}}")
private String adapterUaaClientSecret;

@Autowired
private Environment environment;

@Autowired
private ACSRestTemplateFactory acsRestTemplateFactory;

@Autowired
private ZacTestUtil zacTestUtil;

@Autowired
private ZoneHelper zoneHelper;

@Autowired
private PolicyHelper policyHelper;

private static final String TEST_PART_ID = "part/03f95db1-4255-4265-a509-f7bca3e1fee4";
private static final String ASSET_URI_PATH_SEGMENT = '/' + TEST_PART_ID;

private Zone zone;
private OAuth2RestTemplate acsAdminRestTemplate;
private OAuth2RestTemplate assetRestTemplate;
private boolean registerWithZac;
private URI resourceAttributeConnectorUrl;

private HttpHeaders zoneHeader() throws IOException {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set(PolicyHelper.PREDIX_ZONE_ID, this.zone.getSubdomain());
return httpHeaders;
}

private HttpHeaders assetZoneHeader() throws IOException {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set(PolicyHelper.PREDIX_ZONE_ID, this.assetZoneId);
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
return httpHeaders;
}

private void createAssetRestTemplate() {
ClientCredentialsResourceDetails clientCredentials = new ClientCredentialsResourceDetails();
clientCredentials.setAccessTokenUri(this.assetTokenUrl);
clientCredentials.setClientId(this.assetClientId);
clientCredentials.setClientSecret(this.assetClientSecret);
this.assetRestTemplate = new OAuth2RestTemplate(clientCredentials);

CloseableHttpClient httpClient = HttpClientBuilder.create().useSystemProperties().build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
this.assetRestTemplate.setRequestFactory(requestFactory);
}

private void configureMockAssetData() throws IOException {
this.createAssetRestTemplate();

JsonObject part = new JsonObject();
part.addProperty("id", "03f95db1-4255-4265-a509-f7bca3e1fee4");
part.addProperty("collection", "part");
part.addProperty("partModel", "/partmodels/9a92831d-42f1-4f9e-86bf-4c0914f481e4");
part.addProperty("structureModel", "/structureModels/8c787978-bd8b-417a-b759-f63a8a6d43ee");
part.addProperty("serialNumber", "775277328");
part.addProperty("parent", "/part/01af94ed-5425-44e4-9f6e-2a58cba7b559");
part.addProperty("aircraftPart", "/aircraftPart/13a71359-db68-4602-aac5-a8fa401c3194");
part.addProperty("aircraftPartModel", "/aircraftPartModels/1dc6a36d-a24e-4fec-a181-f576c95a8104");
part.addProperty("uri", ASSET_URI_PATH_SEGMENT);

JsonArray partArray = new JsonArray();
partArray.add(part);

this.assetRestTemplate.exchange(this.assetUrl + "/part", HttpMethod.POST,
new HttpEntity<>(partArray.toString(), assetZoneHeader()), String.class);
}

private void configureAttributeConnector(final boolean isActive) throws IOException {

List<AttributeAdapterConnection> adapters = Collections.singletonList(new AttributeAdapterConnection(
this.adapterEndpoint, this.adapterUaaTokenUrl, this.adapterUaaClientId, this.adapterUaaClientSecret));

AttributeConnector attributeConnector = new AttributeConnector();
attributeConnector.setIsActive(isActive);
attributeConnector.setAdapters(new HashSet<>(adapters));
this.acsAdminRestTemplate.exchange(this.resourceAttributeConnectorUrl, HttpMethod.PUT,
new HttpEntity<>(attributeConnector, zoneHeader()), AttributeConnector.class);
}

private void setupPredixAcs() throws IOException {
this.zacTestUtil.assumeZacServerAvailable();
this.acsAdminRestTemplate = this.acsRestTemplateFactory.getACSTemplateWithPolicyScope();
this.registerWithZac = true;

}

private void setupPublicAcs() throws IOException {
UaaTestUtil uaaTestUtil = new UaaTestUtil(this.acsRestTemplateFactory.getOAuth2RestTemplateForUaaAdmin(),
this.acsUaaUrl);
uaaTestUtil.setup(Collections.singletonList(this.acsZone1Name));
this.acsAdminRestTemplate = this.acsRestTemplateFactory.getOAuth2RestTemplateForAcsAdmin();
this.registerWithZac = false;
}

@BeforeClass
void beforeClass() throws IOException {
if (Arrays.asList(this.environment.getActiveProfiles()).contains("public")) {
setupPublicAcs();
} else {
setupPredixAcs();
}

this.zone = this.zoneHelper.createTestZone(this.acsAdminRestTemplate, this.acsZone1Name, this.registerWithZac);
this.resourceAttributeConnectorUrl = URI.create(this.zoneHelper.getAcsBaseURL() + "/v1/connector/resource");
this.configureMockAssetData();
}

private void deconfigureMockAssetData() throws IOException {
this.assetRestTemplate.exchange(this.assetUrl + ASSET_URI_PATH_SEGMENT, HttpMethod.DELETE,
new HttpEntity<>(assetZoneHeader()), Void.class);
}

private void deconfigureAttributeConnector() throws IOException {
this.acsAdminRestTemplate.exchange(this.resourceAttributeConnectorUrl, HttpMethod.DELETE,
new HttpEntity<>(zoneHeader()), Void.class);
}

@AfterClass
void afterClass() throws IOException {
this.deconfigureMockAssetData();
this.zoneHelper.deleteZone(this.acsAdminRestTemplate, this.acsZone1Name, this.registerWithZac);
}

@Test(dataProvider = "adapterStatusesAndResultingEffects")
public void testPolicyEvaluationWithAdapters(final boolean adapterActive, final Effect expectedEffect)
throws Exception {
String testPolicyName = this.policyHelper.setTestPolicy(this.acsAdminRestTemplate, zoneHeader(),
this.zoneHelper.getAcsBaseURL(),
"src/test/resources/policy-set-with-one-policy-using-resource-attributes-from-asset-adapter.json");

try {
this.configureAttributeConnector(adapterActive);
PolicyEvaluationRequestV1 policyEvaluationRequest = this.policyHelper.createEvalRequest("GET",
"testSubject", TEST_PART_ID, null);
ResponseEntity<PolicyEvaluationResult> policyEvaluationResponse = this.acsAdminRestTemplate.postForEntity(
this.zoneHelper.getAcsBaseURL() + PolicyHelper.ACS_POLICY_EVAL_API_PATH,
new HttpEntity<>(policyEvaluationRequest, zoneHeader()), PolicyEvaluationResult.class);
Assert.assertEquals(policyEvaluationResponse.getStatusCode(), HttpStatus.OK);
PolicyEvaluationResult policyEvaluationResult = policyEvaluationResponse.getBody();
Assert.assertEquals(policyEvaluationResult.getEffect(), expectedEffect);
} finally {
this.policyHelper.deletePolicySet(this.acsAdminRestTemplate, this.zoneHelper.getAcsBaseURL(),
testPolicyName, zoneHeader());
this.deconfigureAttributeConnector();
}
}

@DataProvider
private Object[][] adapterStatusesAndResultingEffects() {
return new Object[][] { { true, Effect.PERMIT }, { false, Effect.NOT_APPLICABLE } };
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "test-policy-set-with-asset-attributes",
"policies": [
{
"name": "Policy for a part",
"conditions": [
{
"name": "has a specific id",
"condition": "match.single(resource.attributes('https://not/set/yet', 'id'), '03f95db1-4255-4265-a509-f7bca3e1fee4')"
},
{
"name": "is part of a specific collection",
"condition": "match.single(resource.attributes('https://not/set/yet', 'collection'), 'part')"
},
{
"name": "belongs to a specific model",
"condition": "match.single(resource.attributes('https://not/set/yet', 'partModel'), '/partmodels/9a92831d-42f1-4f9e-86bf-4c0914f481e4')"
},
{
"name": "belongs to a specific structural model",
"condition": "match.single(resource.attributes('https://not/set/yet', 'structureModel'), '/structureModels/8c787978-bd8b-417a-b759-f63a8a6d43ee')"
},
{
"name": "has a specific serial number",
"condition": "match.single(resource.attributes('https://not/set/yet', 'serialNumber'), '775277328')"
},
{
"name": "has a specific parent",
"condition": "match.single(resource.attributes('https://not/set/yet', 'parent'), '/part/01af94ed-5425-44e4-9f6e-2a58cba7b559')"
},
{
"name": "belongs to a specific aircraft part",
"condition": "match.single(resource.attributes('https://not/set/yet', 'aircraftPart'), '/aircraftPart/13a71359-db68-4602-aac5-a8fa401c3194')"
},
{
"name": "belongs to a specific aircraft part model",
"condition": "match.single(resource.attributes('https://not/set/yet', 'aircraftPartModel'), '/aircraftPartModels/1dc6a36d-a24e-4fec-a181-f576c95a8104')"
},
{
"name": "has a specific uri",
"condition": "match.single(resource.attributes('https://not/set/yet', 'uri'), '/part/03f95db1-4255-4265-a509-f7bca3e1fee4')"
}
],
"effect": "PERMIT"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ public AttributeAdapterConnection(final String adapterEndpoint, final String uaa
this.uaaClientSecret = uaaClientSecret;
}

public AttributeAdapterConnection(final AttributeAdapterConnection other) {
this.adapterEndpoint = other.adapterEndpoint;
this.uaaTokenUrl = other.uaaTokenUrl;
this.uaaClientId = other.uaaClientId;
this.uaaClientSecret = other.uaaClientSecret;
}

public String getAdapterEndpoint() {
return adapterEndpoint;
}
Expand Down
Loading