Skip to content

Commit

Permalink
Add try/catch block.
Browse files Browse the repository at this point in the history
  • Loading branch information
phleven committed Jan 17, 2025
1 parent 724514f commit ea13dfa
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,13 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
}
}

chain.doFilter(httpRequest, response);
try {
chain.doFilter(httpRequest, response);
} catch (IOException | ServletException e) {
log.error("Error: " + e.getMessage());
e.printStackTrace();
return;
}
}

private static boolean hasHomoGlyphs(WrappedHttp httpRequest) {
Expand Down
150 changes: 76 additions & 74 deletions src/main/java/com/deloitte/elrr/datasync/jpa/service/CommonSvc.java
Original file line number Diff line number Diff line change
@@ -1,90 +1,92 @@
package com.deloitte.elrr.datasync.jpa.service;

import com.deloitte.elrr.datasync.exception.ResourceNotFoundException;

import java.io.Serializable;
import java.util.Optional;

import org.springframework.data.repository.CrudRepository;

import com.deloitte.elrr.datasync.exception.ResourceNotFoundException;

/**
* @author mnelakurti
* @param <T>
* @param <newId>
*/
public interface CommonSvc<T, newId extends Serializable> {

/**
*
* @return Iterable<T>
*/
default Iterable<T> findAll() {
return getRepository().findAll();
}
/**
*
* @param id
* @return Optional<T>
*/
default Optional<T> get(newId id) {
return getRepository().findById(id);
}
/**
*
* @param entity
* @return T
*/
default T save(T entity) {
return getRepository().save(entity);
}
/**
*
* @param entities
* @return Iterable<T>
*/
default Iterable<T> saveAll(Iterable<T> entities) {
return getRepository().saveAll(entities);
}
/**
*
* @param id
* @throws ResourceNotFoundException
*/
default void delete(newId id) throws ResourceNotFoundException {
if (getRepository().existsById(id)) {
getRepository().deleteById(id);
} else {
throw new ResourceNotFoundException(
" Id not found for delete : " + id);
}
}
/**
*
*/
default void deleteAll() {
getRepository().deleteAll();
/**
* @return Iterable<T>
*/
default Iterable<T> findAll() {
return getRepository().findAll();
}

/**
* @param id
* @return Optional<T>
*/
default Optional<T> get(newId id) {
return getRepository().findById(id);
}

/**
* @param entity
* @return T
*/
default T save(T entity) {
return getRepository().save(entity);
}

/**
* @param entities
* @return Iterable<T>
*/
default Iterable<T> saveAll(Iterable<T> entities) {
return getRepository().saveAll(entities);
}

/**
* @param id
* @throws ResourceNotFoundException
*/
default void delete(newId id) throws ResourceNotFoundException {
try {
if (getRepository().existsById(id)) {
getRepository().deleteById(id);
} else {
throw new ResourceNotFoundException(" Id not found for delete : " + id);
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
return;
}
/**
*
* @param entity
* @throws ResourceNotFoundException
*/
default void update(T entity) throws ResourceNotFoundException {
if (getRepository().existsById(getId(entity))) {
getRepository().save(entity);
} else {
throw new ResourceNotFoundException(
"Not found record in DB to update: " + entity);
}
}

/** */
default void deleteAll() {
getRepository().deleteAll();
}

/**
* @param entity
* @throws ResourceNotFoundException
*/
default void update(T entity) throws ResourceNotFoundException {
if (getRepository().existsById(getId(entity))) {
getRepository().save(entity);
} else {
throw new ResourceNotFoundException("Not found record in DB to update: " + entity);
}
/**
*
* @param entity
* @return NewId
*/
newId getId(T entity);
/**
*
* @return CrudRepository<T, NewId>
*/
CrudRepository<T, newId> getRepository();
}

/**
* @param entity
* @return NewId
*/
newId getId(T entity);

/**
* @return CrudRepository<T, NewId>
*/
CrudRepository<T, newId> getRepository();
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private int insertSyncRecord(final Statement statement, final ImportDetail impor
createSyncRecordDetail(sync, statement);
successCount++;

} catch (Exception e) {
} catch (JsonProcessingException e) {
log.error("Exception in processing " + e.getMessage());
e.getStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ public class NewDataService {
@Autowired private ErrorsService errorsService;

private ObjectMapper mapper = new ObjectMapper();

/**
* 1. Retrieve all unprocessed syncrecord records with INSERTED status
* 2. Send the message to Kafka
* 3. Update the records SyncRecord and SyncRecordDetails to SUCCCESS/INSERTED status
* 1. Retrieve all unprocessed syncrecord records with INSERTED status 2. Send the message to
* Kafka 3. Update the records SyncRecord and SyncRecordDetails to SUCCCESS/INSERTED status
*/
public void process(Statement[] statements) {

Expand Down Expand Up @@ -76,7 +75,7 @@ public void process(Statement[] statements) {
syncRecordDetail.setRecordStatus("SUCCESS");
syncRecordDetailService.save(syncRecordDetail);

} catch (Exception e) {
} catch (JsonProcessingException e) {

log.error("Exception in processing " + e.getMessage());
e.printStackTrace();
Expand Down Expand Up @@ -124,7 +123,8 @@ private void sendToKafka(final MessageVO message) {
* @return
* @throws JsonProcessingException
*/
private MessageVO createKafkaJsonMessage(final Statement statement, final SyncRecordDetail syncRecordDetail)
private MessageVO createKafkaJsonMessage(
final Statement statement, final SyncRecordDetail syncRecordDetail)
throws JsonProcessingException {
AuditRecord auditRecord = new AuditRecord();
MessageVO vo = new MessageVO();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,43 +36,33 @@
class ImportsControllerTest {

/** */
@MockBean
private ImportsCreatorSvc svc;
@MockBean private ImportsCreatorSvc svc;

/** */
@MockBean
private ImportDetailRepository mockImportDetailRepository;
@MockBean private ImportDetailRepository mockImportDetailRepository;

/** */
@MockBean
private ImportRepository mockImportRepository;
@MockBean private ImportRepository mockImportRepository;

/** */
@MockBean
private SyncRecordDetailRepository mockSyncRecordDetailRepository;
@MockBean private SyncRecordDetailRepository mockSyncRecordDetailRepository;

/** */
@MockBean
private SyncRecordRepository mockSyncRecordRepository;
@MockBean private SyncRecordRepository mockSyncRecordRepository;

/** */
@MockBean
private RestTemplateBuilder mockRestTemplateBuilder;
@MockBean private RestTemplateBuilder mockRestTemplateBuilder;

/** */
@MockBean
private RestTemplate mockRestTemplate;
@MockBean private RestTemplate mockRestTemplate;

/** */
@MockBean
private ModelMapper mapper;
@MockBean private ModelMapper mapper;

/** */
@Autowired
private MockMvc mockMvc;
@Autowired private MockMvc mockMvc;

@MockBean
private ErrorsRepository mockErrorsRepository;
@MockBean private ErrorsRepository mockErrorsRepository;

/**
* @throws Exception
Expand All @@ -85,7 +75,6 @@ void getImportsAllTest() throws Exception {
mockMvc.perform(get("/api/getImports")).andExpect(status().isBadRequest());
}

/** */
@Test
void getImportsTest() throws Exception {
ImportDTO importDTO = new ImportDTO();
Expand Down

0 comments on commit ea13dfa

Please sign in to comment.