Skip to content

Commit

Permalink
webapp tests
Browse files Browse the repository at this point in the history
  • Loading branch information
avojak committed Mar 30, 2019
1 parent cc21fdb commit ef80ab8
Show file tree
Hide file tree
Showing 10 changed files with 348 additions and 6 deletions.
4 changes: 4 additions & 0 deletions aws-p2-repository-test-report/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<configuration>
<excludes>
<!-- Exclude the main class, which is not testable -->
<exclude>**/AWSP2Repository.*</exclude>
</excludes>
</configuration>
<executions>
<execution>
Expand Down
21 changes: 21 additions & 0 deletions aws-p2-repository-webapp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.springframework.context.annotation.ComponentScan;

/**
* Main application class for AWS p2 Browser.
* Main application class for AWS p2 Repository.
*/
@ComponentScan(value = {
"com.avojak.webapp.aws.p2.repository.service",
Expand All @@ -21,7 +21,7 @@ public class AWSP2Repository {
* The command-line arguments.
*/
public static void main(final String... args) {
// TODO: Somewhere on startup, should verify that the p2 inspector is running
// TODO: Somewhere on startup, should verify that the p2 inspector is running?
SpringApplication.run(AWSP2Repository.class, args);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* The web application properties.
*/
public class WebappProperties {
public final class WebappProperties {

private final String brandName;
private final String brandIcon;
Expand All @@ -33,8 +33,8 @@ public class WebappProperties {
* @param bucketName
* The S3 bucket name.
*/
WebappProperties(final String brandName, final String brandIcon, final String brandFavicon,
final String customDomain, final String welcomeMessage, final String bucketName) {
public WebappProperties(final String brandName, final String brandIcon, final String brandFavicon,
final String customDomain, final String welcomeMessage, final String bucketName) {
this.brandName = brandName;
this.brandIcon = brandIcon;
this.brandFavicon = brandFavicon;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public class ViewController {
* Constructor.
*
* @param service
* The {@link DataService}.
* The {@link DataService}. Cannot be null.
* @param properties
* The {@link WebappProperties}. Cannot be null.
*/
@Autowired
public ViewController(final DataService service, final WebappProperties properties) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.avojak.webapp.aws.p2.repository.webapp.configuration;

import org.junit.Before;
import org.junit.Test;
import org.springframework.test.util.ReflectionTestUtils;

import static org.junit.Assert.assertNotNull;

/**
* Test class for {@link WebappConfiguration}.
*/
public class WebappConfigurationTest {

private WebappConfiguration configuration;

@Before
public void setup() {
configuration = new WebappConfiguration();
ReflectionTestUtils.setField(configuration, "brandName", "Mock Brand");
ReflectionTestUtils.setField(configuration, "brandIcon", "mock.icon");
ReflectionTestUtils.setField(configuration, "brandFavicon", "mock.favicon");
ReflectionTestUtils.setField(configuration, "customDomain", "mock.domain");
ReflectionTestUtils.setField(configuration, "welcomeMessage", "Mock welcome");
ReflectionTestUtils.setField(configuration, "bucketName", "mock-bucket");
}

@Test
public void testWebappProperties() {
assertNotNull(configuration.webappProperties());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.avojak.webapp.aws.p2.repository.webapp.configuration;

import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* Test class for {@link WebappProperties}.
*/
public class WebappPropertiesTest {

@Test
public void testGetters() {
final WebappProperties properties = new WebappProperties("brandName", "brandIcon", "brandFavicon",
"customDomain", "welcomeMessage", "bucketName");
assertEquals("brandName", properties.getBrandName());
assertEquals("brandIcon", properties.getBrandIcon());
assertEquals("brandFavicon", properties.getBrandFavicon());
assertEquals("customDomain", properties.getCustomDomain());
assertEquals("welcomeMessage", properties.getWelcomeMessage());
assertEquals("bucketName", properties.getBucketName());
}

@Test
public void testEquals() {
EqualsVerifier.forClass(WebappProperties.class).verify();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.avojak.webapp.aws.p2.repository.webapp.controller;

import com.avojak.webapp.aws.p2.repository.model.Qualifier;
import com.avojak.webapp.aws.p2.repository.model.project.Project;
import com.avojak.webapp.aws.p2.repository.service.DataService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* Test class for {@link DataController}.
*/
@RunWith(MockitoJUnitRunner.class)
public class DataControllerTest {

@Mock
private DataService dataService;

@Mock
private HttpServletRequest request;

private DataController controller;

@Before
public void setup() {
controller = new DataController(dataService);
}

@Test(expected = NullPointerException.class)
public void testConstructor_NullDataService() {
new DataController(null);
}

@Test
public void testGetResource() {
final String requestURI = "content/mock-project/releases/1.0.0";
when(request.getRequestURI()).thenReturn(requestURI);
final String project = "mock-project";
final String qualifier = "releases";
final String version = "1.0.0";
final Resource resource = mock(Resource.class);
when(dataService.getResource("mock-project/releases/1.0.0", project, Qualifier.RELEASE, version))
.thenReturn(Optional.of(resource));

final ResponseEntity<Resource> response = controller.getResource(project, qualifier, version, request);
assertEquals(200, response.getStatusCodeValue());
assertEquals(resource, response.getBody());
}

@Test
public void testGetResource_NotFound() {
final String requestURI = "content/mock-project/releases/1.0.0";
when(request.getRequestURI()).thenReturn(requestURI);
final String project = "mock-project";
final String qualifier = "releases";
final String version = "1.0.0";
when(dataService.getResource("mock-project/releases/1.0.0", project, Qualifier.RELEASE, version))
.thenReturn(Optional.empty());

final ResponseEntity<Resource> response = controller.getResource(project, qualifier, version, request);
assertEquals(404, response.getStatusCodeValue());
}

@Test
public void testGetProjects() {
final List<Project> projects = new ArrayList<>();
when(dataService.getProjects()).thenReturn(projects);

final ResponseEntity<Collection<Project>> response = controller.getProjects();

assertEquals(200, response.getStatusCodeValue());
assertEquals(projects, response.getBody());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.avojak.webapp.aws.p2.repository.webapp.controller;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* Test class for {@link RedirectController}.
*/
public class RedirectControllerTest {

@Test
public void testRedirect() {
assertEquals("redirect:/browse", new RedirectController().redirectToBrowse());
}

}

0 comments on commit ef80ab8

Please sign in to comment.