Skip to content

Commit

Permalink
EcmObjectService correctly treats type name as case insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
RHarryH committed Feb 10, 2024
1 parent f8540d3 commit 9c482c7
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/com/avispa/ecm/model/EcmObjectService.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class EcmObjectService {
public EcmObject getEcmObjectFrom(UUID id, String typeName) {
EcmObject entity = ecmObjectRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException(String.format("The object '%s' does not exist", id)));
String foundTypeName = typeService.getTypeName(entity.getClass());
if(!foundTypeName.equals(typeName)) {
if (!foundTypeName.equalsIgnoreCase(typeName)) {
throw new EcmException(String.format("The object '%s' is not an object of '%s' type", id, typeName));
}

Expand Down
79 changes: 79 additions & 0 deletions src/test/java/com/avispa/ecm/model/EcmObjectServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Avispa ECM - a small framework for implementing basic ECM solution
* Copyright (C) 2024 Rafał Hiszpański
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.avispa.ecm.model;

import com.avispa.ecm.model.type.TypeService;
import com.avispa.ecm.util.TestDocument;
import com.avispa.ecm.util.exception.EcmException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Optional;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

/**
* @author Rafał Hiszpański
*/
@ExtendWith(MockitoExtension.class)
class EcmObjectServiceTest {
@Mock
private EcmObjectRepository<EcmObject> ecmObjectRepository;

@Mock
private TypeService typeService;

@InjectMocks
private EcmObjectService ecmObjectService;

@Test
void givenIdAndTypeName_whenGetObject_thenReturnObject() {
var testDocument = new TestDocument();
when(ecmObjectRepository.findById(any(UUID.class))).thenReturn(Optional.of(testDocument));
when(typeService.getTypeName(TestDocument.class)).thenReturn("Test document");

assertEquals(testDocument, ecmObjectService.getEcmObjectFrom(UUID.randomUUID(), "Test document"));
}

@Test
void givenIdAndTypeName_whenGetObjectWithLowerCaseTypeName_thenReturnObject() {
var testDocument = new TestDocument();
when(ecmObjectRepository.findById(any(UUID.class))).thenReturn(Optional.of(testDocument));
when(typeService.getTypeName(TestDocument.class)).thenReturn("test document");

assertEquals(testDocument, ecmObjectService.getEcmObjectFrom(UUID.randomUUID(), "Test document"));
}

@Test
void givenIdAndTypeName_whenTypeDoesNotMatch_thenThrowException() {
var testDocument = new TestDocument();
when(ecmObjectRepository.findById(any(UUID.class))).thenReturn(Optional.of(testDocument));
when(typeService.getTypeName(TestDocument.class)).thenReturn("Document");

var id = UUID.randomUUID();
assertThrows(EcmException.class, () -> ecmObjectService.getEcmObjectFrom(id, "Test document"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ void fieldTest() {
assertNotNull(EcmPropertyUtils.getField(TestDocument.class, "testInt"));
assertNull(EcmPropertyUtils.getField(TestDocument.class, "nonExisting"));
assertNull(EcmPropertyUtils.getField(TestDocument.class, "table[0].objectName"));
assertNotNull(EcmPropertyUtils.getField(TestDocument.class, "creationDate"));
});
}
}

0 comments on commit 9c482c7

Please sign in to comment.