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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ script:
- echo "Running unit and controller tests for 'h2,public,simple-cache' spring profile"
- mvn clean verify -Ppostgres -B
- echo "Running unit and controller tests for 'h2,public,simple-cache,titan' spring profile"
- mvn verify deploy -s acs-ci-config/mvn_settings.xml -Pwith.jacoco -Dartifactory.password=${ARTIFACTORY_PASSWORD} -B
- mvn verify deploy -s acs-ci-config/mvn_settings.xml -Pwith.jacoco,predix -Dartifactory.password=${ARTIFACTORY_PASSWORD} -B
- tar zcvf acs-model-jacoco.tar.gz model/target/site/jacoco
- tar zcvf acs-commons-jacoco.tar.gz commons/target/site/jacoco
- tar zcvf acs-service-jacoco.tar.gz service/target/site/jacoco
2 changes: 1 addition & 1 deletion acs-integration-tests/src/test/resources/log4j.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<!-- Introducing logback.xml for acs-int-tests package. Adding log level
control for class ResourceOwnerPasswordAccessTokenProvider to avoid printing
password details in log file -->
<log4j:configuration debug="true"
<log4j:configuration debug="false"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<spring-cloud.version>1.0.5.RELEASE</spring-cloud.version>
<spring-cloud-netflix.version>1.0.7.RELEASE</spring-cloud-netflix.version>
<spring-data-redis.version>1.6.4.RELEASE</spring-data-redis.version>
<spring-log-filter.version>1.0.1</spring-log-filter.version>
<spring-log-filter.version>1.0.2</spring-log-filter.version>
<springfox.swagger.version>2.2.2</springfox.swagger.version>
<google.guava.version>18.0</google.guava.version>
<swagger.annotations>1.5.0</swagger.annotations>
Expand All @@ -71,7 +71,7 @@
<openjpa.version>2.4.0</openjpa.version>
<postgres.version>9.3-1103-jdbc4</postgres.version>
<uaa-token-lib.version>3.2.2</uaa-token-lib.version>
<audit-sdk.version>0.0.3-SNAPSHOT</audit-sdk.version>
<audit-sdk.version>0.0.4-SNAPSHOT</audit-sdk.version>

<!-- Test Dependency Version -->
<mockito.version>1.9.0</mockito.version>
Expand Down
29 changes: 29 additions & 0 deletions service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,24 @@
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<excludes>
<exclude>com/ge/predix/acs/audit/*</exclude>
</excludes>
<testExcludes>
<testExclude>com/ge/predix/acs/audit/*</testExclude>
</testExcludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>predix</id>
Expand All @@ -498,6 +516,11 @@
<artifactId>audit-sdk</artifactId>
<version>${audit-sdk.version}</version>
</dependency>
<dependency>
<groupId>com.ge.predix.eventhub</groupId>
<artifactId>predix-event-hub-sdk</artifactId>
<version>1.1.6-SNAPSHOT</version>
</dependency>
</dependencies>
</profile>
<profile>
Expand Down Expand Up @@ -553,6 +576,12 @@
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<excludes>
<exclude>com/ge/predix/acs/audit/*</exclude>
</excludes>
<testExcludes>
<testExclude>com/ge/predix/acs/audit/*</testExclude>
</testExcludes>
</configuration>
</plugin>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.ge.predix.acs.audit;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ge.predix.audit.AuditEvent;
import com.ge.predix.audit.sdk.message.AuditEnums.CategoryType;
import com.ge.predix.audit.sdk.message.AuditEnums.Classifier;
import com.ge.predix.audit.sdk.message.AuditEnums.EventType;
import com.ge.predix.audit.sdk.message.AuditEnums.PublisherType;
import com.ge.predix.audit.sdk.message.AuditEventV2;
import com.ge.predix.audit.sdk.message.AuditEventV2.AuditEventV2Builder;

@Component
@Profile("predix")
public class PredixEventMapper {

private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(PredixEventProcessor.class);

private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

public AuditEventV2 map(final AuditEvent auditEvent) {
Map<String, String> auditPayload = new HashMap<>();
auditPayload.put("httpMethod", auditEvent.getMethod());
auditPayload.put("requestBody", auditEvent.getRequestBody());
auditPayload.put("responseBody", auditEvent.getResponseBody());

String payload = "";
try {
payload = OBJECT_MAPPER.writeValueAsString(auditPayload);
} catch (JsonProcessingException e) {
LOGGER.warn("Unable to convert audit payload to json: " + auditPayload.toString());
}

AuditEventV2Builder auditEventBuilder = AuditEventV2.builder().timestamp(auditEvent.getTime().toEpochMilli())
.correlationId(auditEvent.getCorrelationId()).tenantUuid(auditEvent.getZoneId())
.publisherType(PublisherType.APP_SERVICE).categoryType(CategoryType.API_CALLS).payload(payload);

if (isSuccessful(auditEvent)) {
auditEventBuilder = auditEventBuilder.eventType(EventType.SUCCESS_API_REQUEST)
.classifier(Classifier.SUCCESS);
} else {
auditEventBuilder = auditEventBuilder.eventType(EventType.FAILURE_API_REQUEST)
.classifier(Classifier.FAILURE);
}

return auditEventBuilder.build();
}

private boolean isSuccessful(final AuditEvent auditEvent) {
return HttpStatus.valueOf(auditEvent.getStatus()).is2xxSuccessful();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.ge.predix.acs.audit;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.ge.predix.audit.AuditEvent;
import com.ge.predix.audit.AuditEventProcessor;
import com.ge.predix.audit.sdk.AuditClient;
import com.ge.predix.audit.sdk.exception.AuditException;
import com.ge.predix.audit.sdk.message.AuditEventV2;

public class PredixEventProcessor implements AuditEventProcessor {

private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(PredixEventProcessor.class);

@Autowired
private AuditClient auditClient;

@Autowired
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This coincidentally works since PredixEventProcessor and PredixEventMapper are part of the same package, but it might break if trying to wire an instance of PredixEventMapper elsewhere. Perhaps add an @Component annotation to the PredixEventMapper class above?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has not been addressed, frank can you please comment?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anurag-gujral, @FrankGasparovic has addressed this above.

private PredixEventMapper eventMapper;

@Override
public boolean process(final AuditEvent auditEvent) {
AuditEventV2 predixEvent = eventMapper.map(auditEvent);
try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need proper exception handling rather than catching the exception and returning boolean

Copy link
Author

@FrankGasparovic FrankGasparovic Feb 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Boolean is required because of the way we created the tests for the AuditEventProcessor. Also we shouldn't add the throws at the interface level because different implementations of AuditEventProcessor may not use the Predix-Audit SDK.

this.auditClient.audit(predixEvent);
} catch (AuditException e) {
LOGGER.warn("Audit failed on process with event: " + predixEvent.toString());
return false;
}
return true;
}

}
2 changes: 1 addition & 1 deletion service/src/main/resources/log4j.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
limitations under the License.
-->
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
<log4j:configuration debug="false"
xmlns:log4j='http://jakarta.apache.org/log4j/'>

<appender name="console" class="org.apache.log4j.ConsoleAppender">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.ge.predix.acs.audit;

import java.io.IOException;

import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.util.ContentCachingRequestWrapper;
import org.springframework.web.util.ContentCachingResponseWrapper;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.ge.predix.audit.AuditEvent;
import com.ge.predix.audit.sdk.message.AuditEnums.CategoryType;
import com.ge.predix.audit.sdk.message.AuditEnums.Classifier;
import com.ge.predix.audit.sdk.message.AuditEnums.EventType;
import com.ge.predix.audit.sdk.message.AuditEnums.PublisherType;
import com.ge.predix.audit.sdk.message.AuditEventV2;

public class PredixEventMapperTest {

// CHECKSTYLE:OFF: MagicNumber
@Test(dataProvider = "predixMapperDataProvider")
public void testPredixAuditEventMapper(final String requestBody, final String methodType, final int status,
final String responseBody, final String zoneId, final String correlationId,
final PublisherType publisherType, final CategoryType categoryType, final EventType eventType,
final Classifier classifier) throws IOException {

PredixEventMapper mapper = new PredixEventMapper();

// setup mocked request and response
MockHttpServletRequest request = new MockHttpServletRequest();
request.setContent(requestBody.getBytes());
MockHttpServletResponse response = new MockHttpServletResponse();
request.setMethod(methodType);
request.setRequestURI("not-used");
response.setStatus(status);

ContentCachingRequestWrapper cachedRequest = new ContentCachingRequestWrapper(request);
ContentCachingResponseWrapper cachedResponse = new ContentCachingResponseWrapper(response);
cachedResponse.getWriter().write(responseBody);
cachedRequest.getReader().readLine();

AuditEvent event = new AuditEvent(cachedRequest, cachedResponse, zoneId, correlationId);
AuditEventV2 predixEvent = mapper.map(event);

Assert.assertTrue(predixEvent.getPayload().contains(requestBody));
Assert.assertTrue(predixEvent.getPayload().contains(methodType));
Assert.assertTrue(predixEvent.getPayload().contains(responseBody));
Assert.assertEquals(predixEvent.getPublisherType(), publisherType);
Assert.assertEquals(predixEvent.getCategoryType(), categoryType);
Assert.assertEquals(predixEvent.getEventType(), eventType);
Assert.assertEquals(predixEvent.getClassifier(), classifier);
}
// CHECKSTYLE:ON: MagicNumber

@DataProvider
public Object[][] predixMapperDataProvider() {
return new Object[][] {
new Object[] { "request body", "POST", 401, "response body", "1234", "5678", PublisherType.APP_SERVICE,
CategoryType.API_CALLS, EventType.FAILURE_API_REQUEST, Classifier.FAILURE },
{ "request body", "PUT", 200, "response body", "9101112", "13141516", PublisherType.APP_SERVICE,
CategoryType.API_CALLS, EventType.SUCCESS_API_REQUEST, Classifier.SUCCESS } };
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably add at least one test for auditing input validation failures: { ... , PublisherType.APP_SERVICE, CategoryType.MALICIOUS, EventType.INVALID_INPUTS, Classifier.FAILURE }

If it's outside the scope of the current release, we should at least create a story to track this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added story for this in audit feature

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The story for CategoryType.MALICIOUS /input validation has been added to the backlog.


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.ge.predix.acs.audit;

import static org.mockito.Matchers.anyObject;

import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.ge.predix.audit.AuditEvent;
import com.ge.predix.audit.sdk.AuditClient;
import com.ge.predix.audit.sdk.exception.AuditException;
import com.ge.predix.audit.sdk.message.AuditEventV2;
import com.ge.predix.eventhub.EventHubClientException;

public class PredixEventProcessorTest {

@InjectMocks
private PredixEventProcessor eventProcessor;

@Mock
private AuditClient mockedClient;

@Mock
private PredixEventMapper mockedMapper;

@BeforeMethod
public void setup() throws AuditException, EventHubClientException {
MockitoAnnotations.initMocks(this);
}

@Test
public void testPredixEventProcess() throws AuditException, EventHubClientException {
AuditEvent mockAuditEvent = Mockito.mock(AuditEvent.class);
AuditEventV2 mockPredixEvent = Mockito.mock(AuditEventV2.class);
Mockito.when(mockedMapper.map(anyObject())).thenReturn(mockPredixEvent);

Assert.assertTrue(this.eventProcessor.process(mockAuditEvent));
Mockito.verify(mockedClient).audit(mockPredixEvent);
}

}