Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CARDS-2154: cards-utils tests #1418

Draft
wants to merge 17 commits into
base: dev
Choose a base branch
from
Draft

CARDS-2154: cards-utils tests #1418

wants to merge 17 commits into from

Conversation

sofi2002sofi
Copy link
Contributor

No description provided.

* @version $Id $
*/
@RunWith(MockitoJUnitRunner.class)
public class ResourceToCSVAdapterFactoryTest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More tests:

  • getAdapterWithNoProcessorsReturnsResourcePath - has an empty list of processors
  • getAdapterUsesFirstProcessorThatCanProcess - has 3 processors, first cannot process, check second's output is used, verify first's and third's serialize is never invoked

}

@Test
public void getAdapterForResourceAdaptableObjectReturnsResourcePath()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public void getAdapterForResourceAdaptableObjectReturnsResourcePath()
public void getAdapterForUnsupportedResourceReturnsResourcePath()

Comment on lines 84 to 86
when(processor.isEnabledByDefault(adaptable)).thenReturn(true);
when(processor.getName()).thenReturn("name");
when(processor.canProcess(adaptable)).thenReturn(true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines are the same in all 3 tests, they should be moved in a mockWorkingProcessor method.

Comment on lines 97 to 98
ResourceMetadata resourceMetadata = mock(ResourceMetadata.class);
when(adaptable.getResourceMetadata()).thenReturn(resourceMetadata);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code seems useless to me; the fact that it is actually needed here suggests that the actual code can be improved. Try moving the adaptTo(Node.class) line earlier in the method, and add a check for null.

Normally, in test-driven development, the tests are supposed to influence the code, not the other way around, so you should feel free to also change the code being tested when you feel that it can be improved, not just write tests that perfectly match the existing code.

* @version $Id $
*/
@RunWith(MockitoJUnitRunner.class)
public class ResourceToJsonAdapterFactoryTest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More tests:

  • getAdapterUsesAllSupportedAndEnabledProcessors - receives a list of processors that either support or not the resource, and that are either enabled or disabled by default
  • getAdapterSortsProcessors - receives a list of processors in the wrong order; to make the order validation easier to check, write a simple class that receives a priority and 2 integers a and b in its constructor, and implements ResourceJsonProcessor.processProperty as return Json.createValue(input == null ? b : ((JsonNumber) input).intValue() * a + b);; instantiate 4 such processors with different prime values for a and b, and serialize a number; make sure the output matches the expected result
  • getAdapterWithNoProcessorsReturnsEmptyJsonObject - uses an empty list of processors
  • getAdapterWithNoSupportedProcessorsReturnsEmptyJsonObject - uses one processor that cannot process the resource
  • getAdapterWithRecursiveReferencesUsesResourcePathForNestedReferences
  • getAdapterUsesResourceSelectorsToDisableDefaultProcessors
  • getAdapterUsesResourceSelectorsToEnableProcessors
  • getAdapterWithBothEnableAndDisableSelectorsPrioritizesEnable

}

@Test
public void isEnabledByDefaultTest()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I think each processor needs this test, even if the method is not overridden in a class, just to check that it is indeed false.

Property property = node.getProperty("jcr:baseVersion");
JsonValue json = Json.createValue(FORM_TYPE);
JsonValue jsonValue = this.dereferenceProcessor.processProperty(node, property, json, this::serializeNode);
assertNotNull(jsonValue);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs an assertEquals check as well.

        assertEquals(property.getNode().getPath(), ((JsonString) jsonValue).getString());

assertTrue(jsonObject.containsKey("@path"));
assertEquals(Json.createValue(TEST_FORM_PATH), jsonObject.getJsonString("@path"));
assertTrue(jsonObject.containsKey("@name"));
assertEquals(Json.createValue("f1"), jsonObject.getJsonString("@name"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not the simpler:

        assertEquals("f1", jsonObject.getString("@name"));

}

@Test
public void leaveAddsPathAndNameAndReferencedParameters() throws RepositoryException
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would split this into 2 methods:

  • leaveAddsPathAndNameParameters
  • leaveAddsReferencedParameter

Also, I would test the reference for 2 different nodes, the form (for false) and the questionnaire (for true).

* @version $Id $
*/
@RunWith(MockitoJUnitRunner.class)
public class SimpleProcessorTest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing a positive test: processPropertyReturnsInput.

@veronikaslc
Copy link
Contributor

ResourceToJsonAdapterFactoryTest: Lines 267-208 not covered from ResourceToJsonAdapterFactory
BareProcessorTest not covered cases of lines 141 and 206-209 of BareProcessor
DereferenceProcessorTest: not covered line 127 of DereferenceProcessor
SimpleProcessorTest: not covered line 101 of SimpleProcessor

@veronikaslc
Copy link
Contributor

jacoco.zip
jacoco:report html file attached

tests for DenyScriptsSlingPostProcessor
tests for ContentTypeSetter and StatusCodeSetter
tests for ResourceToCSVAdapterFactory
tests for ResourceToJsonAdapterFactory, ResourceToMarkdownAdspterFactory, ResourceToTextAdapterFactory
tests for DeepProcessor, DereferenceProcessor, IdentificationProcessor, PropertiesProcessor, SimpleProcessor
fixes for ResourceToCSVAdapterFactory
fixes for ResourceToMarkdownAdapterFactory
fixes for ResourceToTextAdapterFactory
fixes for DenyScriptsSlingPostProcessorTest
fixes for DeepProcessorTest, DeferenceProcessorTest, PropertiesProcessorTest, PropertiesProcessorTest
fixes for IdentificationProcessorTest
fixes for SimpleProcessorTest
fixes for ResourceToJsonAdapterFactoryTest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants