001/*
002 * Copyright 2020 Global Biodiversity Information Facility (GBIF)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.gbif.api.model.common;
017
018import org.gbif.api.SerdeTestUtils;
019import org.gbif.api.vocabulary.UserRole;
020
021import java.io.IOException;
022import java.util.HashMap;
023import java.util.HashSet;
024import java.util.Locale;
025import java.util.Map;
026import java.util.Set;
027
028import jakarta.validation.ConstraintViolation;
029import jakarta.validation.Validation;
030import jakarta.validation.Validator;
031
032import org.junit.jupiter.api.Test;
033
034import static org.junit.jupiter.api.Assertions.assertEquals;
035import static org.junit.jupiter.api.Assertions.assertFalse;
036import static org.junit.jupiter.api.Assertions.assertTrue;
037
038/**
039 * Tests related to {@link GbifUser}.
040 */
041public class GbifUserTest {
042
043  private final Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
044
045  @Test
046  public void testHasRole() {
047    GbifUser u = new GbifUser();
048    u.setUserName("betty");
049    u.setEmail("betty@gbif.org");
050    u.setFirstName("Betty Ford");
051
052    assertFalse(u.hasRole(UserRole.USER));
053    assertFalse(u.hasRole(UserRole.REGISTRY_EDITOR));
054    assertFalse(u.hasRole(UserRole.REGISTRY_ADMIN));
055    assertFalse(u.hasRole(null));
056
057    u.addRole(UserRole.USER);
058    assertTrue(u.hasRole(UserRole.USER));
059    assertFalse(u.hasRole(UserRole.REGISTRY_EDITOR));
060    assertFalse(u.hasRole(UserRole.REGISTRY_ADMIN));
061    assertFalse(u.hasRole(null));
062
063    u.addRole(UserRole.REGISTRY_ADMIN);
064    assertTrue(u.hasRole(UserRole.USER));
065    assertFalse(u.hasRole(UserRole.REGISTRY_EDITOR));
066    assertTrue(u.hasRole(UserRole.REGISTRY_ADMIN));
067    assertFalse(u.hasRole(null));
068  }
069
070  @Test
071  public void testValidation() {
072    GbifUser u = new GbifUser();
073    u.setKey(100);
074    u.setUserName("be");
075    u.setEmail("betty@gbif.org");
076    u.setFirstName("Betty");
077    u.setLastName("Ford");
078    u.setPasswordHash("a");
079    assertValidation("userName", u);
080
081    u.setUserName("betty");
082    u.setEmail("betty at gbif.org");
083    assertValidation("email", u);
084  }
085
086  @Test
087  public void testEmailValidation() {
088    GbifUser u = new GbifUser();
089    u.setKey(100);
090    u.setUserName("betty");
091    u.setEmail("b.betty@pre.name-dash.org");
092    u.setFirstName("Betty");
093    u.setLastName("Ford");
094    u.setPasswordHash("a");
095    assertValidation(null, u);
096  }
097
098  private void assertValidation(String property, GbifUser u) {
099    Set<ConstraintViolation<GbifUser>> err = validator.validate(u);
100    System.out.print(err);
101    if (property == null) {
102      assertEquals(0, err.size());
103    } else {
104      assertEquals(1, err.size());
105      assertEquals(property, err.iterator().next().getPropertyPath().toString());
106    }
107  }
108
109  @Test
110  public void testSerDe() throws IOException {
111    GbifUser u = new GbifUser();
112    u.setKey(100);
113    u.setUserName("be");
114    u.setEmail("betty@gbif.org");
115    u.setFirstName("Betty");
116    u.setLastName("Ford");
117    SerdeTestUtils.testSerDe(u, GbifUser.class);
118  }
119
120  @Test
121  public void testSerDeSettings() throws IOException {
122    GbifUser u = new GbifUser();
123    u.setKey(100);
124    u.setUserName("be");
125    u.setEmail("betty@gbif.org");
126    u.setFirstName("Betty");
127    u.setLastName("Ford");
128    Map<String, String> settings = new HashMap<>();
129    settings.put("name", "tim");
130    u.setSettings(settings);
131    SerdeTestUtils.testSerDe(u, GbifUser.class);
132  }
133
134  @Test
135  public void testName() {
136    GbifUser u = new GbifUser();
137    u.setKey(100);
138    u.setUserName("be");
139    u.setEmail("betty@gbif.org");
140    u.setFirstName("Betty");
141    u.setLastName("Ford");
142    assertEquals("Betty Ford", u.getName());
143  }
144
145  @Test
146  public void testCopyConstructor() {
147    GbifUser u = new GbifUser();
148    u.setKey(100);
149    u.setUserName("be");
150    u.setEmail("betty@gbif.org");
151    u.setFirstName("Betty");
152    u.setLastName("Ford");
153
154    Map<String, String> settings = new HashMap<>();
155    settings.put("name", "tim");
156    settings.put("locale", Locale.SIMPLIFIED_CHINESE.toLanguageTag());
157    u.setSettings(settings);
158
159    Map<String, String> systemSettings = new HashMap<>();
160    systemSettings.put("hello", "world");
161    u.setSystemSettings(systemSettings);
162
163    Set<UserRole> roles = new HashSet<>();
164    roles.add(UserRole.USER);
165    u.setRoles(roles);
166
167    GbifUser copy = new GbifUser(u);
168
169    // change original
170    u.setKey(101);
171    u.setUserName("new");
172    u.setEmail("new@gbif.org");
173    u.setFirstName("John");
174    u.setLastName("Smith");
175
176    settings.put("another", "one");
177    settings.put("locale", Locale.TRADITIONAL_CHINESE.toLanguageTag());
178    u.setSettings(settings);
179
180    systemSettings.put("another", "two");
181    u.setSystemSettings(systemSettings);
182
183    roles.add(UserRole.REGISTRY_ADMIN);
184    u.setRoles(roles);
185
186    // assert copy not changed
187    assertEquals(Integer.valueOf(100), copy.getKey());
188    assertEquals("be", copy.getUserName());
189    assertEquals("betty@gbif.org", copy.getEmail());
190    assertEquals("Betty", copy.getFirstName());
191    assertEquals("Ford", copy.getLastName());
192    assertEquals(Locale.SIMPLIFIED_CHINESE, copy.getLocale());
193    assertEquals(2, copy.getSettings().size());
194    assertEquals("tim", copy.getSettings().get("name"));
195    assertEquals(Locale.SIMPLIFIED_CHINESE.toLanguageTag(), copy.getSettings().get("locale"));
196    assertEquals(1, copy.getSystemSettings().size());
197    assertEquals("world", copy.getSystemSettings().get("hello"));
198    assertEquals(1, copy.getRoles().size());
199  }
200}