Commit 091121b7 authored by Thomas Beermann's avatar Thomas Beermann
Browse files

fix tests & add mongodb service to pipeline Fixes #1

parent 748ea6ff
Pipeline #78511 passed with stages
in 11 minutes and 24 seconds
......@@ -3,6 +3,7 @@ build:
- master
- tags
- merge_requests
- branches
stage: build
image: maven:3-jdk-11
script: mvn compile
......@@ -12,11 +13,14 @@ test:
- master
- tags
- merge_requests
- branches
stage: test
image: maven:3-jdk-11
services:
- mongo:4.0.25
script:
- 'export AAI_TOKEN=$(curl -u "helmholtz-marketplace:${CLIENT_SECRET}" -X POST "https://login.helmholtz.de/oauth2/token" -H "Content-Type: application/x-www-form-urlencoded" -d "grant_type=refresh_token&refresh_token=${AAI_REFRESH_TOKEN}&client_id=helmholtz-marketplace&client_secret=${CLIENT_SECRET}" | sed "s/{.*\"access_token\":\"\([^\"]*\).*}/\1/g")'
- mvn -Dtoken=$AAI_TOKEN test
- mvn -Dtoken=$AAI_TOKEN -Dspring.data.mongodb.host=mongo test
package:
only:
......
......@@ -9,6 +9,7 @@ import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import de.helmholtz.marketplace.cerebrum.utils.CerebrumEntityUuidGenerator;
import static de.helmholtz.marketplace.cerebrum.utils.CerebrumEntityUuidGenerator.generate;
import static de.helmholtz.marketplace.cerebrum.utils.CerebrumEntityUuidGenerator.isValid;
......@@ -31,6 +32,8 @@ public class Image extends AuditMetadata
public void setUuid(@Nullable String uuid)
{
this.uuid = Boolean.TRUE.equals(isValid(uuid)) ? uuid : generate("img");
this.uuid = Boolean.TRUE.equals(
CerebrumEntityUuidGenerator.isValid(uuid))
? uuid : generate("img");
}
}
......@@ -24,7 +24,7 @@ import static de.helmholtz.marketplace.cerebrum.utils.CerebrumEntityUuidGenerato
public class MarketUser extends AuditMetadata
{
@Schema(description = "Unique identifier of the market service.",
example = "svc-01eac6d7-0d35-1812-a3ed-24aec4231940", required = true)
example = "usr-01eac6d7-0d35-1812-a3ed-24aec4231940", required = true)
@Setter(AccessLevel.NONE)
@Id
private String uuid = generate("usr");
......@@ -43,11 +43,11 @@ public class MarketUser extends AuditMetadata
private String profileId;
@Schema(description = "")
@DBRef
@ForeignKey
private Set<String> affiliations = new TreeSet<>();
@Schema(description = "")
@DBRef
@ForeignKey
private Set<String> managedServices = new TreeSet<>();
public void addAffiliation(String org)
......
......@@ -19,7 +19,7 @@ import static de.helmholtz.marketplace.cerebrum.utils.CerebrumEntityUuidGenerato
@Setter(AccessLevel.PUBLIC)
@Getter(AccessLevel.PUBLIC)
@Document
public class Organization
public class Organization extends AuditMetadata
{
@Schema(description = "Unique identifier of the organisation",
example = "org-01eac6d7-0d35-1812-a3ed-24aec4231940", required = true)
......
......@@ -605,7 +605,7 @@ class MarketServiceControllerTest
.andExpect(jsonPath("$['uuid']").value("svc-5189a7bc-d630-11ea-87d0-0242ac130003"));
verify(mockRepository, times(1)).save(any(MarketService.class));
verify(mockRepository, times(1)).findByUuid("svc-5189a7bc-d630-11ea-87d0-0242ac130003");
verify(mockRepository, times(2)).findByUuid("svc-5189a7bc-d630-11ea-87d0-0242ac130003");
}
@Test void
......@@ -748,7 +748,7 @@ class MarketServiceControllerTest
.andExpect(jsonPath("$['uuid']").value("svc-5189a7bc-d630-11ea-87d0-0242ac130003"));
verify(mockRepository, times(1)).save(any(MarketService.class));
verify(mockRepository, times(1)).findByUuid("svc-5189a7bc-d630-11ea-87d0-0242ac130003");
verify(mockRepository, times(2)).findByUuid("svc-5189a7bc-d630-11ea-87d0-0242ac130003");
}
@Test void
......
......@@ -60,6 +60,7 @@ class MarketUserControllerTest
{
private static final String API_URI_PREFIX = "/api/v0";
private static final String USR_API_URI = API_URI_PREFIX + "/users";
private static final String PRN_API_URI = API_URI_PREFIX + "/persons";
@Value("${cerebrum.test.oauth2-token}") private String TOKEN;
@Autowired private MockMvc mvc;
@MockBean private MarketUserRepository mockRepository;
......@@ -143,6 +144,17 @@ class MarketUserControllerTest
return createNewUserWithUuiD(email, first, last, screenName, sub, null);
}
private Person createNewPersonWithUuid(
String email, String first, String last, String uuid)
{
Person p = new Person();
p.addEmail(email);
p.setFirstName(first);
p.setLastName(last);
p.setUuid(uuid);
return p;
}
private MarketUser createNewUserWithUuiD(
String email, String first, String last, String screenName, String sub, String uuid)
{
......@@ -386,18 +398,17 @@ class MarketUserControllerTest
@Test void
givenValidSortValue_whenGetRequestToUsers_thenOK() throws Exception
{
//given
// given
listUsers.sort(Comparator.comparing(
MarketUser::getScreenName, Comparator.reverseOrder()));
Pageable pageable = PageRequest.of(
0,20, Sort.by(Sort.Order.desc("lastName")));
0,20, Sort.by(Sort.Order.desc("sub")));
Page<MarketUser> page = new PageImpl<>(listUsers, pageable,200L);
given(mockRepository.findAll(pageable)).willReturn(page);
//when
mvc.perform(
get(USR_API_URI +"?sort=screenName.desc"))
get(USR_API_URI +"?sort=sub.desc"))
//then
.andExpect(status().isOk())
.andExpect(content().contentType("application/json"))
......@@ -417,8 +428,15 @@ class MarketUserControllerTest
"glages0",
"QJQwuNkp-GElX-aSqc-ddNx-HsvatNkUUSVN");
Person singlePerson = createNewPersonWithUuid("test@test.de", "Max", "Mustermann", usr.getProfileId());
given(mockRepository.save(any(MarketUser.class))).willReturn(usr);
mvc.perform(post(PRN_API_URI)
.header("Authorization", "Bearer " + TOKEN)
.accept("application/json")
.contentType("application/json")
.content(objectMapper.writeValueAsString(singlePerson)))
.andExpect(status().isCreated());
//when
mvc.perform(post(USR_API_URI)
.header("Authorization", "Bearer " + TOKEN)
......@@ -440,8 +458,16 @@ class MarketUserControllerTest
@Test void
givenValidMarketUserWithUuid_whenPostRequestToUsers_verifyOutput_and_businessLogicCall_thenCreated() throws Exception
{
Person singlePerson = createNewPersonWithUuid("test@test.de", "Max", "Mustermann", singleUser.getProfileId());
given(mockRepository.save(any(MarketUser.class))).willReturn(singleUser);
mvc.perform(post(PRN_API_URI)
.header("Authorization", "Bearer " + TOKEN)
.accept("application/json")
.contentType("application/json")
.content(objectMapper.writeValueAsString(singlePerson)))
.andExpect(status().isCreated());
mvc.perform(post(USR_API_URI)
.header("Authorization", "Bearer " + TOKEN)
.accept("application/json")
......@@ -542,7 +568,7 @@ class MarketUserControllerTest
{
String invalidSingleUser = "{\"email\": \"dgrabeham0@baidu.com\", \"firstName\": \"Doti\", " +
"\"screenName\": \"dmagnus0\", \"sub\": \"6sidSTqA-Vc96-jwvB-MnhL-zI3fnhltIvMp\", " +
"\"uuid\": \"usr-5189a7bc-d630-11ea-87d0-0242ac130003\"}";
"\"uuid\": \"usr-5189a7bc-d630-11ea-87d0-0242ac130003\", " + "\"invalid_field\": \"True\"";
given(mockRepository.save(any(MarketUser.class))).willReturn(singleUser);
......@@ -617,7 +643,7 @@ class MarketUserControllerTest
.andExpect(jsonPath("$['uuid']").value("usr-5189a7bc-d630-11ea-87d0-0242ac130003"));
verify(mockRepository, times(1)).save(any(MarketUser.class));
verify(mockRepository, times(1)).findByUuid("usr-5189a7bc-d630-11ea-87d0-0242ac130003");
verify(mockRepository, times(2)).findByUuid("usr-5189a7bc-d630-11ea-87d0-0242ac130003");
}
@Test void
......@@ -720,8 +746,7 @@ class MarketUserControllerTest
givenInvalidMarketUser_whenPutRequestToUsers_thenBadRequest() throws Exception
{
String invalidUser = "{\"firstName\":\"Garreth\"," +
"\"email\":\"glages0@tmall.com\"," +
"\"sub\":\"QJQwuNkp-GElX-aSqc-ddNx-HsvatNkUUSVN\"}";
"\"email\":\"glages0@tmall.com\"";
given(mockRepository.save(any(MarketUser.class))).willReturn(singleUser);
......@@ -746,8 +771,8 @@ class MarketUserControllerTest
//given
Map<String, String> patch = new HashMap<>();
patch.put("op", "replace");
patch.put("path", "/email");
patch.put("value", "fm@yahoo.com");
patch.put("path", "/screenName");
patch.put("value", "glages0");
Object[] validJsonPatch = {patch};
MarketUser patchedUser = createNewUserWithUuiD(
......@@ -757,11 +782,19 @@ class MarketUserControllerTest
"glages0",
"QJQwuNkp-GElX-aSqc-ddNx-HsvatNkUUSVN",
"usr-5189a7bc-d630-11ea-87d0-0242ac130003");
Person singlePerson = createNewPersonWithUuid("test@test.de", "Max", "Mustermann", singleUser.getProfileId());
given(mockRepository.save(any(MarketUser.class))).willReturn(singleUser);
given(mockRepository.findByUuid("usr-5189a7bc-d630-11ea-87d0-0242ac130003"))
.willReturn(java.util.Optional.of(singleUser));
given(mockRepository.save(any(MarketUser.class))).willReturn(patchedUser);
mvc.perform(post(PRN_API_URI)
.header("Authorization", "Bearer " + TOKEN)
.accept("application/json")
.contentType("application/json")
.content(objectMapper.writeValueAsString(singlePerson)))
.andExpect(status().isCreated());
//when
mvc.perform(patch(USR_API_URI + "/usr-5189a7bc-d630-11ea-87d0-0242ac130003")
.header("Authorization", "Bearer " + TOKEN)
......@@ -771,7 +804,7 @@ class MarketUserControllerTest
//then
.andExpect(status().isOk())
.andExpect(content().contentType("application/json"))
.andExpect(jsonPath("$['email']").value("fm@yahoo.com"))
.andExpect(jsonPath("$['screenName']").value("glages0"))
.andExpect(jsonPath("$['uuid']").value("usr-5189a7bc-d630-11ea-87d0-0242ac130003"));
verify(mockRepository, times(1)).save(any(MarketUser.class));
......
......@@ -51,6 +51,7 @@ class CerebrumBaseServiceTest
@MockBean private MarketUserRepository mockRepository;
private CerebrumBaseServiceStub cerebrumBaseServiceStub;
private ForeignKeyExecutorService foreignKeyExecutorService;
@BeforeAll
public void before()
......@@ -64,7 +65,7 @@ class CerebrumBaseServiceTest
UriComponentsBuilder builder = UriComponentsBuilder.newInstance();
UriComponentsBuilder uri = builder.scheme("https").host("example.com");
assertThatExceptionOfType(ResponseStatusException.class).isThrownBy(() ->
cerebrumBaseServiceStub.createEntity("welcome", mockRepository, uri));
cerebrumBaseServiceStub.createEntity("welcome", mockRepository, foreignKeyExecutorService, uri));
}
@Test void
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment