### Question:
ApplicationsService implements ApplicationsApi { @Override public Application createApplication(final CreateApplicationInput createApplicationInput) { log.info("Creating application with input {}", createApplicationInput); ApplicationRecord applicationRecord = modelMapper.map(createApplicationInput, ApplicationRecord.class); applicationRecord.setCreatedAt(Instant.now(clock)); applicationRecord.setVersion(1L); applicationRecord.setUserId(securityContext.getUserPrincipal().getName()); try { dynamodb.putItem(PutItemRequest.builder() .tableName(tableName) .item(applicationRecord.toAttributeMap()) .conditionExpression( String.format("attribute_not_exists(%s) AND attribute_not_exists(%s)", ApplicationRecord.USER_ID_ATTRIBUTE_NAME, ApplicationRecord.APPLICATION_ID_ATTRIBUTE_NAME)) .build()); } catch (ConditionalCheckFailedException e) { throw new ConflictApiException(new ConflictException() .errorCode("ApplicationAlreadyExist") .message(String.format("Application %s already exists.", createApplicationInput.getApplicationId()))); } return modelMapper.map(applicationRecord, Application.class); } @Inject  ApplicationsService(
        final TokenSerializer<Map<String, AttributeValue>> paginationTokenSerializer,
        final DynamoDbClient dynamodb, final ConfigProvider configProvider);  ApplicationsService(
        final TokenSerializer<Map<String, AttributeValue>> paginationTokenSerializer,
        final DynamoDbClient dynamodb, final ConfigProvider configProvider,
        final Clock clock); @Override Application createApplication(final CreateApplicationInput createApplicationInput); @Override void deleteApplication(final String applicationId); @Override Application getApplication(String applicationId); @Override ApplicationList listApplications(final String nextToken, final Integer maxItems); @Override Application updateApplication(final UpdateApplicationInput updateApplicationInput,
                                       final String applicationId);  }### Answer:
@Test public void createApplication_alreadyExist() { String userId = UUID.randomUUID().toString(); String applicationId = UUID.randomUUID().toString(); CreateApplicationInput input = new CreateApplicationInput() .applicationId(applicationId); when(principal.getName()).thenReturn(userId); doThrow(ConditionalCheckFailedException.class).when(dynamodb).putItem(any(PutItemRequest.class)); assertThatThrownBy(() -> service.createApplication(input)) .isInstanceOf(ConflictApiException.class); }
@Test public void createApplication() { String userId = UUID.randomUUID().toString(); String applicationId = UUID.randomUUID().toString(); Instant createdAt = Instant.now(); CreateApplicationInput input = new CreateApplicationInput() .applicationId(applicationId); Map<String, AttributeValue> recordMap = keyMap(userId, applicationId); recordMap.put("version", AttributeValue.builder().n("1").build()); recordMap.put("createdAt", AttributeValue.builder().s(createdAt.toString()).build()); when(principal.getName()).thenReturn(userId); when(clock.instant()).thenReturn(createdAt); PutItemRequest expectedPutItemRequest = PutItemRequest.builder() .tableName(TABLE_NAME) .item(recordMap) .conditionExpression("attribute_not_exists(userId) AND attribute_not_exists(applicationId)") .build(); Application application = service.createApplication(input); ArgumentCaptor<PutItemRequest> putItemRequestArgumentCaptor = ArgumentCaptor.forClass(PutItemRequest.class); verify(dynamodb).putItem(putItemRequestArgumentCaptor.capture()); assertThat(application.getApplicationId()).isEqualTo(applicationId); assertThat(putItemRequestArgumentCaptor.getValue()).isEqualTo(expectedPutItemRequest); }