Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wurstbrot committed Jan 30, 2024
1 parent 6ce79a1 commit 1dc98ab
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 112 deletions.
8 changes: 4 additions & 4 deletions definitions/two towers/sauron.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ settings:
application: "sauron" # e.g. from deployment
team: "two towers"

# TODO optional fields results in null values in the output is good?
# Do not modify without testing the code, this file is used in integration tests

activities:
Source Control Protection:
components:
- url: "https://test1.com"
date: 2023-05-01
date: 2023-01-01
- date: 2022-10-01
url: "https://test1.com"
- date: 2021-10-01
- date: 2000-01-01
url: "https://test1.com"
- date: 2023-12-19
url: "https://test1.com"
- date: 2022-11-19
- date: 2022-05-01
url: "https://test2.com"
- date: 2023-12-19
url: "https://test2.com"
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/templates/grafana-dashboard-matrix.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"editable": true,
"fiscalYearStartMonth": 0,
"graphTooltip": 0,
"id": 23,
"links": [],
"liveNow": false,
"panels": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ public void setUp() throws Exception {
constructor.setAccessible(true);
applicationDirector = constructor.newInstance();
YamlScanner yamlScanner = new YamlScanner();
setPrivateField(yamlScanner, "yamlApplicationFolderPath", "definitions");
setPrivateField(yamlScanner, "yamlSkeletonFilePath", "src/main/resources/skeleton.yaml");
setPrivateFieldInTest(yamlScanner, "yamlApplicationFolderPath", "definitions");
setPrivateFieldInTest(yamlScanner, "yamlSkeletonFilePath", "src/main/resources/skeleton.yaml");
setPrivateFieldInTest(yamlScanner, "yamlTeamsFilePath", "src/main/resources/teams.yaml");
yamlScanner.getApplicationYamls();
setPrivateField(this.applicationDirector, "yamlScanner", yamlScanner);
applicationDirector.getApplications();
setPrivateFieldInTest(this.applicationDirector, "yamlScanner", yamlScanner);
applicationDirector.initiateApplicationsViaCron();
}

private void setPrivateField(Object targetObject, String fieldName, Object valueToSet) {
private void setPrivateFieldInTest(Object targetObject, String fieldName, Object valueToSet) {
try {
Field field = targetObject.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,69 +1,42 @@
//package org.owasp.dsomm.metricca.analyzer.deserialization;
//
//import org.junit.jupiter.api.BeforeEach;
//import org.junit.jupiter.api.Test;
//import org.mockito.Mock;
//import org.mockito.MockitoAnnotations;
//import org.owasp.dsomm.metricca.analyzer.deserialization.activity.Activity;
//
//import java.lang.reflect.Constructor;
//import java.lang.reflect.Field;
//import java.util.List;
//
//import static org.junit.jupiter.api.Assertions.assertTrue;
//
//public class ApplicationDirectorTest {
//
// private ApplicationDirector applicationDirector;
//
// @Mock
// private YamlScanner yamlScanner;
//
// @BeforeEach
// public void setup() {
// MockitoAnnotations.openMocks(this);
// }
//
// @BeforeEach
// public void setUp() throws Exception {
// Constructor<ApplicationDirector> constructor = ApplicationDirector.class.getDeclaredConstructor();
// constructor.setAccessible(true);
// applicationDirector = constructor.newInstance();
// YamlScanner yamlScanner = new YamlScanner();
// setPrivateField(yamlScanner, "yamlApplicationFolderPath", "src/test/resources/test-all/definitions");
// setPrivateField(yamlScanner, "yamlSkeletonFilePath", "src/test/resources/test-all/skeleton.yaml");
// yamlScanner.getApplicationYamls();
// setPrivateField(this.applicationDirector, "yamlScanner", yamlScanner);
// }
//
// private void setPrivateField(Object targetObject, String fieldName, Object valueToSet) {
// try {
// Field field = targetObject.getClass().getDeclaredField(fieldName);
// field.setAccessible(true);
// field.set(targetObject, valueToSet);
// } catch (NoSuchFieldException | IllegalAccessException e) {
// throw new RuntimeException(e);
// }
// }
//
// @Test
// public void testApplicationsExists() throws Exception {
// List<Application> actualApplications = applicationDirector.getApplications();
// assertTrue(actualApplications.size() > 0);
// }
//
// @Test
// public void testTeamActivitySecurityRequirementsExists() throws Exception {
// List<Application> actualApplications = applicationDirector.getApplications();
// boolean foundSecurityRequirements = false;
// for (Application application : actualApplications) {
// for (Activity activity : application.getActivities()) {
// if (activity.getName().equals("Security requirements")) {
// foundSecurityRequirements = true;
// break;
// }
// }
// }
// assertTrue(foundSecurityRequirements);
// }
//}
package org.owasp.dsomm.metricca.analyzer.deserialization;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.owasp.dsomm.metricca.analyzer.ApplicationTest;
import org.owasp.dsomm.metricca.analyzer.deserialization.activity.Activity;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class ApplicationDirectorTest extends ApplicationTest {
@BeforeEach
public void setup() {
MockitoAnnotations.openMocks(this);
}

@Test
public void testApplicationsExists() throws Exception {
List<Application> actualApplications = applicationDirector.getApplications();
assertTrue(actualApplications.size() > 0);
}

@Test
public void testTeamActivitySecurityRequirementsExists() throws Exception {
List<Application> actualApplications = applicationDirector.getApplications();
boolean foundSecurityRequirements = false;
for (Application application : actualApplications) {
for (Activity activity : application.getActivities()) {
if (activity.getName().equals("Security requirements")) {
foundSecurityRequirements = true;
break;
}
}
}
assertTrue(foundSecurityRequirements);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockitoAnnotations;
import org.owasp.dsomm.metricca.analyzer.ApplicationTest;
import org.owasp.dsomm.metricca.analyzer.deserialization.activity.UrlActivity;
import org.owasp.dsomm.metricca.analyzer.deserialization.activity.threshold.DatePeriod;
import org.slf4j.Logger;
Expand All @@ -22,40 +23,9 @@

import static org.junit.jupiter.api.Assertions.*;

public class DatePeriodWithSecurityRequirementsTest {
public class DatePeriodWithSecurityRequirementsTest extends ApplicationTest {
private static final Logger logger = LoggerFactory.getLogger(DatePeriodWithSecurityRequirementsTest.class);

private ApplicationDirector applicationDirector;

@BeforeEach
public void setup() {
MockitoAnnotations.openMocks(this);
}

@BeforeEach
public void setUp() throws Exception {
Constructor<ApplicationDirector> constructor = ApplicationDirector.class.getDeclaredConstructor();
constructor.setAccessible(true);
applicationDirector = constructor.newInstance();
YamlScanner yamlScanner = new YamlScanner();
yamlScanner.initiate();
setPrivateField(yamlScanner, "yamlApplicationFolderPath", "src/test/resources/test-security-requirements/definitions");
setPrivateField(yamlScanner, "yamlSkeletonFilePath", "src/test/resources/test-security-requirements/skeleton.yaml");
yamlScanner.getApplicationYamls();
setPrivateField(this.applicationDirector, "yamlScanner", yamlScanner);
yamlScanner.getSkeletonYaml();
}

private void setPrivateField(Object targetObject, String fieldName, Object valueToSet) {
try {
Field field = targetObject.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(targetObject, valueToSet);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}

private UrlActivity getSecurityRequirementsActivity() throws GitAPIException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
List<Application> actualApplications = applicationDirector.getApplications();
for (Application application : actualApplications) {
Expand Down Expand Up @@ -104,7 +74,7 @@ public void testShowEndDateWithEndDateToShowForSingleDate() throws Exception {
SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd");
isoFormat.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));

Date existingDate = isoFormat.parse("2021-05-01");
Date existingDate = isoFormat.parse("2000-01-01");
DatePeriod endDateForExistingDate = activity.getThresholdDatePeriodMap().get("Level 1").getDatePeriodForDate(existingDate);
assertNotNull(endDateForExistingDate);
assertTrue(endDateForExistingDate.getShowEndDate());
Expand Down

0 comments on commit 1dc98ab

Please sign in to comment.