From e64487cb57ca75d17fe5a8a664c1e8247c7b5168 Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Sat, 7 Jan 2023 19:23:11 +0000 Subject: refactor(web/server): Use Panache for entity modeling This change updates the OpenDC web server to use Panache (provided by Quarkus) to model entities. Such approach is better supported in Quarkus and simplifies our implementation. --- .../server/service/UserAccountingServiceTest.java | 213 +++++++++++++++++++++ 1 file changed, 213 insertions(+) create mode 100644 opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/service/UserAccountingServiceTest.java (limited to 'opendc-web/opendc-web-server/src/test/java/org') diff --git a/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/service/UserAccountingServiceTest.java b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/service/UserAccountingServiceTest.java new file mode 100644 index 00000000..d1d82097 --- /dev/null +++ b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/service/UserAccountingServiceTest.java @@ -0,0 +1,213 @@ +/* + * Copyright (c) 2023 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.web.server.service; + +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyString; + +import io.quarkus.panache.mock.PanacheMock; +import io.quarkus.test.junit.QuarkusTest; +import java.time.Duration; +import java.time.LocalDate; +import javax.persistence.EntityExistsException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.opendc.web.server.model.UserAccounting; + +/** + * Test suite for the {@link UserAccountingService}. + */ +@QuarkusTest +public class UserAccountingServiceTest { + /** + * The {@link UserAccountingService} instance under test. + */ + private UserAccountingService service; + + /** + * The user id to test with + */ + private final String userId = "test"; + + @BeforeEach + public void setUp() { + PanacheMock.mock(UserAccounting.class); + service = new UserAccountingService(Duration.ofHours(1)); + } + + @Test + public void testGetUserDoesNotExist() { + Mockito.when(UserAccounting.findByUser(userId)).thenReturn(null); + + var accounting = service.getAccounting(userId); + + assertTrue(accounting.getPeriodEnd().isAfter(LocalDate.now())); + assertEquals(0, accounting.getSimulationTime()); + } + + @Test + public void testGetUserDoesExist() { + var now = LocalDate.now(); + var periodEnd = now.plusMonths(1); + + var mockAccounting = new UserAccounting(userId, periodEnd, 3600); + mockAccounting.simulationTime = 32; + + Mockito.when(UserAccounting.findByUser(userId)).thenReturn(mockAccounting); + + var accounting = service.getAccounting(userId); + + assertAll( + () -> assertEquals(periodEnd, accounting.getPeriodEnd()), + () -> assertEquals(32, accounting.getSimulationTime()), + () -> assertEquals(3600, accounting.getSimulationTimeBudget())); + } + + @Test + public void testHasBudgetUserDoesNotExist() { + Mockito.when(UserAccounting.findByUser(userId)).thenReturn(null); + + assertTrue(service.hasSimulationBudget(userId)); + } + + @Test + public void testHasBudget() { + var periodEnd = LocalDate.now().plusMonths(2); + + var mockAccounting = new UserAccounting(userId, periodEnd, 3600); + Mockito.when(UserAccounting.findByUser(userId)).thenReturn(mockAccounting); + + assertTrue(service.hasSimulationBudget(userId)); + } + + @Test + public void testHasBudgetExceededButPeriodExpired() { + var periodEnd = LocalDate.now().minusMonths(2); + + var mockAccounting = new UserAccounting(userId, periodEnd, 3600); + mockAccounting.simulationTime = 3900; + Mockito.when(UserAccounting.findByUser(userId)).thenReturn(mockAccounting); + + assertTrue(service.hasSimulationBudget(userId)); + } + + @Test + public void testHasBudgetPeriodExpired() { + var periodEnd = LocalDate.now().minusMonths(2); + + var mockAccounting = new UserAccounting(userId, periodEnd, 3600); + Mockito.when(UserAccounting.findByUser(userId)).thenReturn(mockAccounting); + + assertTrue(service.hasSimulationBudget(userId)); + } + + @Test + public void testHasBudgetExceeded() { + var periodEnd = LocalDate.now().plusMonths(1); + + var mockAccounting = new UserAccounting(userId, periodEnd, 3600); + mockAccounting.simulationTime = 3900; + Mockito.when(UserAccounting.findByUser(userId)).thenReturn(mockAccounting); + + assertFalse(service.hasSimulationBudget(userId)); + } + + @Test + public void testConsumeBudgetNewUser() { + Mockito.when(UserAccounting.findByUser(userId)).thenReturn(null); + Mockito.when(UserAccounting.create(anyString(), any(), anyInt(), anyInt())) + .thenAnswer((i) -> { + var accounting = new UserAccounting(i.getArgument(0), i.getArgument(1), i.getArgument(2)); + accounting.simulationTime = i.getArgument(3); + return accounting; + }); + + assertFalse(service.consumeSimulationBudget(userId, 10)); + } + + @Test + public void testConsumeBudgetNewUserExceeded() { + Mockito.when(UserAccounting.findByUser(userId)).thenReturn(null); + Mockito.when(UserAccounting.create(anyString(), any(), anyInt(), anyInt())) + .thenAnswer((i) -> { + var accounting = new UserAccounting(i.getArgument(0), i.getArgument(1), i.getArgument(2)); + accounting.simulationTime = i.getArgument(3); + return accounting; + }); + + assertTrue(service.consumeSimulationBudget(userId, 4000)); + } + + @Test + public void testConsumeBudgetNewUserConflict() { + var periodEnd = LocalDate.now().plusMonths(1); + var accountingMock = Mockito.spy(new UserAccounting(userId, periodEnd, 3600)); + + Mockito.when(UserAccounting.findByUser(userId)).thenReturn(null).thenReturn(accountingMock); + Mockito.when(UserAccounting.create(anyString(), any(), anyInt(), anyInt())) + .thenThrow(new EntityExistsException()); + Mockito.when(accountingMock.consumeBudget(anyInt())).thenAnswer((i) -> { + accountingMock.simulationTime += i.getArgument(0); + return true; + }); + + assertFalse(service.consumeSimulationBudget(userId, 10)); + } + + @Test + public void testConsumeBudgetResetSuccess() { + var periodEnd = LocalDate.now().minusMonths(2); + var accountingMock = Mockito.spy(new UserAccounting(userId, periodEnd, 3600)); + accountingMock.simulationTime = 3900; + + Mockito.when(UserAccounting.findByUser(userId)).thenReturn(accountingMock); + Mockito.when(accountingMock.resetBudget(any(), anyInt())).thenAnswer((i) -> { + accountingMock.periodEnd = i.getArgument(0); + accountingMock.simulationTime += i.getArgument(1); + return true; + }); + + assertTrue(service.consumeSimulationBudget(userId, 4000)); + } + + @Test + public void testInfiniteConflict() { + var periodEnd = LocalDate.now().plusMonths(1); + var accountingMock = Mockito.spy(new UserAccounting(userId, periodEnd, 3600)); + + Mockito.when(UserAccounting.findByUser(userId)).thenReturn(accountingMock); + Mockito.when(accountingMock.consumeBudget(anyInt())).thenAnswer((i) -> { + accountingMock.simulationTime += i.getArgument(0); + return false; + }); + + assertThrows(IllegalStateException.class, () -> service.consumeSimulationBudget(userId, 10)); + } +} -- cgit v1.2.3 From 6927c51885bb3073b310150c4f40c64eea44a919 Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Sun, 22 Jan 2023 00:30:51 +0000 Subject: refactor(web/server): Convert resources to Java This change converts the resource classes of the OpenDC web server to use Java, The Quarkus integration for Java is more mature and the programming quality of experience is not that much worse. --- .../web/server/rest/SchedulerResourceTest.java | 45 ++++ .../opendc/web/server/rest/TraceResourceTest.java | 86 +++++++ .../web/server/rest/runner/JobResourceTest.java | 194 ++++++++++++++ .../server/rest/user/PortfolioResourceTest.java | 240 ++++++++++++++++++ .../rest/user/PortfolioScenarioResourceTest.java | 218 ++++++++++++++++ .../web/server/rest/user/ProjectResourceTest.java | 208 +++++++++++++++ .../web/server/rest/user/ScenarioResourceTest.java | 166 ++++++++++++ .../web/server/rest/user/TopologyResourceTest.java | 281 +++++++++++++++++++++ .../web/server/rest/user/UserResourceTest.java | 65 +++++ 9 files changed, 1503 insertions(+) create mode 100644 opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/SchedulerResourceTest.java create mode 100644 opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/TraceResourceTest.java create mode 100644 opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/runner/JobResourceTest.java create mode 100644 opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/PortfolioResourceTest.java create mode 100644 opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/PortfolioScenarioResourceTest.java create mode 100644 opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/ProjectResourceTest.java create mode 100644 opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/ScenarioResourceTest.java create mode 100644 opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/TopologyResourceTest.java create mode 100644 opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/UserResourceTest.java (limited to 'opendc-web/opendc-web-server/src/test/java/org') diff --git a/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/SchedulerResourceTest.java b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/SchedulerResourceTest.java new file mode 100644 index 00000000..feeac4d3 --- /dev/null +++ b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/SchedulerResourceTest.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.web.server.rest; + +import static io.restassured.RestAssured.when; + +import io.quarkus.test.common.http.TestHTTPEndpoint; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.http.ContentType; +import org.junit.jupiter.api.Test; + +/** + * Test suite for {@link SchedulerResource}. + */ +@QuarkusTest +@TestHTTPEndpoint(SchedulerResource.class) +public final class SchedulerResourceTest { + /** + * Test to verify whether we can obtain all schedulers. + */ + @Test + public void testGetSchedulers() { + when().get().then().statusCode(200).contentType(ContentType.JSON); + } +} diff --git a/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/TraceResourceTest.java b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/TraceResourceTest.java new file mode 100644 index 00000000..ebef3945 --- /dev/null +++ b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/TraceResourceTest.java @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2023 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.web.server.rest; + +import static io.restassured.RestAssured.when; +import static org.hamcrest.Matchers.equalTo; + +import io.quarkus.panache.mock.PanacheMock; +import io.quarkus.test.common.http.TestHTTPEndpoint; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.http.ContentType; +import java.util.stream.Stream; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.opendc.web.server.model.Trace; + +/** + * Test suite for {@link TraceResource}. + */ +@QuarkusTest +@TestHTTPEndpoint(TraceResource.class) +public final class TraceResourceTest { + /** + * Set up the test environment. + */ + @BeforeEach + public void setUp() { + PanacheMock.mock(Trace.class); + } + + /** + * Test that tries to obtain all traces (empty response). + */ + @Test + public void testGetAllEmpty() { + Mockito.when(Trace.streamAll()).thenReturn(Stream.of()); + + when().get().then().statusCode(200).contentType(ContentType.JSON).body("", Matchers.empty()); + } + + /** + * Test that tries to obtain a non-existent trace. + */ + @Test + public void testGetNonExisting() { + Mockito.when(Trace.findById("bitbrains")).thenReturn(null); + + when().get("/bitbrains").then().statusCode(404).contentType(ContentType.JSON); + } + + /** + * Test that tries to obtain an existing trace. + */ + @Test + public void testGetExisting() { + Mockito.when(Trace.findById("bitbrains")).thenReturn(new Trace("bitbrains", "Bitbrains", "VM")); + + when().get("/bitbrains") + .then() + .statusCode(200) + .contentType(ContentType.JSON) + .body("name", equalTo("Bitbrains")); + } +} diff --git a/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/runner/JobResourceTest.java b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/runner/JobResourceTest.java new file mode 100644 index 00000000..a163cd29 --- /dev/null +++ b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/runner/JobResourceTest.java @@ -0,0 +1,194 @@ +/* + * Copyright (c) 2023 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.web.server.rest.runner; + +import static io.restassured.RestAssured.given; +import static io.restassured.RestAssured.when; +import static org.hamcrest.Matchers.equalTo; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.eq; + +import io.quarkus.test.common.http.TestHTTPEndpoint; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.mockito.InjectMock; +import io.quarkus.test.security.TestSecurity; +import io.restassured.http.ContentType; +import java.time.Instant; +import java.util.List; +import java.util.Set; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.opendc.web.proto.JobState; +import org.opendc.web.proto.OperationalPhenomena; +import org.opendc.web.proto.Targets; +import org.opendc.web.proto.Trace; +import org.opendc.web.proto.Workload; +import org.opendc.web.proto.runner.Job; +import org.opendc.web.proto.runner.Portfolio; +import org.opendc.web.proto.runner.Scenario; +import org.opendc.web.proto.runner.Topology; +import org.opendc.web.server.service.JobService; + +/** + * Test suite for {@link JobResource}. + */ +@QuarkusTest +@TestHTTPEndpoint(JobResource.class) +public final class JobResourceTest { + @InjectMock + private JobService jobService; + + /** + * Dummy values + */ + private final Portfolio dummyPortfolio = new Portfolio(1, 1, "test", new Targets(Set.of(), 1)); + + private final Topology dummyTopology = new Topology(1, 1, "test", List.of(), Instant.now(), Instant.now()); + private final Trace dummyTrace = new Trace("bitbrains", "Bitbrains", "vm"); + private final Scenario dummyScenario = new Scenario( + 1, + 1, + dummyPortfolio, + "test", + new Workload(dummyTrace, 1.0), + dummyTopology, + new OperationalPhenomena(false, false), + "test"); + private final Job dummyJob = new Job(1, dummyScenario, JobState.PENDING, Instant.now(), Instant.now(), 0, null); + + /** + * Test that tries to query the pending jobs without token. + */ + @Test + public void testQueryWithoutToken() { + when().get().then().statusCode(401); + } + + /** + * Test that tries to query the pending jobs for a user. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testQueryInvalidScope() { + when().get().then().statusCode(403); + } + + /** + * Test that tries to query the pending jobs for a runner. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"runner"}) + public void testQuery() { + Mockito.when(jobService.listPending()).thenReturn(List.of(dummyJob)); + + when().get().then().statusCode(200).contentType(ContentType.JSON).body("get(0).id", equalTo(1)); + } + + /** + * Test that tries to obtain a non-existent job. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"runner"}) + public void testGetNonExisting() { + Mockito.when(jobService.findById(1)).thenReturn(null); + + when().get("/1").then().statusCode(404).contentType(ContentType.JSON); + } + + /** + * Test that tries to obtain a job. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"runner"}) + public void testGetExisting() { + Mockito.when(jobService.findById(1)).thenReturn(dummyJob); + + when().get("/1").then().statusCode(200).contentType(ContentType.JSON).body("id", equalTo(1)); + } + + /** + * Test that tries to update a non-existent job. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"runner"}) + public void testUpdateNonExistent() { + Mockito.when(jobService.updateState(eq(1L), any(), anyInt(), any())).thenReturn(null); + + given().body(new Job.Update(JobState.PENDING, 0, null)) + .contentType(ContentType.JSON) + .when() + .post("/1") + .then() + .statusCode(404) + .contentType(ContentType.JSON); + } + + /** + * Test that tries to update a job. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"runner"}) + public void testUpdateState() { + Mockito.when(jobService.updateState(eq(1L), any(), anyInt(), any())) + .thenReturn(new Job(1, dummyScenario, JobState.CLAIMED, Instant.now(), Instant.now(), 0, null)); + + given().body(new Job.Update(JobState.CLAIMED, 0, null)) + .contentType(ContentType.JSON) + .when() + .post("/1") + .then() + .statusCode(200) + .contentType(ContentType.JSON) + .body("state", equalTo(JobState.CLAIMED.toString())); + } + + /** + * Test that tries to update a job with invalid input. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"runner"}) + public void testUpdateInvalidInput() { + given().body("{ \"test\": \"test\" }") + .contentType(ContentType.JSON) + .when() + .post("/1") + .then() + .statusCode(400) + .contentType(ContentType.JSON); + } +} diff --git a/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/PortfolioResourceTest.java b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/PortfolioResourceTest.java new file mode 100644 index 00000000..cc3ac978 --- /dev/null +++ b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/PortfolioResourceTest.java @@ -0,0 +1,240 @@ +/* + * Copyright (c) 2023 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.web.server.rest.user; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; + +import io.quarkus.test.common.http.TestHTTPEndpoint; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.mockito.InjectMock; +import io.quarkus.test.security.TestSecurity; +import io.restassured.http.ContentType; +import java.time.Instant; +import java.util.List; +import java.util.Set; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.opendc.web.proto.Targets; +import org.opendc.web.proto.user.Portfolio; +import org.opendc.web.proto.user.Project; +import org.opendc.web.proto.user.ProjectRole; +import org.opendc.web.server.service.PortfolioService; + +/** + * Test suite for {@link PortfolioResource}. + */ +@QuarkusTest +@TestHTTPEndpoint(PortfolioResource.class) +public final class PortfolioResourceTest { + @InjectMock + private PortfolioService portfolioService; + + /** + * Dummy project and portfolio + */ + private final Project dummyProject = new Project(1, "test", Instant.now(), Instant.now(), ProjectRole.OWNER); + + private final Portfolio dummyPortfolio = + new Portfolio(1, 1, dummyProject, "test", new Targets(Set.of(), 1), List.of()); + + /** + * Test that tries to obtain the list of portfolios belonging to a project. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testGetForProject() { + Mockito.when(portfolioService.findByUser("testUser", 1)).thenReturn(List.of()); + + given().pathParam("project", 1).when().get().then().statusCode(200).contentType(ContentType.JSON); + } + + /** + * Test that tries to create a topology for a project. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testCreateNonExistent() { + Mockito.when(portfolioService.create(eq("testUser"), eq(1), any())).thenReturn(null); + + given().pathParam("project", "1") + .body(new Portfolio.Create("test", new Targets(Set.of(), 1))) + .contentType(ContentType.JSON) + .when() + .post() + .then() + .statusCode(404) + .contentType(ContentType.JSON); + } + + /** + * Test that tries to create a portfolio for a scenario. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testCreate() { + Mockito.when(portfolioService.create(eq("testUser"), eq(1L), any())).thenReturn(dummyPortfolio); + + given().pathParam("project", "1") + .body(new Portfolio.Create("test", new Targets(Set.of(), 1))) + .contentType(ContentType.JSON) + .when() + .post() + .then() + .statusCode(200) + .contentType(ContentType.JSON) + .body("id", equalTo(1)) + .body("name", equalTo("test")); + } + + /** + * Test to create a portfolio with an empty body. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testCreateEmpty() { + given().pathParam("project", "1") + .body("{}") + .contentType(ContentType.JSON) + .when() + .post() + .then() + .statusCode(400) + .contentType(ContentType.JSON); + } + + /** + * Test to create a portfolio with a blank name. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testCreateBlankName() { + given().pathParam("project", "1") + .body(new Portfolio.Create("", new Targets(Set.of(), 1))) + .contentType(ContentType.JSON) + .when() + .post() + .then() + .statusCode(400) + .contentType(ContentType.JSON); + } + + /** + * Test that tries to obtain a portfolio without token. + */ + @Test + public void testGetWithoutToken() { + given().pathParam("project", "1").when().get("/1").then().statusCode(401); + } + + /** + * Test that tries to obtain a portfolio with an invalid scope. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"runner"}) + public void testGetInvalidToken() { + given().pathParam("project", "1").when().get("/1").then().statusCode(403); + } + + /** + * Test that tries to obtain a non-existent portfolio. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testGetNonExisting() { + Mockito.when(portfolioService.findByUser("testUser", 1, 1)).thenReturn(null); + + given().pathParam("project", "1") + .when() + .get("/1") + .then() + .statusCode(404) + .contentType(ContentType.JSON); + } + + /** + * Test that tries to obtain a portfolio. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testGetExisting() { + Mockito.when(portfolioService.findByUser("testUser", 1, 1)).thenReturn(dummyPortfolio); + + given().pathParam("project", "1") + .when() + .get("/1") + .then() + .statusCode(200) + .contentType(ContentType.JSON) + .body("id", equalTo(1)); + } + + /** + * Test to delete a non-existent portfolio. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testDeleteNonExistent() { + Mockito.when(portfolioService.delete("testUser", 1, 1)).thenReturn(null); + + given().pathParam("project", "1").when().delete("/1").then().statusCode(404); + } + + /** + * Test to delete a portfolio. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testDelete() { + Mockito.when(portfolioService.delete("testUser", 1, 1)).thenReturn(dummyPortfolio); + + given().pathParam("project", "1") + .when() + .delete("/1") + .then() + .statusCode(200) + .contentType(ContentType.JSON); + } +} diff --git a/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/PortfolioScenarioResourceTest.java b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/PortfolioScenarioResourceTest.java new file mode 100644 index 00000000..8cb95a98 --- /dev/null +++ b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/PortfolioScenarioResourceTest.java @@ -0,0 +1,218 @@ +/* + * Copyright (c) 2023 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.web.server.rest.user; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.eq; + +import io.quarkus.test.common.http.TestHTTPEndpoint; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.mockito.InjectMock; +import io.quarkus.test.security.TestSecurity; +import io.restassured.http.ContentType; +import java.time.Instant; +import java.util.List; +import java.util.Set; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.opendc.web.proto.JobState; +import org.opendc.web.proto.OperationalPhenomena; +import org.opendc.web.proto.Targets; +import org.opendc.web.proto.Trace; +import org.opendc.web.proto.Workload; +import org.opendc.web.proto.user.Job; +import org.opendc.web.proto.user.Portfolio; +import org.opendc.web.proto.user.Project; +import org.opendc.web.proto.user.ProjectRole; +import org.opendc.web.proto.user.Scenario; +import org.opendc.web.proto.user.Topology; +import org.opendc.web.server.service.ScenarioService; + +/** + * Test suite for {@link PortfolioScenarioResource}. + */ +@QuarkusTest +@TestHTTPEndpoint(PortfolioScenarioResource.class) +public final class PortfolioScenarioResourceTest { + @InjectMock + private ScenarioService scenarioService; + + /** + * Dummy values + */ + private final Project dummyProject = new Project(0, "test", Instant.now(), Instant.now(), ProjectRole.OWNER); + + private final Portfolio.Summary dummyPortfolio = new Portfolio.Summary(1, 1, "test", new Targets(Set.of(), 1)); + private final Job dummyJob = new Job(1, JobState.PENDING, Instant.now(), Instant.now(), null); + private final Trace dummyTrace = new Trace("bitbrains", "Bitbrains", "vm"); + private final Topology.Summary dummyTopology = new Topology.Summary(1, 1, "test", Instant.now(), Instant.now()); + private final Scenario dummyScenario = new Scenario( + 1, + 1, + dummyProject, + dummyPortfolio, + "test", + new Workload(dummyTrace, 1.0), + dummyTopology, + new OperationalPhenomena(false, false), + "test", + dummyJob); + + /** + * Test that tries to obtain a portfolio without token. + */ + @Test + public void testGetWithoutToken() { + given().pathParam("project", "1") + .pathParam("portfolio", "1") + .when() + .get() + .then() + .statusCode(401); + } + + /** + * Test that tries to obtain a portfolio with an invalid scope. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"runner"}) + public void testGetInvalidToken() { + given().pathParam("project", "1") + .pathParam("portfolio", "1") + .when() + .get() + .then() + .statusCode(403); + } + + /** + * Test that tries to obtain a non-existent portfolio. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testGet() { + Mockito.when(scenarioService.findAll("testUser", 1, 1)).thenReturn(List.of()); + + given().pathParam("project", "1") + .pathParam("portfolio", "1") + .when() + .get() + .then() + .statusCode(200) + .contentType(ContentType.JSON); + } + + /** + * Test that tries to create a scenario for a portfolio. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testCreateNonExistent() { + Mockito.when(scenarioService.create(eq("testUser"), eq(1L), anyInt(), any())) + .thenReturn(null); + + given().pathParam("project", "1") + .pathParam("portfolio", "1") + .body(new Scenario.Create( + "test", new Workload.Spec("test", 1.0), 1, new OperationalPhenomena(false, false), "test")) + .contentType(ContentType.JSON) + .when() + .post() + .then() + .statusCode(404) + .contentType(ContentType.JSON); + } + + /** + * Test that tries to create a scenario for a portfolio. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testCreate() { + Mockito.when(scenarioService.create(eq("testUser"), eq(1L), eq(1), any())) + .thenReturn(dummyScenario); + + given().pathParam("project", "1") + .pathParam("portfolio", "1") + .body(new Scenario.Create( + "test", new Workload.Spec("test", 1.0), 1, new OperationalPhenomena(false, false), "test")) + .contentType(ContentType.JSON) + .when() + .post() + .then() + .statusCode(200) + .contentType(ContentType.JSON) + .body("id", equalTo(1)) + .body("name", equalTo("test")); + } + + /** + * Test to create a project with an empty body. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testCreateEmpty() { + given().pathParam("project", "1") + .pathParam("portfolio", "1") + .body("{}") + .contentType(ContentType.JSON) + .when() + .post() + .then() + .statusCode(400) + .contentType(ContentType.JSON); + } + + /** + * Test to create a project with a blank name. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testCreateBlankName() { + given().pathParam("project", "1") + .pathParam("portfolio", "1") + .body(new Scenario.Create( + "", new Workload.Spec("test", 1.0), 1, new OperationalPhenomena(false, false), "test")) + .contentType(ContentType.JSON) + .when() + .post() + .then() + .statusCode(400) + .contentType(ContentType.JSON); + } +} diff --git a/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/ProjectResourceTest.java b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/ProjectResourceTest.java new file mode 100644 index 00000000..7ca314a6 --- /dev/null +++ b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/ProjectResourceTest.java @@ -0,0 +1,208 @@ +/* + * Copyright (c) 2023 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.web.server.rest.user; + +import static io.restassured.RestAssured.given; +import static io.restassured.RestAssured.when; +import static org.hamcrest.Matchers.equalTo; + +import io.quarkus.test.common.http.TestHTTPEndpoint; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.mockito.InjectMock; +import io.quarkus.test.security.TestSecurity; +import io.restassured.http.ContentType; +import java.time.Instant; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.opendc.web.proto.user.Project; +import org.opendc.web.proto.user.ProjectRole; +import org.opendc.web.server.service.ProjectService; + +/** + * Test suite for [ProjectResource]. + */ +@QuarkusTest +@TestHTTPEndpoint(ProjectResource.class) +public final class ProjectResourceTest { + @InjectMock + private ProjectService projectService; + + /** + * Dummy values. + */ + private final Project dummyProject = new Project(0, "test", Instant.now(), Instant.now(), ProjectRole.OWNER); + + /** + * Test that tries to obtain all projects without token. + */ + @Test + public void testGetAllWithoutToken() { + when().get().then().statusCode(401); + } + + /** + * Test that tries to obtain all projects with an invalid scope. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"runner"}) + public void testGetAllWithInvalidScope() { + when().get().then().statusCode(403); + } + + /** + * Test that tries to obtain all project for a user. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testGetAll() { + Mockito.when(projectService.findByUser("testUser")).thenReturn(List.of(dummyProject)); + + when().get().then().statusCode(200).contentType(ContentType.JSON).body("get(0).name", equalTo("test")); + } + + /** + * Test that tries to obtain a non-existent project. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testGetNonExisting() { + Mockito.when(projectService.findByUser("testUser", 1)).thenReturn(null); + + when().get("/1").then().statusCode(404).contentType(ContentType.JSON); + } + + /** + * Test that tries to obtain a job. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testGetExisting() { + Mockito.when(projectService.findByUser("testUser", 1)).thenReturn(dummyProject); + + when().get("/1").then().statusCode(200).contentType(ContentType.JSON).body("id", equalTo(0)); + } + + /** + * Test that tries to create a project. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testCreate() { + Mockito.when(projectService.create("testUser", "test")).thenReturn(dummyProject); + + given().body(new Project.Create("test")) + .contentType(ContentType.JSON) + .when() + .post() + .then() + .statusCode(200) + .contentType(ContentType.JSON) + .body("id", equalTo(0)) + .body("name", equalTo("test")); + } + + /** + * Test to create a project with an empty body. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testCreateEmpty() { + given().body("{}") + .contentType(ContentType.JSON) + .when() + .post() + .then() + .statusCode(400) + .contentType(ContentType.JSON); + } + + /** + * Test to create a project with a blank name. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testCreateBlankName() { + given().body(new Project.Create("")) + .contentType(ContentType.JSON) + .when() + .post() + .then() + .statusCode(400) + .contentType(ContentType.JSON); + } + + /** + * Test to delete a non-existent project. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testDeleteNonExistent() { + Mockito.when(projectService.delete("testUser", 1)).thenReturn(null); + + when().delete("/1").then().statusCode(404).contentType(ContentType.JSON); + } + + /** + * Test to delete a project. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testDelete() { + Mockito.when(projectService.delete("testUser", 1)).thenReturn(dummyProject); + + when().delete("/1").then().statusCode(200).contentType(ContentType.JSON); + } + + /** + * Test to delete a project which the user does not own. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testDeleteNonOwner() { + Mockito.when(projectService.delete("testUser", 1)) + .thenThrow(new IllegalArgumentException("User does not own project")); + + when().delete("/1").then().statusCode(403).contentType(ContentType.JSON); + } +} diff --git a/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/ScenarioResourceTest.java b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/ScenarioResourceTest.java new file mode 100644 index 00000000..850236d6 --- /dev/null +++ b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/ScenarioResourceTest.java @@ -0,0 +1,166 @@ +/* + * Copyright (c) 2023 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.web.server.rest.user; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; + +import io.quarkus.test.common.http.TestHTTPEndpoint; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.mockito.InjectMock; +import io.quarkus.test.security.TestSecurity; +import io.restassured.http.ContentType; +import java.time.Instant; +import java.util.Set; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.opendc.web.proto.JobState; +import org.opendc.web.proto.OperationalPhenomena; +import org.opendc.web.proto.Targets; +import org.opendc.web.proto.Trace; +import org.opendc.web.proto.Workload; +import org.opendc.web.proto.user.Job; +import org.opendc.web.proto.user.Portfolio; +import org.opendc.web.proto.user.Project; +import org.opendc.web.proto.user.ProjectRole; +import org.opendc.web.proto.user.Scenario; +import org.opendc.web.proto.user.Topology; +import org.opendc.web.server.service.ScenarioService; + +/** + * Test suite for [ScenarioResource]. + */ +@QuarkusTest +@TestHTTPEndpoint(ScenarioResource.class) +public final class ScenarioResourceTest { + @InjectMock + private ScenarioService scenarioService; + + /** + * Dummy values + */ + private final Project dummyProject = new Project(0, "test", Instant.now(), Instant.now(), ProjectRole.OWNER); + + private final Portfolio.Summary dummyPortfolio = new Portfolio.Summary(1, 1, "test", new Targets(Set.of(), 1)); + private final Job dummyJob = new Job(1, JobState.PENDING, Instant.now(), Instant.now(), null); + private final Trace dummyTrace = new Trace("bitbrains", "Bitbrains", "vm"); + private final Topology.Summary dummyTopology = new Topology.Summary(1, 1, "test", Instant.now(), Instant.now()); + private final Scenario dummyScenario = new Scenario( + 1, + 1, + dummyProject, + dummyPortfolio, + "test", + new Workload(dummyTrace, 1.0), + dummyTopology, + new OperationalPhenomena(false, false), + "test", + dummyJob); + + /** + * Test that tries to obtain a scenario without token. + */ + @Test + public void testGetWithoutToken() { + given().pathParam("project", "1").when().get("/1").then().statusCode(401); + } + + /** + * Test that tries to obtain a scenario with an invalid scope. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"runner"}) + public void testGetInvalidToken() { + given().pathParam("project", "1").when().get("/1").then().statusCode(403); + } + + /** + * Test that tries to obtain a non-existent scenario. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testGetNonExisting() { + Mockito.when(scenarioService.findOne("testUser", 1, 1)).thenReturn(null); + + given().pathParam("project", "1") + .when() + .get("/1") + .then() + .statusCode(404) + .contentType(ContentType.JSON); + } + + /** + * Test that tries to obtain a scenario. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testGetExisting() { + Mockito.when(scenarioService.findOne("testUser", 1, 1)).thenReturn(dummyScenario); + + given().pathParam("project", "1") + .when() + .get("/1") + .then() + .statusCode(200) + .contentType(ContentType.JSON) + .body("id", equalTo(1)); + } + + /** + * Test to delete a non-existent scenario. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testDeleteNonExistent() { + Mockito.when(scenarioService.delete("testUser", 1, 1)).thenReturn(null); + + given().pathParam("project", "1").when().delete("/1").then().statusCode(404); + } + + /** + * Test to delete a scenario. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testDelete() { + Mockito.when(scenarioService.delete("testUser", 1, 1)).thenReturn(dummyScenario); + + given().pathParam("project", "1") + .when() + .delete("/1") + .then() + .statusCode(200) + .contentType(ContentType.JSON); + } +} diff --git a/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/TopologyResourceTest.java b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/TopologyResourceTest.java new file mode 100644 index 00000000..2cc6ea4b --- /dev/null +++ b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/TopologyResourceTest.java @@ -0,0 +1,281 @@ +/* + * Copyright (c) 2023 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.web.server.rest.user; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.eq; + +import io.quarkus.test.common.http.TestHTTPEndpoint; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.mockito.InjectMock; +import io.quarkus.test.security.TestSecurity; +import io.restassured.http.ContentType; +import java.time.Instant; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.opendc.web.proto.user.Project; +import org.opendc.web.proto.user.ProjectRole; +import org.opendc.web.proto.user.Topology; +import org.opendc.web.server.service.TopologyService; + +/** + * Test suite for {@link TopologyResource}. + */ +@QuarkusTest +@TestHTTPEndpoint(TopologyResource.class) +public final class TopologyResourceTest { + @InjectMock + private TopologyService topologyService; + + /** + * Dummy project and topology. + */ + private final Project dummyProject = new Project(1, "test", Instant.now(), Instant.now(), ProjectRole.OWNER); + + private final Topology dummyTopology = + new Topology(1, 1, dummyProject, "test", List.of(), Instant.now(), Instant.now()); + + /** + * Test that tries to obtain the list of topologies belonging to a project. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testGetForProject() { + Mockito.when(topologyService.findAll("testUser", 1)).thenReturn(List.of()); + + given().pathParam("project", "1").when().get().then().statusCode(200).contentType(ContentType.JSON); + } + + /** + * Test that tries to create a topology for a project. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testCreateNonExistent() { + Mockito.when(topologyService.create(eq("testUser"), eq(1L), any())).thenReturn(null); + + given().pathParam("project", "1") + .body(new Topology.Create("test", List.of())) + .contentType(ContentType.JSON) + .when() + .post() + .then() + .statusCode(404) + .contentType(ContentType.JSON); + } + + /** + * Test that tries to create a topology for a project. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testCreate() { + Mockito.when(topologyService.create(eq("testUser"), eq(1L), any())).thenReturn(dummyTopology); + + given().pathParam("project", "1") + .body(new Topology.Create("test", List.of())) + .contentType(ContentType.JSON) + .when() + .post() + .then() + .statusCode(200) + .contentType(ContentType.JSON) + .body("id", equalTo(1)) + .body("name", equalTo("test")); + } + + /** + * Test to create a topology with an empty body. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testCreateEmpty() { + given().pathParam("project", "1") + .body("{}") + .contentType(ContentType.JSON) + .when() + .post() + .then() + .statusCode(400) + .contentType(ContentType.JSON); + } + + /** + * Test to create a topology with a blank name. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testCreateBlankName() { + given().pathParam("project", "1") + .body(new Topology.Create("", List.of())) + .contentType(ContentType.JSON) + .when() + .post() + .then() + .statusCode(400) + .contentType(ContentType.JSON); + } + + /** + * Test that tries to obtain a topology without token. + */ + @Test + public void testGetWithoutToken() { + given().pathParam("project", "1").when().get("/1").then().statusCode(401); + } + + /** + * Test that tries to obtain a topology with an invalid scope. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"runner"}) + public void testGetInvalidToken() { + given().pathParam("project", "1").when().get("/1").then().statusCode(403); + } + + /** + * Test that tries to obtain a non-existent topology. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testGetNonExisting() { + Mockito.when(topologyService.findOne("testUser", 1, 1)).thenReturn(null); + + given().pathParam("project", "1") + .when() + .get("/1") + .then() + .statusCode(404) + .contentType(ContentType.JSON); + } + + /** + * Test that tries to obtain a topology. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testGetExisting() { + Mockito.when(topologyService.findOne("testUser", 1, 1)).thenReturn(dummyTopology); + + given().pathParam("project", "1") + .when() + .get("/1") + .then() + .statusCode(200) + .contentType(ContentType.JSON) + .body("id", equalTo(1)); + } + + /** + * Test to delete a non-existent topology. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testUpdateNonExistent() { + Mockito.when(topologyService.update(eq("testUser"), anyLong(), anyInt(), any())) + .thenReturn(null); + + given().pathParam("project", "1") + .body(new Topology.Update(List.of())) + .contentType(ContentType.JSON) + .when() + .put("/1") + .then() + .statusCode(404); + } + + /** + * Test to update a topology. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testUpdate() { + Mockito.when(topologyService.update(eq("testUser"), anyLong(), anyInt(), any())) + .thenReturn(dummyTopology); + + given().pathParam("project", "1") + .body(new Topology.Update(List.of())) + .contentType(ContentType.JSON) + .when() + .put("/1") + .then() + .statusCode(200) + .contentType(ContentType.JSON); + } + + /** + * Test to delete a non-existent topology. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testDeleteNonExistent() { + Mockito.when(topologyService.delete("testUser", 1, 1)).thenReturn(null); + + given().pathParam("project", "1").when().delete("/1").then().statusCode(404); + } + + /** + * Test to delete a topology. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testDelete() { + Mockito.when(topologyService.delete("testUser", 1, 1)).thenReturn(dummyTopology); + + given().pathParam("project", "1") + .when() + .delete("/1") + .then() + .statusCode(200) + .contentType(ContentType.JSON); + } +} diff --git a/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/UserResourceTest.java b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/UserResourceTest.java new file mode 100644 index 00000000..6dcb3b4d --- /dev/null +++ b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/UserResourceTest.java @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2023 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.web.server.rest.user; + +import static io.restassured.RestAssured.when; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.greaterThan; + +import io.quarkus.test.common.http.TestHTTPEndpoint; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.security.TestSecurity; +import io.restassured.http.ContentType; +import org.junit.jupiter.api.Test; + +/** + * Test suite for [UserResource]. + */ +@QuarkusTest +@TestHTTPEndpoint(UserResource.class) +public final class UserResourceTest { + /** + * Test that tries to obtain the profile of the active user. + */ + @Test + @TestSecurity( + user = "testUser", + roles = {"openid"}) + public void testMe() { + when().get("me") + .then() + .statusCode(200) + .contentType(ContentType.JSON) + .body("userId", equalTo("testUser")) + .body("accounting.simulationTime", equalTo(0)) + .body("accounting.simulationTimeBudget", greaterThan(0)); + } + + /** + * Test that tries to obtain the profile of the active user without authorization. + */ + @Test + public void testMeUnauthorized() { + when().get("me").then().statusCode(401); + } +} -- cgit v1.2.3 From 49b3015a16287bb4486aa64c5c26f05f7c22089c Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Mon, 30 Jan 2023 22:22:59 +0000 Subject: refactor(web/server): Remove unnecessary service indirections This change removes the unnecessary service classes where they are only used to forward data from the resource to the entities. Furthermore, DTOs are now moved from the service layer to the resources. --- .../opendc/web/server/rest/TraceResourceTest.java | 30 +--- .../web/server/rest/runner/JobResourceTest.java | 75 ++------- .../server/rest/user/PortfolioResourceTest.java | 145 +++++++++++------ .../rest/user/PortfolioScenarioResourceTest.java | 171 +++++++++++++------- .../web/server/rest/user/ProjectResourceTest.java | 75 ++++----- .../web/server/rest/user/ScenarioResourceTest.java | 127 +++++++++------ .../web/server/rest/user/TopologyResourceTest.java | 175 +++++++++++++++------ .../opendc/web/server/service/JobServiceTest.java | 124 +++++++++++++++ 8 files changed, 586 insertions(+), 336 deletions(-) create mode 100644 opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/service/JobServiceTest.java (limited to 'opendc-web/opendc-web-server/src/test/java/org') diff --git a/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/TraceResourceTest.java b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/TraceResourceTest.java index ebef3945..5c5976db 100644 --- a/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/TraceResourceTest.java +++ b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/TraceResourceTest.java @@ -25,16 +25,10 @@ package org.opendc.web.server.rest; import static io.restassured.RestAssured.when; import static org.hamcrest.Matchers.equalTo; -import io.quarkus.panache.mock.PanacheMock; import io.quarkus.test.common.http.TestHTTPEndpoint; import io.quarkus.test.junit.QuarkusTest; import io.restassured.http.ContentType; -import java.util.stream.Stream; -import org.hamcrest.Matchers; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockito.Mockito; -import org.opendc.web.server.model.Trace; /** * Test suite for {@link TraceResource}. @@ -43,21 +37,11 @@ import org.opendc.web.server.model.Trace; @TestHTTPEndpoint(TraceResource.class) public final class TraceResourceTest { /** - * Set up the test environment. - */ - @BeforeEach - public void setUp() { - PanacheMock.mock(Trace.class); - } - - /** - * Test that tries to obtain all traces (empty response). + * Test that tries to obtain all traces. */ @Test public void testGetAllEmpty() { - Mockito.when(Trace.streamAll()).thenReturn(Stream.of()); - - when().get().then().statusCode(200).contentType(ContentType.JSON).body("", Matchers.empty()); + when().get().then().statusCode(200).contentType(ContentType.JSON); } /** @@ -65,9 +49,7 @@ public final class TraceResourceTest { */ @Test public void testGetNonExisting() { - Mockito.when(Trace.findById("bitbrains")).thenReturn(null); - - when().get("/bitbrains").then().statusCode(404).contentType(ContentType.JSON); + when().get("/unknown").then().statusCode(404).contentType(ContentType.JSON); } /** @@ -75,12 +57,10 @@ public final class TraceResourceTest { */ @Test public void testGetExisting() { - Mockito.when(Trace.findById("bitbrains")).thenReturn(new Trace("bitbrains", "Bitbrains", "VM")); - - when().get("/bitbrains") + when().get("/bitbrains-small") .then() .statusCode(200) .contentType(ContentType.JSON) - .body("name", equalTo("Bitbrains")); + .body("name", equalTo("Bitbrains Small")); } } diff --git a/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/runner/JobResourceTest.java b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/runner/JobResourceTest.java index a163cd29..94b2cef0 100644 --- a/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/runner/JobResourceTest.java +++ b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/runner/JobResourceTest.java @@ -25,30 +25,13 @@ package org.opendc.web.server.rest.runner; import static io.restassured.RestAssured.given; import static io.restassured.RestAssured.when; import static org.hamcrest.Matchers.equalTo; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyInt; -import static org.mockito.ArgumentMatchers.eq; import io.quarkus.test.common.http.TestHTTPEndpoint; import io.quarkus.test.junit.QuarkusTest; -import io.quarkus.test.junit.mockito.InjectMock; import io.quarkus.test.security.TestSecurity; import io.restassured.http.ContentType; -import java.time.Instant; -import java.util.List; -import java.util.Set; import org.junit.jupiter.api.Test; -import org.mockito.Mockito; import org.opendc.web.proto.JobState; -import org.opendc.web.proto.OperationalPhenomena; -import org.opendc.web.proto.Targets; -import org.opendc.web.proto.Trace; -import org.opendc.web.proto.Workload; -import org.opendc.web.proto.runner.Job; -import org.opendc.web.proto.runner.Portfolio; -import org.opendc.web.proto.runner.Scenario; -import org.opendc.web.proto.runner.Topology; -import org.opendc.web.server.service.JobService; /** * Test suite for {@link JobResource}. @@ -56,27 +39,6 @@ import org.opendc.web.server.service.JobService; @QuarkusTest @TestHTTPEndpoint(JobResource.class) public final class JobResourceTest { - @InjectMock - private JobService jobService; - - /** - * Dummy values - */ - private final Portfolio dummyPortfolio = new Portfolio(1, 1, "test", new Targets(Set.of(), 1)); - - private final Topology dummyTopology = new Topology(1, 1, "test", List.of(), Instant.now(), Instant.now()); - private final Trace dummyTrace = new Trace("bitbrains", "Bitbrains", "vm"); - private final Scenario dummyScenario = new Scenario( - 1, - 1, - dummyPortfolio, - "test", - new Workload(dummyTrace, 1.0), - dummyTopology, - new OperationalPhenomena(false, false), - "test"); - private final Job dummyJob = new Job(1, dummyScenario, JobState.PENDING, Instant.now(), Instant.now(), 0, null); - /** * Test that tries to query the pending jobs without token. */ @@ -90,7 +52,7 @@ public final class JobResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "test", roles = {"openid"}) public void testQueryInvalidScope() { when().get().then().statusCode(403); @@ -101,12 +63,10 @@ public final class JobResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "test", roles = {"runner"}) public void testQuery() { - Mockito.when(jobService.listPending()).thenReturn(List.of(dummyJob)); - - when().get().then().statusCode(200).contentType(ContentType.JSON).body("get(0).id", equalTo(1)); + when().get().then().statusCode(200).contentType(ContentType.JSON).body("get(0).state", equalTo("PENDING")); } /** @@ -114,12 +74,10 @@ public final class JobResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "test", roles = {"runner"}) public void testGetNonExisting() { - Mockito.when(jobService.findById(1)).thenReturn(null); - - when().get("/1").then().statusCode(404).contentType(ContentType.JSON); + when().get("/0").then().statusCode(404).contentType(ContentType.JSON); } /** @@ -127,11 +85,9 @@ public final class JobResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "test", roles = {"runner"}) public void testGetExisting() { - Mockito.when(jobService.findById(1)).thenReturn(dummyJob); - when().get("/1").then().statusCode(200).contentType(ContentType.JSON).body("id", equalTo(1)); } @@ -140,15 +96,13 @@ public final class JobResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "test", roles = {"runner"}) public void testUpdateNonExistent() { - Mockito.when(jobService.updateState(eq(1L), any(), anyInt(), any())).thenReturn(null); - - given().body(new Job.Update(JobState.PENDING, 0, null)) + given().body(new org.opendc.web.proto.runner.Job.Update(JobState.PENDING, 0, null)) .contentType(ContentType.JSON) .when() - .post("/1") + .post("/0") .then() .statusCode(404) .contentType(ContentType.JSON); @@ -159,16 +113,13 @@ public final class JobResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "test", roles = {"runner"}) public void testUpdateState() { - Mockito.when(jobService.updateState(eq(1L), any(), anyInt(), any())) - .thenReturn(new Job(1, dummyScenario, JobState.CLAIMED, Instant.now(), Instant.now(), 0, null)); - - given().body(new Job.Update(JobState.CLAIMED, 0, null)) + given().body(new org.opendc.web.proto.runner.Job.Update(JobState.CLAIMED, 0, null)) .contentType(ContentType.JSON) .when() - .post("/1") + .post("/2") .then() .statusCode(200) .contentType(ContentType.JSON) @@ -180,7 +131,7 @@ public final class JobResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "test", roles = {"runner"}) public void testUpdateInvalidInput() { given().body("{ \"test\": \"test\" }") diff --git a/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/PortfolioResourceTest.java b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/PortfolioResourceTest.java index cc3ac978..a952d83f 100644 --- a/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/PortfolioResourceTest.java +++ b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/PortfolioResourceTest.java @@ -24,24 +24,14 @@ package org.opendc.web.server.rest.user; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.equalTo; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; import io.quarkus.test.common.http.TestHTTPEndpoint; import io.quarkus.test.junit.QuarkusTest; -import io.quarkus.test.junit.mockito.InjectMock; import io.quarkus.test.security.TestSecurity; import io.restassured.http.ContentType; -import java.time.Instant; -import java.util.List; import java.util.Set; import org.junit.jupiter.api.Test; -import org.mockito.Mockito; import org.opendc.web.proto.Targets; -import org.opendc.web.proto.user.Portfolio; -import org.opendc.web.proto.user.Project; -import org.opendc.web.proto.user.ProjectRole; -import org.opendc.web.server.service.PortfolioService; /** * Test suite for {@link PortfolioResource}. @@ -49,27 +39,25 @@ import org.opendc.web.server.service.PortfolioService; @QuarkusTest @TestHTTPEndpoint(PortfolioResource.class) public final class PortfolioResourceTest { - @InjectMock - private PortfolioService portfolioService; - - /** - * Dummy project and portfolio - */ - private final Project dummyProject = new Project(1, "test", Instant.now(), Instant.now(), ProjectRole.OWNER); - - private final Portfolio dummyPortfolio = - new Portfolio(1, 1, dummyProject, "test", new Targets(Set.of(), 1), List.of()); - /** * Test that tries to obtain the list of portfolios belonging to a project. */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testGetForProject() { - Mockito.when(portfolioService.findByUser("testUser", 1)).thenReturn(List.of()); + given().pathParam("project", 1).when().get().then().statusCode(200).contentType(ContentType.JSON); + } + /** + * Test that tries to obtain the list of portfolios belonging to a project without authorization. + */ + @Test + @TestSecurity( + user = "unknown", + roles = {"openid"}) + public void testGetForProjectNoAuthorization() { given().pathParam("project", 1).when().get().then().statusCode(200).contentType(ContentType.JSON); } @@ -78,40 +66,53 @@ public final class PortfolioResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testCreateNonExistent() { - Mockito.when(portfolioService.create(eq("testUser"), eq(1), any())).thenReturn(null); + given().pathParam("project", "0") + .body(new org.opendc.web.proto.user.Portfolio.Create("test", new Targets(Set.of(), 1))) + .contentType(ContentType.JSON) + .when() + .post() + .then() + .statusCode(404) + .contentType(ContentType.JSON); + } + /** + * Test that tries to create a topology for a project. + */ + @Test + @TestSecurity( + user = "viewer", + roles = {"openid"}) + public void testCreateNotPermitted() { given().pathParam("project", "1") - .body(new Portfolio.Create("test", new Targets(Set.of(), 1))) + .body(new org.opendc.web.proto.user.Portfolio.Create("test", new Targets(Set.of(), 1))) .contentType(ContentType.JSON) .when() .post() .then() - .statusCode(404) + .statusCode(403) .contentType(ContentType.JSON); } /** - * Test that tries to create a portfolio for a scenario. + * Test that tries to create a portfolio for a project. */ @Test @TestSecurity( - user = "testUser", + user = "editor", roles = {"openid"}) public void testCreate() { - Mockito.when(portfolioService.create(eq("testUser"), eq(1L), any())).thenReturn(dummyPortfolio); - given().pathParam("project", "1") - .body(new Portfolio.Create("test", new Targets(Set.of(), 1))) + .body(new org.opendc.web.proto.user.Portfolio.Create("test", new Targets(Set.of(), 1))) .contentType(ContentType.JSON) .when() .post() .then() .statusCode(200) .contentType(ContentType.JSON) - .body("id", equalTo(1)) .body("name", equalTo("test")); } @@ -120,7 +121,7 @@ public final class PortfolioResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "editor", roles = {"openid"}) public void testCreateEmpty() { given().pathParam("project", "1") @@ -138,11 +139,11 @@ public final class PortfolioResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "editor", roles = {"openid"}) public void testCreateBlankName() { given().pathParam("project", "1") - .body(new Portfolio.Create("", new Targets(Set.of(), 1))) + .body(new org.opendc.web.proto.user.Portfolio.Create("", new Targets(Set.of(), 1))) .contentType(ContentType.JSON) .when() .post() @@ -164,7 +165,7 @@ public final class PortfolioResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"runner"}) public void testGetInvalidToken() { given().pathParam("project", "1").when().get("/1").then().statusCode(403); @@ -175,12 +176,26 @@ public final class PortfolioResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testGetNonExisting() { - Mockito.when(portfolioService.findByUser("testUser", 1, 1)).thenReturn(null); - given().pathParam("project", "1") + .when() + .get("/0") + .then() + .statusCode(404) + .contentType(ContentType.JSON); + } + + /** + * Test that tries to obtain a portfolio for a non-existent project. + */ + @Test + @TestSecurity( + user = "owner", + roles = {"openid"}) + public void testGetNonExistingProject() { + given().pathParam("project", "0") .when() .get("/1") .then() @@ -193,11 +208,9 @@ public final class PortfolioResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testGetExisting() { - Mockito.when(portfolioService.findByUser("testUser", 1, 1)).thenReturn(dummyPortfolio); - given().pathParam("project", "1") .when() .get("/1") @@ -212,12 +225,21 @@ public final class PortfolioResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testDeleteNonExistent() { - Mockito.when(portfolioService.delete("testUser", 1, 1)).thenReturn(null); + given().pathParam("project", "1").when().delete("/0").then().statusCode(404); + } - given().pathParam("project", "1").when().delete("/1").then().statusCode(404); + /** + * Test to delete a portfolio on a non-existent project. + */ + @Test + @TestSecurity( + user = "owner", + roles = {"openid"}) + public void testDeleteNonExistentProject() { + given().pathParam("project", "0").when().delete("/1").then().statusCode(404); } /** @@ -225,16 +247,41 @@ public final class PortfolioResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testDelete() { - Mockito.when(portfolioService.delete("testUser", 1, 1)).thenReturn(dummyPortfolio); + int number = given().pathParam("project", "1") + .body(new org.opendc.web.proto.user.Portfolio.Create("Delete Portfolio", new Targets(Set.of(), 1))) + .contentType(ContentType.JSON) + .when() + .post() + .then() + .statusCode(200) + .contentType(ContentType.JSON) + .extract() + .path("number"); given().pathParam("project", "1") .when() - .delete("/1") + .delete("/" + number) .then() .statusCode(200) .contentType(ContentType.JSON); } + + /** + * Test to delete a portfolio as a viewer. + */ + @Test + @TestSecurity( + user = "viewer", + roles = {"openid"}) + public void testDeleteAsViewer() { + given().pathParam("project", "1") + .when() + .delete("/1") + .then() + .statusCode(403) + .contentType(ContentType.JSON); + } } diff --git a/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/PortfolioScenarioResourceTest.java b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/PortfolioScenarioResourceTest.java index 8cb95a98..4f8d412c 100644 --- a/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/PortfolioScenarioResourceTest.java +++ b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/PortfolioScenarioResourceTest.java @@ -24,32 +24,15 @@ package org.opendc.web.server.rest.user; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.equalTo; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyInt; -import static org.mockito.ArgumentMatchers.eq; import io.quarkus.test.common.http.TestHTTPEndpoint; import io.quarkus.test.junit.QuarkusTest; -import io.quarkus.test.junit.mockito.InjectMock; import io.quarkus.test.security.TestSecurity; import io.restassured.http.ContentType; -import java.time.Instant; -import java.util.List; -import java.util.Set; import org.junit.jupiter.api.Test; -import org.mockito.Mockito; -import org.opendc.web.proto.JobState; import org.opendc.web.proto.OperationalPhenomena; -import org.opendc.web.proto.Targets; -import org.opendc.web.proto.Trace; import org.opendc.web.proto.Workload; -import org.opendc.web.proto.user.Job; -import org.opendc.web.proto.user.Portfolio; -import org.opendc.web.proto.user.Project; -import org.opendc.web.proto.user.ProjectRole; import org.opendc.web.proto.user.Scenario; -import org.opendc.web.proto.user.Topology; -import org.opendc.web.server.service.ScenarioService; /** * Test suite for {@link PortfolioScenarioResource}. @@ -57,30 +40,6 @@ import org.opendc.web.server.service.ScenarioService; @QuarkusTest @TestHTTPEndpoint(PortfolioScenarioResource.class) public final class PortfolioScenarioResourceTest { - @InjectMock - private ScenarioService scenarioService; - - /** - * Dummy values - */ - private final Project dummyProject = new Project(0, "test", Instant.now(), Instant.now(), ProjectRole.OWNER); - - private final Portfolio.Summary dummyPortfolio = new Portfolio.Summary(1, 1, "test", new Targets(Set.of(), 1)); - private final Job dummyJob = new Job(1, JobState.PENDING, Instant.now(), Instant.now(), null); - private final Trace dummyTrace = new Trace("bitbrains", "Bitbrains", "vm"); - private final Topology.Summary dummyTopology = new Topology.Summary(1, 1, "test", Instant.now(), Instant.now()); - private final Scenario dummyScenario = new Scenario( - 1, - 1, - dummyProject, - dummyPortfolio, - "test", - new Workload(dummyTrace, 1.0), - dummyTopology, - new OperationalPhenomena(false, false), - "test", - dummyJob); - /** * Test that tries to obtain a portfolio without token. */ @@ -99,7 +58,7 @@ public final class PortfolioScenarioResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"runner"}) public void testGetInvalidToken() { given().pathParam("project", "1") @@ -111,15 +70,30 @@ public final class PortfolioScenarioResourceTest { } /** - * Test that tries to obtain a non-existent portfolio. + * Test that tries to obtain a scenario without authorization. */ @Test @TestSecurity( - user = "testUser", + user = "unknown", roles = {"openid"}) - public void testGet() { - Mockito.when(scenarioService.findAll("testUser", 1, 1)).thenReturn(List.of()); + public void testGetUnauthorized() { + given().pathParam("project", "1") + .pathParam("portfolio", "1") + .when() + .get() + .then() + .statusCode(200) + .contentType(ContentType.JSON); + } + /** + * Test that tries to obtain a scenario. + */ + @Test + @TestSecurity( + user = "owner", + roles = {"openid"}) + public void testGet() { given().pathParam("project", "1") .pathParam("portfolio", "1") .when() @@ -134,14 +108,31 @@ public final class PortfolioScenarioResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testCreateNonExistent() { - Mockito.when(scenarioService.create(eq("testUser"), eq(1L), anyInt(), any())) - .thenReturn(null); + given().pathParam("project", "1") + .pathParam("portfolio", "0") + .body(new Scenario.Create( + "test", new Workload.Spec("test", 1.0), 1, new OperationalPhenomena(false, false), "test")) + .contentType(ContentType.JSON) + .when() + .post() + .then() + .statusCode(404) + .contentType(ContentType.JSON); + } + /** + * Test that tries to create a scenario for a portfolio without authorization. + */ + @Test + @TestSecurity( + user = "unknown", + roles = {"openid"}) + public void testCreateUnauthorized() { given().pathParam("project", "1") - .pathParam("portfolio", "1") + .pathParam("portfolio", "0") .body(new Scenario.Create( "test", new Workload.Spec("test", 1.0), 1, new OperationalPhenomena(false, false), "test")) .contentType(ContentType.JSON) @@ -152,28 +143,48 @@ public final class PortfolioScenarioResourceTest { .contentType(ContentType.JSON); } + /** + * Test that tries to create a scenario for a portfolio as a viewer. + */ + @Test + @TestSecurity( + user = "viewer", + roles = {"openid"}) + public void testCreateAsViewer() { + given().pathParam("project", "1") + .pathParam("portfolio", "0") + .body(new Scenario.Create( + "test", new Workload.Spec("test", 1.0), 1, new OperationalPhenomena(false, false), "test")) + .contentType(ContentType.JSON) + .when() + .post() + .then() + .statusCode(403) + .contentType(ContentType.JSON); + } + /** * Test that tries to create a scenario for a portfolio. */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testCreate() { - Mockito.when(scenarioService.create(eq("testUser"), eq(1L), eq(1), any())) - .thenReturn(dummyScenario); - given().pathParam("project", "1") .pathParam("portfolio", "1") .body(new Scenario.Create( - "test", new Workload.Spec("test", 1.0), 1, new OperationalPhenomena(false, false), "test")) + "test", + new Workload.Spec("bitbrains-small", 1.0), + 1, + new OperationalPhenomena(false, false), + "test")) .contentType(ContentType.JSON) .when() .post() .then() .statusCode(200) .contentType(ContentType.JSON) - .body("id", equalTo(1)) .body("name", equalTo("test")); } @@ -182,7 +193,7 @@ public final class PortfolioScenarioResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testCreateEmpty() { given().pathParam("project", "1") @@ -201,7 +212,7 @@ public final class PortfolioScenarioResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testCreateBlankName() { given().pathParam("project", "1") @@ -215,4 +226,48 @@ public final class PortfolioScenarioResourceTest { .statusCode(400) .contentType(ContentType.JSON); } + + /** + * Test that tries to create a scenario for a portfolio. + */ + @Test + @TestSecurity( + user = "owner", + roles = {"openid"}) + public void testCreateUnknownTopology() { + given().pathParam("project", "1") + .pathParam("portfolio", "1") + .body(new Scenario.Create( + "test", + new Workload.Spec("bitbrains-small", 1.0), + -1, + new OperationalPhenomena(false, false), + "test")) + .contentType(ContentType.JSON) + .when() + .post() + .then() + .statusCode(400) + .contentType(ContentType.JSON); + } + + /** + * Test that tries to create a scenario for a portfolio. + */ + @Test + @TestSecurity( + user = "owner", + roles = {"openid"}) + public void testCreateUnknownTrace() { + given().pathParam("project", "1") + .pathParam("portfolio", "1") + .body(new Scenario.Create( + "test", new Workload.Spec("unknown", 1.0), 1, new OperationalPhenomena(false, false), "test")) + .contentType(ContentType.JSON) + .when() + .post() + .then() + .statusCode(400) + .contentType(ContentType.JSON); + } } diff --git a/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/ProjectResourceTest.java b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/ProjectResourceTest.java index 7ca314a6..8bd60808 100644 --- a/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/ProjectResourceTest.java +++ b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/ProjectResourceTest.java @@ -28,16 +28,9 @@ import static org.hamcrest.Matchers.equalTo; import io.quarkus.test.common.http.TestHTTPEndpoint; import io.quarkus.test.junit.QuarkusTest; -import io.quarkus.test.junit.mockito.InjectMock; import io.quarkus.test.security.TestSecurity; import io.restassured.http.ContentType; -import java.time.Instant; -import java.util.List; import org.junit.jupiter.api.Test; -import org.mockito.Mockito; -import org.opendc.web.proto.user.Project; -import org.opendc.web.proto.user.ProjectRole; -import org.opendc.web.server.service.ProjectService; /** * Test suite for [ProjectResource]. @@ -45,14 +38,6 @@ import org.opendc.web.server.service.ProjectService; @QuarkusTest @TestHTTPEndpoint(ProjectResource.class) public final class ProjectResourceTest { - @InjectMock - private ProjectService projectService; - - /** - * Dummy values. - */ - private final Project dummyProject = new Project(0, "test", Instant.now(), Instant.now(), ProjectRole.OWNER); - /** * Test that tries to obtain all projects without token. */ @@ -66,7 +51,7 @@ public final class ProjectResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"runner"}) public void testGetAllWithInvalidScope() { when().get().then().statusCode(403); @@ -77,12 +62,10 @@ public final class ProjectResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testGetAll() { - Mockito.when(projectService.findByUser("testUser")).thenReturn(List.of(dummyProject)); - - when().get().then().statusCode(200).contentType(ContentType.JSON).body("get(0).name", equalTo("test")); + when().get().then().statusCode(200).contentType(ContentType.JSON).body("get(0).name", equalTo("Test Project")); } /** @@ -90,25 +73,21 @@ public final class ProjectResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testGetNonExisting() { - Mockito.when(projectService.findByUser("testUser", 1)).thenReturn(null); - - when().get("/1").then().statusCode(404).contentType(ContentType.JSON); + when().get("/0").then().statusCode(404).contentType(ContentType.JSON); } /** - * Test that tries to obtain a job. + * Test that tries to obtain a project. */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testGetExisting() { - Mockito.when(projectService.findByUser("testUser", 1)).thenReturn(dummyProject); - - when().get("/1").then().statusCode(200).contentType(ContentType.JSON).body("id", equalTo(0)); + when().get("/1").then().statusCode(200).contentType(ContentType.JSON).body("id", equalTo(1)); } /** @@ -116,19 +95,16 @@ public final class ProjectResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testCreate() { - Mockito.when(projectService.create("testUser", "test")).thenReturn(dummyProject); - - given().body(new Project.Create("test")) + given().body(new org.opendc.web.proto.user.Project.Create("test")) .contentType(ContentType.JSON) .when() .post() .then() .statusCode(200) .contentType(ContentType.JSON) - .body("id", equalTo(0)) .body("name", equalTo("test")); } @@ -137,7 +113,7 @@ public final class ProjectResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testCreateEmpty() { given().body("{}") @@ -154,10 +130,10 @@ public final class ProjectResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testCreateBlankName() { - given().body(new Project.Create("")) + given().body(new org.opendc.web.proto.user.Project.Create("")) .contentType(ContentType.JSON) .when() .post() @@ -171,12 +147,10 @@ public final class ProjectResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testDeleteNonExistent() { - Mockito.when(projectService.delete("testUser", 1)).thenReturn(null); - - when().delete("/1").then().statusCode(404).contentType(ContentType.JSON); + when().delete("/0").then().statusCode(404).contentType(ContentType.JSON); } /** @@ -184,12 +158,20 @@ public final class ProjectResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testDelete() { - Mockito.when(projectService.delete("testUser", 1)).thenReturn(dummyProject); + int id = given().body(new org.opendc.web.proto.user.Project.Create("Delete Project")) + .contentType(ContentType.JSON) + .when() + .post() + .then() + .statusCode(200) + .contentType(ContentType.JSON) + .extract() + .path("id"); - when().delete("/1").then().statusCode(200).contentType(ContentType.JSON); + when().delete("/" + id).then().statusCode(200).contentType(ContentType.JSON); } /** @@ -197,12 +179,9 @@ public final class ProjectResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "viewer", roles = {"openid"}) public void testDeleteNonOwner() { - Mockito.when(projectService.delete("testUser", 1)) - .thenThrow(new IllegalArgumentException("User does not own project")); - when().delete("/1").then().statusCode(403).contentType(ContentType.JSON); } } diff --git a/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/ScenarioResourceTest.java b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/ScenarioResourceTest.java index 850236d6..a980e4e2 100644 --- a/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/ScenarioResourceTest.java +++ b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/ScenarioResourceTest.java @@ -27,55 +27,42 @@ import static org.hamcrest.Matchers.equalTo; import io.quarkus.test.common.http.TestHTTPEndpoint; import io.quarkus.test.junit.QuarkusTest; -import io.quarkus.test.junit.mockito.InjectMock; import io.quarkus.test.security.TestSecurity; +import io.restassured.builder.RequestSpecBuilder; import io.restassured.http.ContentType; -import java.time.Instant; -import java.util.Set; +import io.restassured.specification.RequestSpecification; import org.junit.jupiter.api.Test; -import org.mockito.Mockito; -import org.opendc.web.proto.JobState; import org.opendc.web.proto.OperationalPhenomena; -import org.opendc.web.proto.Targets; -import org.opendc.web.proto.Trace; import org.opendc.web.proto.Workload; -import org.opendc.web.proto.user.Job; -import org.opendc.web.proto.user.Portfolio; -import org.opendc.web.proto.user.Project; -import org.opendc.web.proto.user.ProjectRole; import org.opendc.web.proto.user.Scenario; -import org.opendc.web.proto.user.Topology; -import org.opendc.web.server.service.ScenarioService; /** - * Test suite for [ScenarioResource]. + * Test suite for {@link ScenarioResource}. */ @QuarkusTest @TestHTTPEndpoint(ScenarioResource.class) public final class ScenarioResourceTest { - @InjectMock - private ScenarioService scenarioService; + /** + * Test that tries to obtain all scenarios belonging to a project without authorization. + */ + @Test + @TestSecurity( + user = "unknown", + roles = {"openid"}) + public void testGetAllUnauthorized() { + given().pathParam("project", "1").when().get().then().statusCode(404).contentType(ContentType.JSON); + } /** - * Dummy values + * Test that tries to obtain all scenarios belonging to a project. */ - private final Project dummyProject = new Project(0, "test", Instant.now(), Instant.now(), ProjectRole.OWNER); - - private final Portfolio.Summary dummyPortfolio = new Portfolio.Summary(1, 1, "test", new Targets(Set.of(), 1)); - private final Job dummyJob = new Job(1, JobState.PENDING, Instant.now(), Instant.now(), null); - private final Trace dummyTrace = new Trace("bitbrains", "Bitbrains", "vm"); - private final Topology.Summary dummyTopology = new Topology.Summary(1, 1, "test", Instant.now(), Instant.now()); - private final Scenario dummyScenario = new Scenario( - 1, - 1, - dummyProject, - dummyPortfolio, - "test", - new Workload(dummyTrace, 1.0), - dummyTopology, - new OperationalPhenomena(false, false), - "test", - dummyJob); + @Test + @TestSecurity( + user = "owner", + roles = {"openid"}) + public void testGetAll() { + given().pathParam("project", "1").when().get().then().statusCode(200).contentType(ContentType.JSON); + } /** * Test that tries to obtain a scenario without token. @@ -90,7 +77,7 @@ public final class ScenarioResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"runner"}) public void testGetInvalidToken() { given().pathParam("project", "1").when().get("/1").then().statusCode(403); @@ -101,11 +88,25 @@ public final class ScenarioResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testGetNonExisting() { - Mockito.when(scenarioService.findOne("testUser", 1, 1)).thenReturn(null); + given().pathParam("project", "1") + .when() + .get("/0") + .then() + .statusCode(404) + .contentType(ContentType.JSON); + } + /** + * Test that tries to obtain a scenario. + */ + @Test + @TestSecurity( + user = "unknown", + roles = {"openid"}) + public void testGetExistingUnauthorized() { given().pathParam("project", "1") .when() .get("/1") @@ -119,11 +120,9 @@ public final class ScenarioResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testGetExisting() { - Mockito.when(scenarioService.findOne("testUser", 1, 1)).thenReturn(dummyScenario); - given().pathParam("project", "1") .when() .get("/1") @@ -138,27 +137,65 @@ public final class ScenarioResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testDeleteNonExistent() { - Mockito.when(scenarioService.delete("testUser", 1, 1)).thenReturn(null); + given().pathParam("project", "1").when().delete("/0").then().statusCode(404); + } + /** + * Test to delete a scenario without authorization. + */ + @Test + @TestSecurity( + user = "unknown", + roles = {"openid"}) + public void testDeleteUnauthorized() { given().pathParam("project", "1").when().delete("/1").then().statusCode(404); } + /** + * Test to delete a scenario as a viewer. + */ + @Test + @TestSecurity( + user = "viewer", + roles = {"openid"}) + public void testDeleteAsViewer() { + given().pathParam("project", "1").when().delete("/1").then().statusCode(403); + } + /** * Test to delete a scenario. */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testDelete() { - Mockito.when(scenarioService.delete("testUser", 1, 1)).thenReturn(dummyScenario); + RequestSpecification spec = new RequestSpecBuilder() + .setBasePath("/projects/1/portfolios/1/scenarios") + .build(); + + int number = given(spec) + .body(new Scenario.Create( + "test", + new Workload.Spec("bitbrains-small", 1.0), + 1, + new OperationalPhenomena(false, false), + "test")) + .contentType(ContentType.JSON) + .when() + .post() + .then() + .statusCode(200) + .contentType(ContentType.JSON) + .extract() + .path("number"); given().pathParam("project", "1") .when() - .delete("/1") + .delete("/" + number) .then() .statusCode(200) .contentType(ContentType.JSON); diff --git a/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/TopologyResourceTest.java b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/TopologyResourceTest.java index 2cc6ea4b..21e35b09 100644 --- a/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/TopologyResourceTest.java +++ b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/rest/user/TopologyResourceTest.java @@ -24,24 +24,14 @@ package org.opendc.web.server.rest.user; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.equalTo; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyInt; -import static org.mockito.ArgumentMatchers.anyLong; -import static org.mockito.ArgumentMatchers.eq; import io.quarkus.test.common.http.TestHTTPEndpoint; import io.quarkus.test.junit.QuarkusTest; -import io.quarkus.test.junit.mockito.InjectMock; import io.quarkus.test.security.TestSecurity; import io.restassured.http.ContentType; -import java.time.Instant; import java.util.List; import org.junit.jupiter.api.Test; -import org.mockito.Mockito; -import org.opendc.web.proto.user.Project; -import org.opendc.web.proto.user.ProjectRole; import org.opendc.web.proto.user.Topology; -import org.opendc.web.server.service.TopologyService; /** * Test suite for {@link TopologyResource}. @@ -49,27 +39,31 @@ import org.opendc.web.server.service.TopologyService; @QuarkusTest @TestHTTPEndpoint(TopologyResource.class) public final class TopologyResourceTest { - @InjectMock - private TopologyService topologyService; - /** - * Dummy project and topology. + * Test that tries to obtain the list of topologies of a project without proper authorization. */ - private final Project dummyProject = new Project(1, "test", Instant.now(), Instant.now(), ProjectRole.OWNER); - - private final Topology dummyTopology = - new Topology(1, 1, dummyProject, "test", List.of(), Instant.now(), Instant.now()); + @Test + @TestSecurity( + user = "unknown", + roles = {"openid"}) + public void testGetAllWithoutAuth() { + given().pathParam("project", "1") + .when() + .get() + .then() + .statusCode(200) + .contentType(ContentType.JSON) + .body(equalTo("[]")); + } /** * Test that tries to obtain the list of topologies belonging to a project. */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) - public void testGetForProject() { - Mockito.when(topologyService.findAll("testUser", 1)).thenReturn(List.of()); - + public void testGetAll() { given().pathParam("project", "1").when().get().then().statusCode(200).contentType(ContentType.JSON); } @@ -78,18 +72,34 @@ public final class TopologyResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testCreateNonExistent() { - Mockito.when(topologyService.create(eq("testUser"), eq(1L), any())).thenReturn(null); + given().pathParam("project", "0") + .body(new Topology.Create("test", List.of())) + .contentType(ContentType.JSON) + .when() + .post() + .then() + .statusCode(404) + .contentType(ContentType.JSON); + } + /** + * Test that tries to create a topology for a project as viewer. + */ + @Test + @TestSecurity( + user = "viewer", + roles = {"openid"}) + public void testCreateUnauthorized() { given().pathParam("project", "1") .body(new Topology.Create("test", List.of())) .contentType(ContentType.JSON) .when() .post() .then() - .statusCode(404) + .statusCode(403) .contentType(ContentType.JSON); } @@ -98,11 +108,9 @@ public final class TopologyResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testCreate() { - Mockito.when(topologyService.create(eq("testUser"), eq(1L), any())).thenReturn(dummyTopology); - given().pathParam("project", "1") .body(new Topology.Create("test", List.of())) .contentType(ContentType.JSON) @@ -111,7 +119,6 @@ public final class TopologyResourceTest { .then() .statusCode(200) .contentType(ContentType.JSON) - .body("id", equalTo(1)) .body("name", equalTo("test")); } @@ -120,7 +127,7 @@ public final class TopologyResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testCreateEmpty() { given().pathParam("project", "1") @@ -138,7 +145,7 @@ public final class TopologyResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testCreateBlankName() { given().pathParam("project", "1") @@ -164,7 +171,7 @@ public final class TopologyResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"runner"}) public void testGetInvalidToken() { given().pathParam("project", "1").when().get("/1").then().statusCode(403); @@ -175,11 +182,25 @@ public final class TopologyResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testGetNonExisting() { - Mockito.when(topologyService.findOne("testUser", 1, 1)).thenReturn(null); + given().pathParam("project", "1") + .when() + .get("/0") + .then() + .statusCode(404) + .contentType(ContentType.JSON); + } + /** + * Test that tries to obtain a topology without authorization. + */ + @Test + @TestSecurity( + user = "unknown", + roles = {"openid"}) + public void testGetUnauthorized() { given().pathParam("project", "1") .when() .get("/1") @@ -193,11 +214,9 @@ public final class TopologyResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testGetExisting() { - Mockito.when(topologyService.findOne("testUser", 1, 1)).thenReturn(dummyTopology); - given().pathParam("project", "1") .when() .get("/1") @@ -212,12 +231,26 @@ public final class TopologyResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testUpdateNonExistent() { - Mockito.when(topologyService.update(eq("testUser"), anyLong(), anyInt(), any())) - .thenReturn(null); + given().pathParam("project", "1") + .body(new Topology.Update(List.of())) + .contentType(ContentType.JSON) + .when() + .put("/0") + .then() + .statusCode(404); + } + /** + * Test to delete a topology without authorization. + */ + @Test + @TestSecurity( + user = "unknown", + roles = {"openid"}) + public void testUpdateUnauthorized() { given().pathParam("project", "1") .body(new Topology.Update(List.of())) .contentType(ContentType.JSON) @@ -227,17 +260,32 @@ public final class TopologyResourceTest { .statusCode(404); } + /** + * Test to update a topology as a viewer. + */ + @Test + @TestSecurity( + user = "viewer", + roles = {"openid"}) + public void testUpdateAsViewer() { + given().pathParam("project", "1") + .body(new Topology.Update(List.of())) + .contentType(ContentType.JSON) + .when() + .put("/1") + .then() + .statusCode(403) + .contentType(ContentType.JSON); + } + /** * Test to update a topology. */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testUpdate() { - Mockito.when(topologyService.update(eq("testUser"), anyLong(), anyInt(), any())) - .thenReturn(dummyTopology); - given().pathParam("project", "1") .body(new Topology.Update(List.of())) .contentType(ContentType.JSON) @@ -253,27 +301,56 @@ public final class TopologyResourceTest { */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testDeleteNonExistent() { - Mockito.when(topologyService.delete("testUser", 1, 1)).thenReturn(null); + given().pathParam("project", "1").when().delete("/0").then().statusCode(404); + } + /** + * Test to delete a topology without authorization. + */ + @Test + @TestSecurity( + user = "unknown", + roles = {"openid"}) + public void testDeleteUnauthorized() { given().pathParam("project", "1").when().delete("/1").then().statusCode(404); } + /** + * Test to delete a topology as a viewer. + */ + @Test + @TestSecurity( + user = "viewer", + roles = {"openid"}) + public void testDeleteAsViewer() { + given().pathParam("project", "1").when().delete("/1").then().statusCode(403); + } + /** * Test to delete a topology. */ @Test @TestSecurity( - user = "testUser", + user = "owner", roles = {"openid"}) public void testDelete() { - Mockito.when(topologyService.delete("testUser", 1, 1)).thenReturn(dummyTopology); + int number = given().pathParam("project", "1") + .body(new Topology.Create("Delete Topology", List.of())) + .contentType(ContentType.JSON) + .when() + .post() + .then() + .statusCode(200) + .contentType(ContentType.JSON) + .extract() + .path("number"); given().pathParam("project", "1") .when() - .delete("/1") + .delete("/" + number) .then() .statusCode(200) .contentType(ContentType.JSON); diff --git a/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/service/JobServiceTest.java b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/service/JobServiceTest.java new file mode 100644 index 00000000..f6d871c0 --- /dev/null +++ b/opendc-web/opendc-web-server/src/test/java/org/opendc/web/server/service/JobServiceTest.java @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2023 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.web.server.service; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.eq; + +import io.quarkus.test.junit.QuarkusTest; +import java.time.Instant; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.opendc.web.proto.JobState; +import org.opendc.web.server.model.Job; + +/** + * Test suite for the {@link JobService}. + */ +@QuarkusTest +public class JobServiceTest { + /** + * The {@link JobService} instance under test. + */ + private JobService service; + + /** + * The mock {@link UserAccountingService}. + */ + private UserAccountingService mockAccountingService; + + @BeforeEach + public void setUp() { + mockAccountingService = Mockito.mock(UserAccountingService.class); + service = new JobService(mockAccountingService); + } + + @Test + public void testUpdateInvalidTransition() { + Job job = new Job(null, "test", Instant.now(), 1); + job.state = JobState.RUNNING; + + assertThrows(IllegalArgumentException.class, () -> service.updateJob(job, JobState.CLAIMED, 0, null)); + + Mockito.verifyNoInteractions(mockAccountingService); + } + + @Test + public void testUpdateNoBudget() { + Job job = Mockito.spy(new Job(null, "test", Instant.now(), 1)); + job.state = JobState.RUNNING; + + Mockito.when(mockAccountingService.consumeSimulationBudget(any(), anyInt())) + .thenReturn(true); + Mockito.doReturn(true).when(job).updateAtomically(any(), any(), anyInt(), any()); + + service.updateJob(job, JobState.RUNNING, 0, null); + + Mockito.verify(job).updateAtomically(eq(JobState.FAILED), any(), anyInt(), any()); + } + + @Test + public void testUpdateNoBudgetWhenFinishing() { + Job job = Mockito.spy(new Job(null, "test", Instant.now(), 1)); + job.state = JobState.RUNNING; + + Mockito.when(mockAccountingService.consumeSimulationBudget(any(), anyInt())) + .thenReturn(true); + Mockito.doReturn(true).when(job).updateAtomically(any(), any(), anyInt(), any()); + + service.updateJob(job, JobState.FINISHED, 0, null); + + Mockito.verify(job).updateAtomically(eq(JobState.FINISHED), any(), anyInt(), any()); + } + + @Test + public void testUpdateSuccess() { + Job job = Mockito.spy(new Job(null, "test", Instant.now(), 1)); + job.state = JobState.RUNNING; + + Mockito.when(mockAccountingService.consumeSimulationBudget(any(), anyInt())) + .thenReturn(false); + Mockito.doReturn(true).when(job).updateAtomically(any(), any(), anyInt(), any()); + + service.updateJob(job, JobState.FINISHED, 0, null); + + Mockito.verify(job).updateAtomically(eq(JobState.FINISHED), any(), anyInt(), any()); + } + + @Test + public void testUpdateConflict() { + Job job = Mockito.spy(new Job(null, "test", Instant.now(), 1)); + job.state = JobState.RUNNING; + + Mockito.when(mockAccountingService.consumeSimulationBudget(any(), anyInt())) + .thenReturn(false); + Mockito.doReturn(false).when(job).updateAtomically(any(), any(), anyInt(), any()); + + assertThrows(IllegalStateException.class, () -> service.updateJob(job, JobState.FINISHED, 0, null)); + + Mockito.verify(job).updateAtomically(eq(JobState.FINISHED), any(), anyInt(), any()); + } +} -- cgit v1.2.3