### Question:
ObjectRequestResolver { public ObjectRecord resolveObject(String object) throws InvalidArgumentException { if (StringUtils.isBlank(object)) return null; if (StringUtils.isUUID(object)) { return objectFactDao.getObject(UUID.fromString(object)); } Matcher matcher = TYPE_VALUE_PATTERN.matcher(object); if (!matcher.matches()) { return null; } String type = matcher.group(1); String value = matcher.group(2); ObjectTypeEntity typeEntity = fetchObjectType(type); ObjectRecord objectRecord = objectFactDao.getObject(type, value); if (objectRecord == null) { objectRecord = createObject(typeEntity, value); } return objectRecord; } @Inject  ObjectRequestResolver(ObjectManager objectManager,
                               ObjectFactDao objectFactDao,
                               ValidatorFactory validatorFactory); ObjectRecord resolveObject(String object);  }### Answer:
@Test public void testResolveObjectWithInvalidInput() throws Exception { assertNull(resolver.resolveObject(null)); assertNull(resolver.resolveObject("")); assertNull(resolver.resolveObject(" ")); assertNull(resolver.resolveObject("invalid")); }
@Test public void testResolveObjectById() throws Exception { UUID id = UUID.randomUUID(); ObjectRecord object = new ObjectRecord(); when(objectFactDao.getObject(id)).thenReturn(object); assertSame(object, resolver.resolveObject(id.toString())); verify(objectFactDao).getObject(id); }
@Test public void testResolveObjectByTypeValue() throws Exception { String type = "ObjectType"; String value = "ObjectValue"; ObjectRecord object = new ObjectRecord(); when(objectManager.getObjectType(type)).thenReturn(new ObjectTypeEntity()); when(objectFactDao.getObject(type, value)).thenReturn(object); assertSame(object, resolver.resolveObject(String.format("%s/%s", type, value))); verify(objectManager).getObjectType(type); verify(objectFactDao).getObject(type, value); }
@Test public void testCreateMissingObject() throws Exception { String value = "ObjectValue"; ObjectTypeEntity type = mockFetchObjectType(); mockValidator(true); when(objectFactDao.storeObject(any())).thenAnswer(i -> i.getArgument(0)); ObjectRecord resolvedObject = resolver.resolveObject(String.format("%s/%s", type.getName(), value)); assertObjectRecord(resolvedObject, type.getId(), value); verify(objectFactDao).storeObject(argThat(record -> assertObjectRecord(record, type.getId(), value))); }
@Test public void testCreateMissingObjectFailsOnMissingObjectType() { try { resolver.resolveObject("ObjectType/ObjectValue"); fail(); } catch (InvalidArgumentException ex) { assertEquals("object.type.not.exist", ex.getValidationErrors().iterator().next().getMessageTemplate()); verify(objectFactDao, never()).storeObject(any()); } }
@Test public void testCreateMissingObjectFailsOnObjectValidation() { ObjectTypeEntity type = mockFetchObjectType(); mockValidator(false); try { resolver.resolveObject(String.format("%s/%s", type.getName(), "ObjectValue")); fail(); } catch (InvalidArgumentException ex) { assertEquals("object.not.valid", ex.getValidationErrors().iterator().next().getMessageTemplate()); verify(objectFactDao, never()).storeObject(any()); } }