Commit ee012aab authored by Matija Obreza's avatar Matija Obreza
Browse files

Fix: Never generate barcodes for system inventories

parent ba0c9556
......@@ -433,6 +433,10 @@ public class InventoryServiceImpl extends FilteredCRUDServiceImpl<Inventory, Inv
public String assignBarcode(Inventory inventory) {
inventory = get(inventory);
if (inventory.isSystemInventory()) {
throw new InvalidApiUsageException("System inventories do not have barcodes");
}
if (StringUtils.isNotBlank(inventory.getBarcode()))
return inventory.getBarcode(); // return existing barcode
......
......@@ -308,7 +308,7 @@ public class TaxonomyControllerTest extends AbstractApiV1Test {
.perform(post(TaxonomyController.SpeciesController.API_URL + "/filter?d=ASC&s=nameVerifiedDate")
.content(verboseMapper.writeValueAsString(filter))
.contentType(MediaType.APPLICATION_JSON))
.andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$", not(nullValue())))
......
......@@ -59,6 +59,7 @@ import org.gringlobal.persistence.AccessionPedigreeRepository;
import org.gringlobal.persistence.AccessionQuarantineRepository;
import org.gringlobal.persistence.AccessionRepository;
import org.gringlobal.persistence.AccessionSourceRepository;
import org.gringlobal.persistence.AppSettingRepository;
import org.gringlobal.persistence.CitationRepository;
import org.gringlobal.persistence.CooperatorRepository;
import org.gringlobal.persistence.GeographyRepository;
......@@ -74,6 +75,7 @@ import org.gringlobal.persistence.SysLangRepository;
import org.gringlobal.persistence.TaxonomyFamilyRepository;
import org.gringlobal.persistence.TaxonomyGenusRepository;
import org.gringlobal.persistence.TaxonomySpeciesRepository;
import org.gringlobal.service.AppSettingsService;
import org.gringlobal.service.InventoryService;
import org.gringlobal.test.base.AbstractServiceTest;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -161,6 +163,11 @@ public abstract class AbstractServicesTest extends AbstractServiceTest {
@Autowired
protected InventoryViabilityRepository inventoryViabilityRepository;
@Autowired
protected AppSettingsService appSettingsService;
@Autowired
protected AppSettingRepository appSettingRepository;
@Override
public void beforeTest() throws Exception {
super.beforeTest();
......
......@@ -15,6 +15,7 @@
*/
package org.gringlobal.test.service;
import static org.gringlobal.model.community.CommunityAppSettings.BARCODE_INVENTORY;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
......@@ -66,6 +67,7 @@ public class InventoryServiceTest extends AbstractServicesTest {
taxonomySpeciesRepository.deleteAll();
taxonomyGenusRepository.deleteAll();
taxonomyFamilyRepository.deleteAll();
appSettingRepository.deleteAllInBatch();
super.cleanup();
}
......@@ -611,4 +613,35 @@ public class InventoryServiceTest extends AbstractServicesTest {
inventoryService.assignMissingInventoryNumbers();
}
@Test
public void assignBarcode() throws Exception {
asAdmin(() -> {
appSettingsService.ensureAppSetting(BARCODE_INVENTORY.categoryTag, BARCODE_INVENTORY.name, "{{ randomUUID }}");
return true;
});
Accession accession = addAccessionToDB();
var systemInventory = inventoryRepository.getSystemInventory(accession);
assertThat(systemInventory, notNullValue());
assertThat(systemInventory.getBarcode(), nullValue());
var inventory = addInventoryToDB(accession, INVENTORY_NUMBER_PART1_DEFAULT, null, null);
assertThat(inventory, notNullValue());
assertThat(inventory.getBarcode(), nullValue());
// Assert that system inventories don't get a barcode
assertThrows("System inventories do not have barcodes", InvalidApiUsageException.class, () -> {
inventoryService.assignBarcode(systemInventory);
});
// Assign a barcode
var barcode = inventoryService.assignBarcode(inventory);
assertThat(barcode, not(nullValue()));
inventory = inventoryService.get(inventory.getId()); // Reload
assertThat(inventory.getBarcode(), is(barcode));
// Existing barcode is returned
assertThat(inventoryService.assignBarcode(inventory), is(barcode));
}
}
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