From bbb6f362bd017bbfefe5e77fbf2a73bf02c7d505 Mon Sep 17 00:00:00 2001 From: Paulo Gustavo Veiga Date: Sun, 21 Jan 2024 22:27:53 -0800 Subject: [PATCH] Fix test. --- .../test/rest/RestAccountControllerTest.java | 80 +++++---- .../test/rest/RestAdminITCase.java | 153 ------------------ .../com/wisemapping/test/rest/RestHelper.java | 28 +--- 3 files changed, 47 insertions(+), 214 deletions(-) delete mode 100644 wise-webapp/src/test/java/com/wisemapping/test/rest/RestAdminITCase.java diff --git a/wise-webapp/src/test/java/com/wisemapping/test/rest/RestAccountControllerTest.java b/wise-webapp/src/test/java/com/wisemapping/test/rest/RestAccountControllerTest.java index 7473c8de..697e5373 100644 --- a/wise-webapp/src/test/java/com/wisemapping/test/rest/RestAccountControllerTest.java +++ b/wise-webapp/src/test/java/com/wisemapping/test/rest/RestAccountControllerTest.java @@ -1,52 +1,62 @@ /* -* Copyright [2022] [wisemapping] -* -* Licensed under WiseMapping Public License, Version 1.0 (the "License"). -* It is basically the Apache License, Version 2.0 (the "License") plus the -* "powered by wisemapping" text requirement on every single page; -* you may not use this file except in compliance with the License. -* You may obtain a copy of the license at -* -* http://www.wisemapping.org/license -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * Copyright [2022] [wisemapping] + * + * Licensed under WiseMapping Public License, Version 1.0 (the "License"). + * It is basically the Apache License, Version 2.0 (the "License") plus the + * "powered by wisemapping" text requirement on every single page; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the license at + * + * http://www.wisemapping.org/license + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.wisemapping.test.rest; -import com.wisemapping.config.Application; +import com.wisemapping.config.common.CommonConfig; +import com.wisemapping.config.rest.RestAppConfig; +import com.wisemapping.rest.AdminController; +import com.wisemapping.rest.MindmapController; +import com.wisemapping.rest.UserController; import com.wisemapping.rest.model.RestUser; import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.http.*; -import org.springframework.web.client.RestTemplate; import java.net.URI; -import java.util.Collection; import static com.wisemapping.test.rest.RestHelper.*; import static org.junit.jupiter.api.Assertions.assertEquals; +@SpringBootTest(classes = {RestAppConfig.class, CommonConfig.class, MindmapController.class, AdminController.class, UserController.class}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) +public class RestAccountControllerTest { + private static final String ADMIN_USER = "admin@wisemapping.org"; + private static final String ADMIN_PASSWORD = "test"; -@SpringBootTest(classes = Application.class) -public class RestAccountITCase { + @Autowired + private TestRestTemplate restTemplate; + @Test public void deleteUser() { // Configure media types ... + final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON); - final RestTemplate adminTemplate = createTemplate(ADMIN_CREDENTIALS); + final TestRestTemplate adminRestTemplate = this.restTemplate.withBasicAuth(ADMIN_USER, ADMIN_PASSWORD); final RestUser dummyUser = createDummyUser(); - createUser(requestHeaders, adminTemplate, dummyUser); + createUser(requestHeaders, adminRestTemplate, dummyUser); // Delete user ... - final RestTemplate dummyTemplate = createTemplate(dummyUser.getEmail() + ":fooPassword"); + final TestRestTemplate dummyTemplate = this.restTemplate.withBasicAuth(dummyUser.getEmail(), "fooPassword"); dummyTemplate.delete(BASE_REST_URL + "/account"); // Is the user there ? @@ -58,11 +68,13 @@ public class RestAccountITCase { // } } - public String createNewUser(final @NotNull MediaType mediaType) { + @Test + public void createNewUser() { // Configure media types ... - final HttpHeaders requestHeaders = createHeaders(mediaType); - final RestTemplate templateRest = createTemplate(ADMIN_CREDENTIALS); + final HttpHeaders requestHeaders = createHeaders(MediaType.APPLICATION_JSON); + final TestRestTemplate templateRest = this.restTemplate.withBasicAuth(ADMIN_USER, ADMIN_PASSWORD); + // Fill user data ... final RestUser restUser = createDummyUser(); @@ -77,27 +89,25 @@ public class RestAccountITCase { // Find by email and check ... result = findUserByEmail(requestHeaders, templateRest, restUser.getEmail()); assertEquals(result.getBody().getEmail(), restUser.getEmail(), "Returned object object seems not be the same."); - - return restUser.getEmail(); } - private ResponseEntity findUser(HttpHeaders requestHeaders, RestTemplate templateRest, URI location) { - HttpEntity findUserEntity = new HttpEntity(requestHeaders); + private ResponseEntity findUser(HttpHeaders requestHeaders, TestRestTemplate templateRest, URI location) { + HttpEntity findUserEntity = new HttpEntity<>(requestHeaders); final String url = HOST_PORT + location; return templateRest.exchange(url, HttpMethod.GET, findUserEntity, RestUser.class); } - private ResponseEntity findUserByEmail(HttpHeaders requestHeaders, RestTemplate templateRest, final String email) { - HttpEntity findUserEntity = new HttpEntity(requestHeaders); + private ResponseEntity findUserByEmail(HttpHeaders requestHeaders, TestRestTemplate templateRest, final String email) { + HttpEntity findUserEntity = new HttpEntity<>(requestHeaders); // Add extension only to avoid the fact that the last part is extracted ... final String url = BASE_REST_URL + "/admin/users/email/{email}"; return templateRest.exchange(url, HttpMethod.GET, findUserEntity, RestUser.class, email); } - private URI createUser(HttpHeaders requestHeaders, RestTemplate templateRest, RestUser restUser) { - HttpEntity createUserEntity = new HttpEntity< >(restUser, requestHeaders); + private URI createUser(@NotNull HttpHeaders requestHeaders, TestRestTemplate templateRest, RestUser restUser) { + final HttpEntity createUserEntity = new HttpEntity<>(restUser, requestHeaders); return templateRest.postForLocation(BASE_REST_URL + "/admin/users", createUserEntity); } diff --git a/wise-webapp/src/test/java/com/wisemapping/test/rest/RestAdminITCase.java b/wise-webapp/src/test/java/com/wisemapping/test/rest/RestAdminITCase.java deleted file mode 100644 index e318043c..00000000 --- a/wise-webapp/src/test/java/com/wisemapping/test/rest/RestAdminITCase.java +++ /dev/null @@ -1,153 +0,0 @@ -/* - * Copyright [2022] [wisemapping] - * - * Licensed under WiseMapping Public License, Version 1.0 (the "License"). - * It is basically the Apache License, Version 2.0 (the "License") plus the - * "powered by wisemapping" text requirement on every single page; - * you may not use this file except in compliance with the License. - * You may obtain a copy of the license at - * - * http://www.wisemapping.org/license - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.wisemapping.test.rest; - - -import com.wisemapping.rest.model.RestUser; -import org.jetbrains.annotations.NotNull; -import org.springframework.http.*; -import org.springframework.web.client.RestTemplate; -import org.testng.annotations.Test; - -import java.net.URI; - -import static com.wisemapping.test.rest.RestHelper.*; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.fail; - - -@Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function") -public class RestAdminITCase { - - String authorisation = "admin@wisemapping.org" + ":" + "test"; - - @Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function") - public void changePassword(final @NotNull MediaType mediaType) { // Configure media types ... - final HttpHeaders requestHeaders = createHeaders(mediaType); - final RestTemplate templateRest = createTemplate(authorisation); - - // Fill user data ... - final RestUser restUser = createDummyUser(); - - // User has been created ... - final URI location = createUser(requestHeaders, templateRest, restUser); - - // Check that the user has been created ... - ResponseEntity result = findUser(requestHeaders, templateRest, location); - - // Change password ... - requestHeaders.setContentType(MediaType.TEXT_PLAIN); - HttpEntity createUserEntity = new HttpEntity("some-new-password", requestHeaders); - templateRest.put(BASE_REST_URL + "/admin/users/{id}/password", createUserEntity, result.getBody().getId()); - } - - - @Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function") - public void deleteUser(final @NotNull MediaType mediaType) { // Configure media types ... - final HttpHeaders requestHeaders = createHeaders(mediaType); - final RestTemplate templateRest = createTemplate(authorisation); - - final RestUser restUser = createDummyUser(); - - // User has been created ... - final URI location = createUser(requestHeaders, templateRest, restUser); - - // Check that the user has been created ... - ResponseEntity result = findUser(requestHeaders, templateRest, location); - - // Delete user ... - templateRest.delete(BASE_REST_URL + "/admin/users/{id}", result.getBody().getId()); - - // Is the user there ? - // Check that the user has been created ... - try { - findUser(requestHeaders, templateRest, location); - fail("User could not be deleted !"); - } catch (Exception e) { - } - } - - public RestUser createNewUserAndGetUser(final @NotNull MediaType mediaType) { - - // Configure media types ... - final HttpHeaders requestHeaders = createHeaders(mediaType); - final RestTemplate templateRest = createTemplate(authorisation); - - // Fill user data ... - final RestUser restUser = createDummyUser(); - - // Create a new user ... - final URI location = createUser(requestHeaders, templateRest, restUser); - - // Check that the user has been created ... - ResponseEntity result = findUser(requestHeaders, templateRest, location); - assertEquals(result.getBody().getEmail(), restUser.getEmail(), "Returned object object seems not be the same."); - - return result.getBody(); - } - - public String createNewUser(final @NotNull MediaType mediaType) { - - // Fill user data ... - final RestUser restUser = createNewUserAndGetUser(mediaType); - // Find by email and check ... - // @todo: review find by email... It's failing with 406 -// findUser(requestHeaders, templateRest, location); -// result = findUserByEmail(requestHeaders, templateRest, restUser.getEmail()); -// assertEquals(result.getBody().getEmail(), restUser.getEmail(), "Returned object object seems not be the same."); - - return restUser.getEmail(); - } - - @Test(dataProviderClass = RestHelper.class, dataProvider = "ContentType-Provider-Function") - public void createUser(final @NotNull MediaType mediaType) { - this.createNewUser(mediaType); - } - - private ResponseEntity findUser(HttpHeaders requestHeaders, RestTemplate templateRest, URI location) { - HttpEntity findUserEntity = new HttpEntity(requestHeaders); - final String url = HOST_PORT + location; - return templateRest.exchange(url, HttpMethod.GET, findUserEntity, RestUser.class); - } - - public ResponseEntity findUserByEmail(HttpHeaders requestHeaders, RestTemplate templateRest, final String email) { - HttpEntity findUserEntity = new HttpEntity<>(requestHeaders); - - // Add extension only to avoid the fact that the last part is extracted ... - final String url = BASE_REST_URL + "/admin/users/email/{email}"; - return templateRest.exchange(url, HttpMethod.GET, findUserEntity, RestUser.class, email); - } - - private URI createUser(HttpHeaders requestHeaders, RestTemplate templateRest, RestUser restUser) { - HttpEntity createUserEntity = new HttpEntity(restUser, requestHeaders); - return templateRest.postForLocation(BASE_REST_URL + "/admin/users", createUserEntity); - } - - private RestUser createDummyUser() { - final RestUser restUser = new RestUser(); - final String username = "foo-to-delete" + System.nanoTime(); - final String email = username + "@example.org"; - restUser.setEmail(email); - restUser.setFirstname("foo first name"); - restUser.setLastname("foo last name"); - restUser.setPassword("admin"); - return restUser; - } - -} diff --git a/wise-webapp/src/test/java/com/wisemapping/test/rest/RestHelper.java b/wise-webapp/src/test/java/com/wisemapping/test/rest/RestHelper.java index 5705add5..f939cbab 100644 --- a/wise-webapp/src/test/java/com/wisemapping/test/rest/RestHelper.java +++ b/wise-webapp/src/test/java/com/wisemapping/test/rest/RestHelper.java @@ -1,15 +1,12 @@ package com.wisemapping.test.rest; import org.jetbrains.annotations.NotNull; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; -import org.springframework.http.client.SimpleClientHttpRequestFactory; -import org.springframework.security.crypto.codec.Base64; import org.springframework.web.client.RestTemplate; -import org.testng.annotations.DataProvider; -import java.io.IOException; -import java.net.HttpURLConnection; import java.util.ArrayList; import java.util.List; @@ -17,8 +14,6 @@ public class RestHelper { public static final String HOST_PORT = "http://localhost:8081"; public static final String BASE_REST_URL = HOST_PORT + "/api/restfull"; - public static final String ADMIN_CREDENTIALS = "admin@wisemapping.org" + ":" + "test"; - public static final String COLOR = "#000000"; static HttpHeaders createHeaders(@NotNull MediaType mediaType) { List acceptableMediaTypes = new ArrayList<>(); @@ -30,23 +25,4 @@ public class RestHelper { return result; } - static RestTemplate createTemplate(@NotNull final String authorisation) { - SimpleClientHttpRequestFactory s = new SimpleClientHttpRequestFactory() { - @Override - protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException { - super.prepareConnection(connection, httpMethod); - - byte[] encodedAuthorisation = Base64.encode(authorisation.getBytes()); - connection.setRequestProperty("Authorization", "Basic " + new String(encodedAuthorisation)); - } - - }; - return new RestTemplate(s); - } - - @DataProvider(name = "ContentType-Provider-Function") - static Object[][] contentTypes() { - return new Object[][]{{MediaType.APPLICATION_JSON}}; - } - }