ExternalInterfaceServiceImpl implements ExternalInterfaceService { @Override public ExternalInterface createExternalInterface(ExternalInterfaceCreateRequest request) { validateExternalInterfaceCreateRequest(request); ExternalInterfaceEntity externalInterfaceEntity = externalInterfaceDao.getExternalInterfaceByName(request.getExternalInterfaceKey().getExternalInterfaceName()); if (externalInterfaceEntity != null) { throw new AlreadyExistsException(String .format("Unable to create external interface \"%s\" because it already exists.", request.getExternalInterfaceKey().getExternalInterfaceName())); } externalInterfaceEntity = createExternalInterfaceEntity(request); externalInterfaceEntity = externalInterfaceDao.saveAndRefresh(externalInterfaceEntity); return createExternalInterfaceFromEntity(externalInterfaceEntity); }  @Override ExternalInterface createExternalInterface(ExternalInterfaceCreateRequest request); @Override ExternalInterface getExternalInterface(ExternalInterfaceKey externalInterfaceKey); @Override ExternalInterface deleteExternalInterface(ExternalInterfaceKey externalInterfaceKey); @Override ExternalInterfaceKeys getExternalInterfaces(); @Override ExternalInterface updateExternalInterface(ExternalInterfaceKey externalInterfaceKey, ExternalInterfaceUpdateRequest request);  }
@Test public void testCreateExternalInterface() { when(externalInterfaceDao.getExternalInterfaceByName(EXTERNAL_INTERFACE)).thenReturn(null); when(alternateKeyHelper.validateStringParameter(anyString(), anyString(), anyString())).thenReturn(EXTERNAL_INTERFACE); when(alternateKeyHelper.validateStringParameter(anyString(), anyString())).thenReturn(DISPLAY_NAME_FIELD); when(externalInterfaceDao.saveAndRefresh(any(ExternalInterfaceEntity.class))).thenReturn(EXTERNAL_INTERFACE_ENTITY); ExternalInterfaceKey externalInterfaceKey = new ExternalInterfaceKey(EXTERNAL_INTERFACE); ExternalInterfaceCreateRequest externalInterfaceCreateRequest = new ExternalInterfaceCreateRequest(externalInterfaceKey, DISPLAY_NAME_FIELD, DESCRIPTION); ExternalInterface externalInterface = externalInterfaceService.createExternalInterface(externalInterfaceCreateRequest); assertEquals(EXTERNAL_INTERFACE, externalInterface.getExternalInterfaceKey().getExternalInterfaceName()); verify(alternateKeyHelper).validateStringParameter("An", "external interface name", EXTERNAL_INTERFACE); verify(alternateKeyHelper).validateStringParameter("display name", DISPLAY_NAME_FIELD); verify(externalInterfaceDao).getExternalInterfaceByName(EXTERNAL_INTERFACE); verify(externalInterfaceDao).saveAndRefresh(any(ExternalInterfaceEntity.class)); verifyNoMoreInteractionsHelper(); }
@Test public void testCreateExternalInterfaceAlreadyExists() { expectedException.expect(AlreadyExistsException.class); expectedException.expectMessage(String.format("Unable to create external interface \"%s\" because it already exists.", EXTERNAL_INTERFACE)); when(externalInterfaceDao.getExternalInterfaceByName(EXTERNAL_INTERFACE)).thenReturn(EXTERNAL_INTERFACE_ENTITY); when(alternateKeyHelper.validateStringParameter(anyString(), anyString(), anyString())).thenReturn(EXTERNAL_INTERFACE); externalInterfaceService.createExternalInterface(EXTERNAL_INTERFACE_CREATE_REQUEST); }