cleaned up code, added comments

This commit is contained in:
iadgovuser58 2023-10-19 13:46:15 -04:00
parent a0662f75e6
commit 5a42ec379e
4 changed files with 64 additions and 29 deletions

View File

@ -21,17 +21,33 @@ import static org.hamcrest.Matchers.equalTo;
*/
@SpringBootTest
@ActiveProfiles("test")
@TestInstance(TestInstance.Lifecycle.PER_CLASS) // needed to use non-static BeforeAll
@ContextConfiguration(classes = PageTestConfiguration.class)
@ContextConfiguration(classes = PageTestConfiguration.class) // configuration class for db setup
@ActiveProfiles("test") // sets profile for test to allow bean overriding
@TestInstance(TestInstance.Lifecycle.PER_CLASS) // needed to use non-static BeforeAll
public abstract class PageControllerTest {
/**
* Pre-prefix path for all the Controllers.
* There's an option in Page to add prefix path used for some Controllers.
*/
private String prePrefixPath = "HIRS_AttestationCAPortal/portal/";
/**
* Contains server-side support for testing Spring MVC applications
* via WebTestClient with MockMvc for server request handling.
*/
@Autowired
private WebApplicationContext webApplicationContext;
/**
* Used to set up mocked servlet environment to test the HTTP controller
* endpoints without the need to launch the embedded servlet container.
*/
private MockMvc mockMvc;
/**
* Represents the Page for the Controller under test.
*/
private final Page page;
/**

View File

@ -2,8 +2,6 @@ package hirs.attestationca.portal.page;
import hirs.attestationca.portal.PageConfiguration;
import hirs.attestationca.persist.entity.userdefined.certificate.CertificateAuthorityCredential;
import jakarta.xml.bind.DatatypeConverter;
import org.hibernate.AssertionFailure;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
@ -17,7 +15,6 @@ import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
import javax.sql.DataSource;
import java.io.IOException;
@ -26,16 +23,16 @@ import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.*;
import java.security.cert.X509Certificate;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
import java.util.Properties;
import java.util.*;
/**
* A configuration class for testing Attestation CA Portal classes that require a database.
* This apparently is needed to appease spring tests in the TestNG runner.
* This class sets up a temporary in-memory database that is used for testing.
* This class also creates beans that override beans in main class PersistenceJPAConfig.
* A few 'dummy' beans had to be created to override PersistenceJPAConfig beans that were
* not needed and would interfere with the tests.
*/
@Import({ PageConfiguration.class })
//@Import({ PageConfiguration.class })
@TestConfiguration
@EnableJpaRepositories(basePackages = "hirs.attestationca.persist.entity.manager")
public class PageTestConfiguration {
@ -45,6 +42,10 @@ public class PageTestConfiguration {
*/
public static final String FAKE_ROOT_CA = "/certificates/fakeCA.pem";
/**
* Represents the environment in which the current application is running.
* Models 2 aspects: profiles and properties (application-test.properties)
*/
@Autowired
private Environment environment;
@ -64,7 +65,6 @@ public class PageTestConfiguration {
return credential.getX509Certificate();
}
/**
* Overrides the {@link DataSource} with one that is configured against an in-memory HSQL DB.
*
@ -82,12 +82,14 @@ public class PageTestConfiguration {
* Enables auto scanning of annotations such that entities do not need to be registered in a
* hibernate configuration file.
*
* @return session factory
* @return entity manager factory, which provides instances of EntityManager for connecting
* to same database.
*/
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
final LocalContainerEntityManagerFactoryBean entityManagerBean = new LocalContainerEntityManagerFactoryBean();
final LocalContainerEntityManagerFactoryBean entityManagerBean =
new LocalContainerEntityManagerFactoryBean();
entityManagerBean.setDataSource(dataSource());
entityManagerBean.setPackagesToScan("hirs.attestationca.persist.entity");
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
@ -116,20 +118,22 @@ public class PageTestConfiguration {
return hibernateProperties;
}
/**
* Generates JPA transaction manager.
*
* @return transaction manager
*/
@Bean
public PlatformTransactionManager transactionManager() {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}
// @Bean
// public PlatformTransactionManager transactionManager() {
// final JpaTransactionManager transactionManager = new JpaTransactionManager();
// transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
// return transactionManager;
// }
/**
* @return a blank {@link PrivateKey}
* this function is only used to override the PersistenceJPAConfig privateKey bean during test
*/
@Bean
public PrivateKey privateKey() {
try {

View File

@ -26,7 +26,14 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
*/
public class DevicePageControllerTest extends PageControllerTest {
/**
* Name of device
*/
private static final String DEVICE_NAME = "Test Device";
/**
* Location of test credentials for device
*/
private static final String TEST_ENDORSEMENT_CREDENTIAL
= "/endorsement_credentials/tpmcert.pem";
private static final String TEST_ENDORSEMENT_CREDENTIAL_2
@ -34,15 +41,23 @@ public class DevicePageControllerTest extends PageControllerTest {
private static final String TEST_PLATFORM_CREDENTIAL
= "/platform_credentials/Intel_pc.cer";
/**
* Device object to be stored in db for test
*/
private Device device;
/**
* Repository manager to handle data access between device entity and data storage (db).
*/
@Autowired
private DeviceRepository deviceRepository;
/**
* Repository manager to handle data access between certificate entity and data storage (db).
*/
@Autowired
private CertificateRepository certificateRepository;
/**
* Constructor providing the Page's display and routing specification.
*/
@ -55,10 +70,12 @@ public class DevicePageControllerTest extends PageControllerTest {
@BeforeAll
public void beforeMethod() throws IOException {
device = new Device(DEVICE_NAME,null, HealthStatus.TRUSTED, AppraisalStatus.Status.PASS,null,false,"tmp_overrideReason", "tmp_summId");
// Create new device to be used in test and save it to db
device = new Device(DEVICE_NAME,null, HealthStatus.TRUSTED, AppraisalStatus.Status.PASS,
null,false,"tmp_overrideReason", "tmp_summId");
device = deviceRepository.save(device);
//Upload and save EK Cert
// Upload and save EK Cert
EndorsementCredential ec = (EndorsementCredential)
getTestCertificate(EndorsementCredential.class,
TEST_ENDORSEMENT_CREDENTIAL);
@ -117,7 +134,7 @@ public class DevicePageControllerTest extends PageControllerTest {
@Test
public void getDeviceList() throws Exception {
// Add prefix path for page verification
// Add pre-prefix and prefix path for page verification
String pagePath = "/" + getPrePrefixPath() + getPage().getPrefixPath() + getPage().getViewName() + "/list";
if (getPage().getPrefixPath() == null) {
pagePath = "/" + getPrePrefixPath() + getPage().getViewName() + "/list";

View File

@ -3,6 +3,4 @@ spring.main.allow-bean-definition-overriding=true
# need to override properties in application.properties and hibernate.properties
hibernate.dialect = org.hibernate.dialect.HSQLDialect
hibernate.hbm2ddl.auto = create
#hibernate.connection.username=
#hibernate.connection.password=
hibernate.hbm2ddl.auto = create