diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index fb74f83cfc88d38851e5bb91902a6f18b7d76991..eadecd7fa0f3abf5afadedb13c08f5f0cd2239b5 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,4 +1,5 @@ variables: + CORE_BUILD_SUBDIR: partition-core AWS_BUILD_SUBDIR: provider/partition-aws/build-aws AWS_TEST_SUBDIR: testing/partition-test-aws diff --git a/partition-core/pom.xml b/partition-core/pom.xml index 23ad995361f4c74a755cd8cca26886f0b7682a26..d69af7d3788463149604a8cf8756d1f0d9a198c5 100644 --- a/partition-core/pom.xml +++ b/partition-core/pom.xml @@ -178,6 +178,31 @@ </dependency> </dependencies> </plugin> + <plugin> + <groupId>org.jacoco</groupId> + <artifactId>jacoco-maven-plugin</artifactId> + <version>0.8.8</version> + <configuration> + <excludes> + <exclude>org/opengroup/osdu/partition/model/*</exclude> + <exclude>**/**Configuration*</exclude> + </excludes> + </configuration> + <executions> + <execution> + <goals> + <goal>prepare-agent</goal> + </goals> + </execution> + <execution> + <id>report</id> + <phase>prepare-package</phase> + <goals> + <goal>report</goal> + </goals> + </execution> + </executions> + </plugin> </plugins> </build> </project> diff --git a/partition-core/src/test/java/org/opengroup/osdu/partition/controller/HealthCheckControllerTests.java b/partition-core/src/test/java/org/opengroup/osdu/partition/controller/HealthCheckControllerTests.java new file mode 100644 index 0000000000000000000000000000000000000000..5727cebbe2e5a094b9a22f0a1506b2b61e66cc81 --- /dev/null +++ b/partition-core/src/test/java/org/opengroup/osdu/partition/controller/HealthCheckControllerTests.java @@ -0,0 +1,23 @@ +package org.opengroup.osdu.partition.controller; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.http.HttpStatus; + +import static org.junit.Assert.assertEquals; + +public class HealthCheckControllerTests { + + private HealthCheckController sut; + + @Before + public void setup() { + this.sut = new HealthCheckController(); + } + + @Test + public void should_returnHttp200_when_checkLiveness() { + assertEquals(HttpStatus.OK, this.sut.livenessCheck().getStatusCode()); + } + +} diff --git a/partition-core/src/test/java/org/opengroup/osdu/partition/controller/InfoControllerTest.java b/partition-core/src/test/java/org/opengroup/osdu/partition/controller/InfoControllerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..38e351879862f169a1e2fd7ddb91e2e91cf92170 --- /dev/null +++ b/partition-core/src/test/java/org/opengroup/osdu/partition/controller/InfoControllerTest.java @@ -0,0 +1,42 @@ +package org.opengroup.osdu.partition.controller; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.opengroup.osdu.core.common.info.VersionInfoBuilder; +import org.opengroup.osdu.core.common.model.info.VersionInfo; + +import java.io.IOException; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; + + +@RunWith(MockitoJUnitRunner.class) +public class InfoControllerTest { + @InjectMocks + private InfoController sut; + + @Mock + private VersionInfoBuilder versionInfoBuilder; + + @Test + public void should_return200_getVersionInfo() throws IOException { + VersionInfo expectedVersionInfo = VersionInfo.builder() + .groupId("group") + .artifactId("artifact") + .version("0.1.0") + .buildTime("1000") + .branch("master") + .commitId("7777") + .commitMessage("Test commit") + .build(); + when(versionInfoBuilder.buildVersionInfo()).thenReturn(expectedVersionInfo); + + VersionInfo actualVersionInfo = this.sut.info(); + + assertEquals(expectedVersionInfo, actualVersionInfo); + } +} diff --git a/partition-core/src/test/java/org/opengroup/osdu/partition/logging/AuditEventsTest.java b/partition-core/src/test/java/org/opengroup/osdu/partition/logging/AuditEventsTest.java new file mode 100644 index 0000000000000000000000000000000000000000..8225e0d369b08df0bee045eb5172e20e3faab0bf --- /dev/null +++ b/partition-core/src/test/java/org/opengroup/osdu/partition/logging/AuditEventsTest.java @@ -0,0 +1,33 @@ +package org.opengroup.osdu.partition.logging; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThrows; + +public class AuditEventsTest { + + @Test + public void shouldInitializeAuditEvents_whenUserIsValid() { + AuditEvents auditEvents = new AuditEvents("testUser"); + + assertNotNull(auditEvents); + } + + @Test + public void shouldThrowIllegalArgumentException_whenAuditEvents_initializedWithEmptyUser() { + IllegalArgumentException illegalArgumentException = assertThrows(IllegalArgumentException.class, + () -> new AuditEvents("")); + + assertEquals("User not provided for audit events.", illegalArgumentException.getMessage()); + } + + @Test + public void shouldThrowIllegalArgumentException_whenAuditEvents_initializedWithNullUser() { + IllegalArgumentException illegalArgumentException = assertThrows(IllegalArgumentException.class, + () -> new AuditEvents(null)); + + assertEquals("User not provided for audit events.", illegalArgumentException.getMessage()); + } +} diff --git a/partition-core/src/test/java/org/opengroup/osdu/partition/logging/AuditLoggerTest.java b/partition-core/src/test/java/org/opengroup/osdu/partition/logging/AuditLoggerTest.java index 827c7c9a3244d76fdec73aea266f06284acb010f..fa93dd275501b07eecc609682988463d35bda743 100644 --- a/partition-core/src/test/java/org/opengroup/osdu/partition/logging/AuditLoggerTest.java +++ b/partition-core/src/test/java/org/opengroup/osdu/partition/logging/AuditLoggerTest.java @@ -17,12 +17,6 @@ package org.opengroup.osdu.partition.logging; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; - -import java.util.Collections; -import java.util.List; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -30,104 +24,125 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; +import org.springframework.test.util.ReflectionTestUtils; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; @RunWith(MockitoJUnitRunner.class) public class AuditLoggerTest { - @Mock - private JaxRsDpsLog log; + @Mock + private JaxRsDpsLog log; + + @InjectMocks + private AuditLogger sut; + + private List<String> resources; - @InjectMocks - private AuditLogger sut; + @Before + public void setup() { + resources = Collections.singletonList("resources"); + } - private List<String> resources; + @Test + public void should_writeCreatePartitionSuccessEvent() { + this.sut.createPartitionSuccess(this.resources); - @Before - public void setup() { - resources = Collections.singletonList("resources"); - } + verify(this.log, times(1)).audit(any()); + } - @Test - public void should_writeCreatePartitionSuccessEvent() { - this.sut.createPartitionSuccess(this.resources); + @Test + public void should_writeCreatePartitionFailureEvent() { + this.sut.createPartitionFailure(this.resources); - verify(this.log, times(1)).audit(any()); - } + verify(this.log, times(1)).audit(any()); + } - @Test - public void should_writeCreatePartitionFailureEvent() { - this.sut.createPartitionFailure(this.resources); + @Test + public void should_writeReadPartitionSuccessEvent() { + this.sut.readPartitionSuccess(this.resources); - verify(this.log, times(1)).audit(any()); - } + verify(this.log, times(1)).audit(any()); + } - @Test - public void should_writeReadPartitionSuccessEvent() { - this.sut.readPartitionSuccess(this.resources); + @Test + public void should_writeReadPartitionFailureEvent() { + this.sut.readPartitionFailure(this.resources); - verify(this.log, times(1)).audit(any()); - } + verify(this.log, times(1)).audit(any()); + } - @Test - public void should_writeReadPartitionFailureEvent() { - this.sut.readPartitionFailure(this.resources); + @Test + public void should_writeDeletePartitionSuccessEvent() { + this.sut.deletePartitionSuccess(this.resources); - verify(this.log, times(1)).audit(any()); - } + verify(this.log, times(1)).audit(any()); + } - @Test - public void should_writeDeletePartitionSuccessEvent() { - this.sut.deletePartitionSuccess(this.resources); + @Test + public void should_writeDeletePartitionFailureEvent() { + this.sut.deletePartitionFailure(this.resources); - verify(this.log, times(1)).audit(any()); - } + verify(this.log, times(1)).audit(any()); + } - @Test - public void should_writeDeletePartitionFailureEvent() { - this.sut.deletePartitionFailure(this.resources); + @Test + public void should_writeReadServiceLivenessSuccessEvent() { + this.sut.readServiceLivenessSuccess(this.resources); - verify(this.log, times(1)).audit(any()); - } + verify(this.log, times(1)).audit(any()); + } - @Test - public void should_writeReadServiceLivenessSuccessEvent() { - this.sut.readServiceLivenessSuccess(this.resources); + @Test + public void should_writeReadServiceLivenessFailureEvent() { + this.sut.readServiceLivenessFailure(this.resources); - verify(this.log, times(1)).audit(any()); - } + verify(this.log, times(1)).audit(any()); + } - @Test - public void should_writeReadServiceLivenessFailureEvent() { - this.sut.readServiceLivenessFailure(this.resources); + @Test + public void should_writeUpdatePartitionSecretSuccessEvent() { + this.sut.updatePartitionSecretSuccess(this.resources); - verify(this.log, times(1)).audit(any()); - } + verify(this.log, times(1)).audit(any()); + } - @Test - public void should_writeUpdatePartitionSecretSuccessEvent() { - this.sut.updatePartitionSecretSuccess(this.resources); + @Test + public void should_writeUpdatePartitionSecretFailureEvent() { + this.sut.updatePartitionSecretFailure(this.resources); - verify(this.log, times(1)).audit(any()); - } + verify(this.log, times(1)).audit(any()); + } - @Test - public void should_writeUpdatePartitionSecretFailureEvent() { - this.sut.updatePartitionSecretFailure(this.resources); + @Test + public void should_writeReadListPartitionSuccessEvent() { + this.sut.readListPartitionSuccess(this.resources); - verify(this.log, times(1)).audit(any()); - } + verify(this.log, times(1)).audit(any()); + } - @Test - public void should_writeReadListPartitionSuccessEvent() { - this.sut.readListPartitionSuccess(this.resources); + @Test + public void should_writeReadListPartitionFailureEvent() { + this.sut.readListPartitionFailure(this.resources); - verify(this.log, times(1)).audit(any()); - } + verify(this.log, times(1)).audit(any()); + } - @Test - public void should_writeReadListPartitionFailureEvent() { - this.sut.readListPartitionFailure(this.resources); + @Test + public void should_initializeAuditEvents_onlyOnce() { + this.sut.readListPartitionFailure(new ArrayList<>()); + Object events1 = ReflectionTestUtils.getField(this.sut, "events"); + this.sut.readListPartitionFailure(this.resources); + Object events2 = ReflectionTestUtils.getField(this.sut, "events"); - verify(this.log, times(1)).audit(any()); - } -} \ No newline at end of file + assertEquals(events1.hashCode(), events2.hashCode()); + verify(this.log, times(2)).audit(any()); + } +}