Skip to content

Commit d3cf7e9

Browse files
complete integration test
1 parent f15172e commit d3cf7e9

11 files changed

+331
-1
lines changed

audit4j-integration-tests/src/main/java/hello/GreetingController.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package hello;
22

3+
import java.math.BigDecimal;
4+
import java.util.Date;
5+
import java.util.List;
6+
37
import org.audit4j.core.annotation.Audit;
48
import org.slf4j.Logger;
59
import org.slf4j.LoggerFactory;
610
import org.springframework.stereotype.Controller;
711
import org.springframework.web.bind.annotation.RequestMapping;
812

13+
import hello.data.User;
14+
915

1016
@Controller
1117
public class GreetingController {
@@ -20,7 +26,7 @@ public String hello() {
2026
}
2127

2228
@Audit
23-
public void foo1(String string1, String string2) {
29+
public void foo1(User[] d1, BigDecimal d2) {
2430
// TODO Auto-generated method stub
2531

2632
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package hello.data;
2+
3+
public class Address {
4+
5+
private String city;
6+
private String postCode;
7+
8+
public String getCity() {
9+
return city;
10+
}
11+
public void setCity(String city) {
12+
this.city = city;
13+
}
14+
public String getPostCode() {
15+
return postCode;
16+
}
17+
public void setPostCode(String postCode) {
18+
this.postCode = postCode;
19+
}
20+
21+
@Override
22+
public String toString() {
23+
return "Address [city=" + city + ", postCode=" + postCode + "]";
24+
}
25+
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package hello.data;
2+
3+
public class User {
4+
5+
private String lastName;
6+
private String firtName;
7+
private Address address;
8+
private Double val =1.0;
9+
10+
public String getLastName() {
11+
return lastName;
12+
}
13+
14+
public void setLastName(String lastName) {
15+
this.lastName = lastName;
16+
}
17+
18+
public String getFirtName() {
19+
return firtName;
20+
}
21+
22+
public void setFirtName(String firtName) {
23+
this.firtName = firtName;
24+
}
25+
26+
public Address getAddress() {
27+
return address;
28+
}
29+
30+
public void setAddress(Address address) {
31+
this.address = address;
32+
}
33+
34+
@Override
35+
public String toString() {
36+
return "User [lastName=" + lastName + ", firtName=" + firtName + ", address=" + address + ", val=" + val + "]";
37+
}
38+
39+
40+
41+
42+
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package hello;
2+
3+
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
import org.junit.Ignore;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.boot.test.context.SpringBootTest;
12+
import org.springframework.test.context.junit4.SpringRunner;
13+
14+
import com.tngtech.jgiven.junit.ScenarioTest;
15+
16+
import hello.data.User;
17+
import hello.stage.GivenSomeState;
18+
import hello.stage.ThenSomeOutcome;
19+
import hello.stage.WhenSomeAction;
20+
21+
@RunWith(SpringRunner.class)
22+
@SpringBootTest
23+
public class SpringBootAuditedTest
24+
extends ScenarioTest<GivenSomeState, WhenSomeAction, ThenSomeOutcome> {
25+
26+
@Autowired
27+
private GreetingController greetingController;
28+
29+
30+
@Ignore
31+
@Test
32+
public void springbootApplicationWithAuditedMethodTest() {
33+
34+
List<User> ints = new ArrayList<User>();
35+
ints.add(new User());
36+
ints.add(new User());
37+
38+
given().spring_boot_application_started_with_audit4j_with_a_programming_configuration_using_ConsoleAuditHandler();
39+
when().the_method_$_annotated_with_audited_is_called_with_the_arguments_$_$_$("foo", new User(),1, ints, greetingController );
40+
then().the_file_log_contains_$_line(1, "getFilePath()");
41+
// .and().the_file_log_contains_the_expected_output_for_event_builder(getFilePath());
42+
43+
}
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package hello.stage;
2+
3+
4+
import com.tngtech.jgiven.Stage;
5+
6+
public class GivenSomeState extends Stage<GivenSomeState> {
7+
8+
public GivenSomeState spring_boot_application_started_with_audit4j_with_a_programming_configuration_using_ConsoleAuditHandler() {
9+
return self();
10+
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package hello.stage;
2+
3+
4+
import com.tngtech.jgiven.Stage;
5+
import com.tngtech.jgiven.annotation.Hidden;
6+
7+
8+
public class ThenSomeOutcome extends Stage<ThenSomeOutcome> {
9+
10+
public ThenSomeOutcome the_file_log_contains_$_line(int i, @Hidden String string) {
11+
return self();
12+
13+
}
14+
15+
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package hello.stage;
2+
3+
4+
import java.math.BigDecimal;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import com.tngtech.jgiven.Stage;
9+
import com.tngtech.jgiven.annotation.Hidden;
10+
import com.tngtech.jgiven.annotation.Quoted;
11+
12+
import hello.GreetingController;
13+
import hello.data.User;
14+
15+
public class WhenSomeAction extends Stage<WhenSomeAction> {
16+
17+
public void the_method_$_annotated_with_audited_is_called_with_the_arguments_$_$_$(
18+
@Quoted String methodName, @Quoted User arg0,
19+
@Quoted int val,
20+
@Quoted List array,
21+
@Hidden GreetingController greetingController) {
22+
23+
User[] ints = new User[2];
24+
ints[0]=new User();
25+
ints[1]=new User();
26+
27+
greetingController.foo1(ints, new BigDecimal(1));
28+
29+
}
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.audit4j.validation.jgiven;
2+
3+
import java.util.Arrays;
4+
5+
import org.audit4j.validation.jgiven.enumeration.Version;
6+
import org.audit4j.validation.jgiven.stage.GivenSomeState;
7+
import org.audit4j.validation.jgiven.stage.ThenSomeOutcome;
8+
import org.audit4j.validation.jgiven.stage.WhenSomeAction;
9+
import org.junit.Ignore;
10+
import org.junit.Rule;
11+
import org.junit.Test;
12+
import org.junit.contrib.java.lang.system.SystemOutRule;
13+
14+
import com.tngtech.jgiven.junit.ScenarioTest;
15+
16+
public class ConsoleAuditHandlerWithFileConfigWithLayoutTest
17+
extends ScenarioTest<GivenSomeState, WhenSomeAction, ThenSomeOutcome> {
18+
19+
@Rule
20+
public final SystemOutRule systemOutRule = new SystemOutRule().enableLog();
21+
22+
private static final String FILE_NAME = "audit4j-console-layout.conf.yml";
23+
24+
@Test
25+
@Ignore // does not work in version 2.5.0
26+
public void console_audit_handler_with_file_configuration_receiving_a_message_with_event_builder() {
27+
28+
given().audit4j_starting_with_the_configuration_defined_in_the_file_$_for_$(FILE_NAME,Arrays.asList("ConsoleAuditHandler"));
29+
when().a_message_is_sent_to_the_audit_manager_with_event_builder()
30+
.and().audit_manager_is_stopped();
31+
then().the_console_log_contains_the_audit4j_logo_with_version_$(systemOutRule, Version.V250);
32+
//.and().the_console_log_contains_the_expected_output_for_event_builder(systemOutRule);
33+
}
34+
35+
/* @Test
36+
public void console_audit_handler_with_file_configuration_receiving_a_message_with_audit_event() {
37+
38+
given().audit4j_starting_with_the_configuration_defined_in_the_file_$_for_$(FILE_NAME,Arrays.asList("ConsoleAuditHandler"));
39+
when().a_message_is_sent_to_the_audit_manager_with_audit_event()
40+
.and().audit_manager_is_stopped();
41+
then().the_console_log_contains_the_audit4j_logo_with_version_$(systemOutRule, Version.V250)
42+
.and().the_console_log_contains_the_expected_output_for_audit_event(systemOutRule);
43+
}
44+
45+
46+
@Test
47+
public void console_audit_handler_with_file_configuration_receiving_a_message_with_event_builder_during_disable_state() {
48+
49+
given().audit4j_starting_with_the_configuration_defined_in_the_file_$_for_$(FILE_NAME,Arrays.asList("ConsoleAuditHandler"));
50+
when().audit_manager_is_disabled()
51+
.and().a_message_is_sent_to_the_audit_manager_with_event_builder()
52+
.and().audit_manager_is_stopped();
53+
then().the_console_log_contains_the_audit4j_logo_with_version_$(systemOutRule, Version.V250)
54+
.but().the_console_log_does_not_contains_messages_for_event_builder(systemOutRule);
55+
56+
}*/
57+
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.audit4j.validation.jgiven;
2+
3+
4+
import org.audit4j.core.layout.CustomizableLayout;
5+
import org.audit4j.core.layout.Layout;
6+
import org.audit4j.validation.jgiven.enumeration.Version;
7+
import org.audit4j.validation.jgiven.stage.GivenSomeState;
8+
import org.audit4j.validation.jgiven.stage.ThenSomeOutcome;
9+
import org.audit4j.validation.jgiven.stage.WhenSomeAction;
10+
import org.junit.Rule;
11+
import org.junit.Test;
12+
import org.junit.contrib.java.lang.system.SystemOutRule;
13+
14+
import com.tngtech.jgiven.junit.ScenarioTest;
15+
16+
17+
18+
public class ConsoleAuditHandlerWithLayoutTest
19+
extends ScenarioTest<GivenSomeState, WhenSomeAction, ThenSomeOutcome> {
20+
21+
@Rule
22+
public final SystemOutRule systemOutRule = new SystemOutRule().enableLog();
23+
24+
25+
private Layout getLayout() {
26+
CustomizableLayout layout = new CustomizableLayout();
27+
layout.setTemplate("${eventDate}|gg|${uuid}|${actor}|${action}|${origin} => ${foreach fields field}[${field.name}:${field.value}]${end}");
28+
29+
return layout;
30+
}
31+
32+
33+
@Test
34+
public void console_audit_handler_with_programming_configuration_receiving_a_message_with_event_builder() {
35+
given().audit4j_starting_with_a_programming_configuration_using_ConsoleAuditHandler_with_layout(getLayout());
36+
when().a_message_is_sent_to_the_audit_manager_with_event_builder()
37+
.and().audit_manager_is_stopped();
38+
then().the_console_log_contains_the_audit4j_logo_with_version_$(systemOutRule, Version.V250);
39+
//.and().the_console_log_contains_the_expected_output_for_event_builder(systemOutRule);
40+
}
41+
/*
42+
@Test
43+
public void console_audit_handler_with_programming_configuration_receiving_a_message_with_audit_event() {
44+
given().audit4j_starting_with_a_programming_configuration_using_ConsoleAuditHandler();
45+
when().a_message_is_sent_to_the_audit_manager_with_audit_event()
46+
.and().audit_manager_is_stopped();
47+
then().the_console_log_contains_the_audit4j_logo_with_version_$(systemOutRule, Version.V250)
48+
.and().the_console_log_contains_the_expected_output_for_audit_event(systemOutRule);
49+
}
50+
51+
@Test
52+
public void console_audit_handler_with_programming_configuration_receiving_a_message_with_event_builder_during_disable_state() {
53+
54+
given().audit4j_starting_with_a_programming_configuration_using_ConsoleAuditHandler();
55+
when().audit_manager_is_disabled()
56+
.and().a_message_is_sent_to_the_audit_manager_with_event_builder()
57+
.and().audit_manager_is_stopped();
58+
then().the_console_log_contains_the_audit4j_logo_with_version_$(systemOutRule, Version.V250)
59+
.but().the_console_log_does_not_contains_messages_for_event_builder(systemOutRule);
60+
}
61+
*/
62+
63+
64+
}

audit4j-integration-tests/src/test/java/org/audit4j/validation/jgiven/stage/GivenSomeState.java

+13
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.audit4j.core.handler.ConsoleAuditHandler;
1212
import org.audit4j.core.handler.Handler;
1313
import org.audit4j.core.handler.file.FileAuditHandler;
14+
import org.audit4j.core.layout.Layout;
1415
import org.audit4j.core.layout.SimpleLayout;
1516
import org.junit.rules.TemporaryFolder;
1617

@@ -69,12 +70,24 @@ private Configuration getConfigurationForConsoleAndFileHandler(TemporaryFolder f
6970
}
7071

7172

73+
74+
7275
public GivenSomeState audit4j_starting_with_a_programming_configuration_using_ConsoleAuditHandler() {
7376
AuditManager.startWithConfiguration(getConfigurationForConsoleHandler());
7477
return self();
7578

7679
}
7780

81+
public GivenSomeState audit4j_starting_with_a_programming_configuration_using_ConsoleAuditHandler_with_layout(Layout layout) {
82+
83+
Configuration configuration = getConfigurationForConsoleHandler();
84+
configuration.setLayout(layout);
85+
86+
AuditManager.startWithConfiguration(configuration);
87+
return self();
88+
89+
}
90+
7891
public GivenSomeState audit4j_starting_with_a_programming_configuration_using_FileAuditHandler(@Hidden TemporaryFolder folder) {
7992
AuditManager.startWithConfiguration(getConfigurationForFileHandler(folder));
8093
return self();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
!Configuration # Mandatory
2+
3+
# Configure handlers, One or more handlers must be configured.
4+
handlers:
5+
- !org.audit4j.core.handler.ConsoleAuditHandler {}
6+
7+
# Configure handlers, Either one handler must be configured.
8+
layout: !org.audit4j.core.layout.CustomizableLayout
9+
dateFormat: yyyy/MM/dd HH:mm:ss
10+
template: ${eventDate}|${uuid}|${actor}|${action}|${origin} => ${foreach fields field}[${field.name}:${field.value}]${end}
11+
12+
# Configure meta data.
13+
metaData: !org.audit4j.core.DummyMetaData {}
14+

0 commit comments

Comments
 (0)