Skip to content

Commit

Permalink
Merge pull request #67 from companieshouse/IDVA6-1514-refactor-tests
Browse files Browse the repository at this point in the history
Refactored tests
  • Loading branch information
krishna-patel-ch authored Sep 4, 2024
2 parents 4bc0fd9 + cd5f434 commit 8276dce
Show file tree
Hide file tree
Showing 6 changed files with 902 additions and 1,301 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package uk.gov.companieshouse.acsp.manage.users.common;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.Map;
import java.util.Objects;
import org.springframework.test.web.servlet.ResultActions;

public class ParsingUtils {

public static String reduceTimestampResolution( final String timestamp ) {
return timestamp.substring( 0, timestamp.indexOf( ":" ) );
}

public static String localDateTimeToNormalisedString( final LocalDateTime localDateTime ) {
final var timestamp = localDateTime.toString();
return reduceTimestampResolution( timestamp );
}

public static OffsetDateTime localDateTimeToOffsetDateTime( final LocalDateTime localDateTime ) {
return Objects.isNull( localDateTime ) ? null : OffsetDateTime.of( localDateTime, ZoneOffset.UTC );
}

public static <T> T parseResponseTo( final ResultActions response, Class<T> responseType ) throws IOException {
final var responseContent = response.andReturn().getResponse().getContentAsByteArray();
final var objectMapper = new ObjectMapper();
objectMapper.registerModule( new JavaTimeModule() );
return objectMapper.readValue( responseContent, responseType );
}

public static <T> Map<String, Object> toMap( final T object ) {
final var objectMapper = new ObjectMapper();
objectMapper.registerModule( new JavaTimeModule() );
objectMapper.findAndRegisterModules();
objectMapper.configure( SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false );
return objectMapper.convertValue( object, new TypeReference<>() {} );
}

public static <T> T fromMap( final Map<String, Object> map, Class<T> clazz ) {
final var objectMapper = new ObjectMapper();
objectMapper.registerModule( new JavaTimeModule() );
return objectMapper.convertValue( map, clazz );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ class AcspMembershipControllerTest {
private MockMvc mockMvc;

@MockBean
ApiClientService apiClientService;
private ApiClientService apiClientService;

@MockBean
InternalApiClient internalApiClient;
private InternalApiClient internalApiClient;

@MockBean
private UsersService usersService;
Expand All @@ -49,7 +49,7 @@ class AcspMembershipControllerTest {
@MockBean
private AcspMembersService acspMembersService;

private final TestDataManager testDataManager = TestDataManager.getInstance();
private static final TestDataManager testDataManager = TestDataManager.getInstance();

@Test
void getAcspMembershipForAcspAndIdWithoutXRequestIdReturnsBadRequest() throws Exception {
Expand Down
Loading

0 comments on commit 8276dce

Please sign in to comment.