Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@

import lombok.extern.slf4j.Slf4j;
import org.breedinginsight.dao.db.tables.daos.ExperimentProgramUserRoleDao;
import org.breedinginsight.dao.db.tables.pojos.ExperimentProgramUserRoleEntity;
import org.jooq.Configuration;
import org.jooq.DSLContext;

import javax.inject.Inject;
import javax.inject.Singleton;

import java.time.OffsetDateTime;
import java.util.UUID;

import static org.breedinginsight.dao.db.Tables.EXPERIMENT_PROGRAM_USER_ROLE;

@Slf4j
@Singleton
public class ExperimentalCollaboratorDAO extends ExperimentProgramUserRoleDao {
Expand All @@ -36,4 +42,22 @@ public ExperimentalCollaboratorDAO(Configuration config, DSLContext dsl) {
super(config);
this.dsl = dsl;
}

public ExperimentProgramUserRoleEntity create(UUID experimentId, UUID programUserRoleId, UUID userId) {
return dsl.insertInto(EXPERIMENT_PROGRAM_USER_ROLE)
.columns(EXPERIMENT_PROGRAM_USER_ROLE.EXPERIMENT_ID,
EXPERIMENT_PROGRAM_USER_ROLE.PROGRAM_USER_ROLE_ID,
EXPERIMENT_PROGRAM_USER_ROLE.CREATED_BY,
EXPERIMENT_PROGRAM_USER_ROLE.CREATED_AT,
EXPERIMENT_PROGRAM_USER_ROLE.UPDATED_BY,
EXPERIMENT_PROGRAM_USER_ROLE.UPDATED_AT)
.values(experimentId,
programUserRoleId,
userId,
OffsetDateTime.now(),
userId,
OffsetDateTime.now())
.returning(EXPERIMENT_PROGRAM_USER_ROLE.fields())
.fetchOneInto(ExperimentProgramUserRoleEntity.class);
}
}
14 changes: 14 additions & 0 deletions src/main/java/org/breedinginsight/daos/ProgramUserDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,18 @@ public List<ProgramUser> parseRecords(Result<Record> records, BiUserTable create

return resultProgramsUsers;
}

public List<ProgramUser> getProgramUsersByRole(UUID programId, UUID roleId) {
BiUserTable createdByUser = BI_USER.as("createdByUser");
BiUserTable updatedByUser = BI_USER.as("updatedByUser");

Result<Record> records = getProgramUsersQuery(createdByUser, updatedByUser)
.where(PROGRAM.ACTIVE.eq(true))
.and(PROGRAM_USER_ROLE.ACTIVE.eq(true))
.and(PROGRAM.ID.eq(programId))
.and(ROLE.ID.eq(roleId))
.fetch();

return parseRecords(records, createdByUser, updatedByUser);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@
package org.breedinginsight.services;

import lombok.extern.slf4j.Slf4j;
import org.breedinginsight.dao.db.tables.pojos.ExperimentProgramUserRoleEntity;
import org.breedinginsight.daos.ExperimentalCollaboratorDAO;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.List;
import java.util.UUID;

@Slf4j
@Singleton
public class ExperimentalCollaboratorService {

private final ExperimentalCollaboratorDAO experimentalCollaboratorDAO;
Expand All @@ -31,4 +36,18 @@ public class ExperimentalCollaboratorService {
public ExperimentalCollaboratorService(ExperimentalCollaboratorDAO experimentalCollaboratorDAO) {
this.experimentalCollaboratorDAO = experimentalCollaboratorDAO;
}

public ExperimentProgramUserRoleEntity createExperimentalCollaborator(UUID programUserRoleId, UUID experimentId, UUID userId) {
return this.experimentalCollaboratorDAO.create(experimentId, programUserRoleId, userId);
}

public List<ExperimentProgramUserRoleEntity> getExperimentalCollaborators(UUID experimentId) {
// Get all collaborators for an experiment.
return this.experimentalCollaboratorDAO.fetchByExperimentId(experimentId);
}

public void deleteExperimentalCollaborator(UUID collaboratorId) {
// Note: collaboratorId is the PK of the experiment_program_user_role table.
this.experimentalCollaboratorDAO.deleteById(collaboratorId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,11 @@ public boolean existsAndActive(UUID programId, UUID userId) {
return programUserDao.existsAndActive(programId, userId);
}

public List<ProgramUser> getProgramUsersByRole(UUID programId, UUID roleId) throws DoesNotExistException {
if (!programService.exists(programId)) {
throw new DoesNotExistException("Program id does not exist");
}

return programUserDao.getProgramUsersByRole(programId, roleId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package org.breedinginsight.services;

import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import lombok.SneakyThrows;
import org.breedinginsight.DatabaseTest;
import io.kowalski.fannypack.FannyPack;
import org.breedinginsight.dao.db.tables.pojos.ExperimentProgramUserRoleEntity;
import org.jooq.DSLContext;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;

import javax.inject.Inject;
import java.util.List;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.*;

@MicronautTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class ExperimentalCollaboratorServiceTest extends DatabaseTest {

private FannyPack fp;

@Inject
private DSLContext dsl;

@Inject
private ExperimentalCollaboratorService experimentalCollaboratorService;


@BeforeAll
public void setup() {

// Insert test data into the db
fp = FannyPack.fill("src/test/resources/sql/ExperimentalCollaboratorServiceTest.sql");

// Create user.
dsl.execute(fp.get("CreateUser"));

// Create program.
dsl.execute(fp.get("CreateProgram"));

// Add user to program in the Experimental Collaborator role.
dsl.execute(fp.get("AddUserToProgram"));
}

@Test
@SneakyThrows
void testExperimentalCollaboratorCRUD() {
// TODO: currently only Created, Read, and Delete are implemented. Once update is implemented, test that too.
// Create experiment_program_user_role row using the service method, and ensure it was created and returned as expected.

UUID experimentId = UUID.fromString("12d8aaf8-d0d9-4837-a71e-aa64b47885ae");
UUID programUserRoleId = UUID.fromString("0fb8ecf4-3a16-40c6-9c7f-cdfc945967a1");
UUID userId = UUID.fromString("00000000-0000-0000-0000-000000000000");

// Test create.
ExperimentProgramUserRoleEntity created = experimentalCollaboratorService.createExperimentalCollaborator(
programUserRoleId,
experimentId,
userId
);

assertNotNull(created);
assertEquals(experimentId, created.getExperimentId());
assertEquals(programUserRoleId, created.getProgramUserRoleId());
assertEquals(userId, created.getCreatedBy());
assertEquals(userId, created.getUpdatedBy());

// Test read.
ExperimentProgramUserRoleEntity retrieved = experimentalCollaboratorService.getExperimentalCollaborators(experimentId).get(0);

assertNotNull(retrieved);

assertEquals(created.getId(), retrieved.getId());
assertEquals(created.getProgramUserRoleId(), retrieved.getProgramUserRoleId());
assertEquals(created.getExperimentId(), retrieved.getExperimentId());
assertEquals(created.getCreatedBy(), retrieved.getCreatedBy());
assertEquals(created.getUpdatedBy(), retrieved.getUpdatedBy());
assertEquals(created.getCreatedAt(), retrieved.getCreatedAt());
assertEquals(created.getUpdatedAt(), retrieved.getUpdatedAt());

// TODO: test update.

// Test delete.
experimentalCollaboratorService.deleteExperimentalCollaborator(created.getId());

List<ExperimentProgramUserRoleEntity> result = experimentalCollaboratorService.getExperimentalCollaborators(experimentId);
assertTrue(result.isEmpty());
}

}
34 changes: 34 additions & 0 deletions src/test/resources/sql/ExperimentalCollaboratorServiceTest.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
-- name: CopyrightNotice
/*
* See the NOTICE file distributed with this work for additional information
* regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/

-- name: CreateUser
INSERT INTO bi_user (id, orcid, name, email, created_by, updated_by, active)
VALUES ('594ec70e-0476-4c40-baf5-581ab0cfcd75', '0000-0001-2345-6789', 'Tester', 'tester@mailinator.com', '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000', true);

-- name: CreateProgram
INSERT INTO program (id, species_id, name, created_by, updated_by, active, key, germplasm_sequence, exp_sequence, env_sequence)
SELECT '33a69523-b7b2-4867-94af-a1352c4a69d4', species.id, 'Trail Mix', '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000', true, 'TMTEST', 'tmtest_germplasm_sequence', 'tmtest_exp_sequence', 'tmtest_env_sequence'
FROM species WHERE species.common_name = 'Grape';

-- name: AddUserToProgram
INSERT INTO program_user_role (id, program_id, user_id, role_id, created_by, updated_by, active)
SELECT '0fb8ecf4-3a16-40c6-9c7f-cdfc945967a1', program.id, bi_user.id, role.id, '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000', true
FROM
bi_user
JOIN role ON bi_user.name = 'Tester' and role.domain = 'Experimental Collaborator'
JOIN program ON program.name = 'Trail Mix';