FileTypeServiceImpl implements FileTypeService { @Override public FileType createFileType(FileTypeCreateRequest fileTypeCreateRequest) { validateFileTypeCreateRequest(fileTypeCreateRequest); FileTypeEntity fileTypeEntity = fileTypeDao.getFileTypeByCode(fileTypeCreateRequest.getFileTypeCode()); if (fileTypeEntity != null) { throw new AlreadyExistsException( String.format("Unable to create file type \"%s\" because it already exists.", fileTypeCreateRequest.getFileTypeCode())); } fileTypeEntity = createFileTypeEntity(fileTypeCreateRequest); fileTypeEntity = fileTypeDao.saveAndRefresh(fileTypeEntity); return createFileTypeFromEntity(fileTypeEntity); }  @Override FileType createFileType(FileTypeCreateRequest fileTypeCreateRequest); @Override FileType getFileType(FileTypeKey fileTypeKey); @Override FileType deleteFileType(FileTypeKey fileTypeKey); @Override FileTypeKeys getFileTypes();  }
@Test public void testCreateFileType() { when(fileTypeMockDao.getFileTypeByCode(FORMAT_FILE_TYPE_CODE)).thenReturn(null); when(alternateKeyHelper.validateStringParameter(anyString(), anyString())).thenReturn(FORMAT_FILE_TYPE_CODE); when(fileTypeMockDao.saveAndRefresh(any(FileTypeEntity.class))).thenReturn(FILE_TYPE_ENTITY); FileType fileType = fileTypeMockService.createFileType(FILE_TYPE_CREATE_REQUEST); assertEquals(FORMAT_FILE_TYPE_CODE, fileType.getFileTypeCode()); verify(alternateKeyHelper).validateStringParameter("file type code", FORMAT_FILE_TYPE_CODE); verify(fileTypeMockDao).getFileTypeByCode(FORMAT_FILE_TYPE_CODE); verify(fileTypeMockDao).saveAndRefresh(any(FileTypeEntity.class)); verifyNoMoreInteractionsHelper(); }
@Test public void testCreateFileTypeAlreadyExists() { expectedException.expect(AlreadyExistsException.class); expectedException.expectMessage(String.format("Unable to create file type \"%s\" because it already exists.", FORMAT_FILE_TYPE_CODE)); when(fileTypeMockDao.getFileTypeByCode(FORMAT_FILE_TYPE_CODE)).thenReturn(FILE_TYPE_ENTITY); when(alternateKeyHelper.validateStringParameter(anyString(), anyString())).thenReturn(FORMAT_FILE_TYPE_CODE); fileTypeMockService.createFileType(FILE_TYPE_CREATE_REQUEST); }