1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
package com.calculateur.warhammer.rest.controller;
import static org.junit.jupiter.api.Assertions.fail;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import com.calculateur.warhammer.base.dto.ILibelleDTO;
import com.calculateur.warhammer.base.dto.ILibelleEtDescriptionDTO;
import com.calculateur.warhammer.base.service.IServiceRechercheExistant;
import com.calculateur.warhammer.dto.contrat.IDTOparEnumeration;
public abstract class AbstractListRestControllerTest<DTO extends ILibelleDTO,S extends IServiceRechercheExistant<DTO>> {
protected static final String[] LOCALES = {"fr","en"};
protected static final String LOCALE_NON_IMPLEMENTEE = "zz";
protected static final int PRECONDITION_FAILED = 412;
private static final String URL_ALL = "/all/";
@Autowired
protected MockMvc mvc;
/**
*
* @return L'url du contrôleur
*/
protected abstract String getUrlController();
/**
*
* @return Le service de test
*/
protected abstract S getService();
@Test
public void doTestGetAllOK() {
Arrays.asList(LOCALES).stream().forEach(l -> doTestGetAllOK(l));
}
private void doTestGetAllOK(String langue) {
try {
List<DTO> list = getService().liste(langue);
ResultActions action = mvc.perform(MockMvcRequestBuilders
.get(getUrlAll(langue))
.accept(MediaType.APPLICATION_JSON_VALUE))
.andExpect(MockMvcResultMatchers.jsonPath("$.length()").value(list.size()))
.andExpect(MockMvcResultMatchers.status().isOk());
for(int i = 0; i < list.size();i++) {
valideDTO(action, i, list.get(i));
}
action.andReturn();
} catch (Exception e) {
fail(e);
}
}
@Test
public void testAllLangueNonImplementee(){
try {
mvc.perform(MockMvcRequestBuilders
.get(getUrlAll(LOCALE_NON_IMPLEMENTEE))
.accept(MediaType.APPLICATION_JSON_VALUE))
.andExpect(MockMvcResultMatchers.status().is(PRECONDITION_FAILED)).andReturn();
} catch (Exception e) {
fail(e);
}
}
/**
*
* @param position Position
* @param champ Champ testé
* @return Le JSON pour test à la position i
*/
protected String getTestJsonForPosition(int position,String champ) {
StringBuilder sb = new StringBuilder();
sb.append("$.[");
sb.append(position);
sb.append("].");
sb.append(champ);
return sb.toString();
}
private String getUrlAll(String locale) {
StringBuilder url = new StringBuilder();
url.append(getUrlController());
url.append(URL_ALL);
url.append(locale);
return url.toString();
}
protected void valideDTO(ResultActions action, int i, DTO dto) throws Exception {
action.andExpect(MockMvcResultMatchers.jsonPath(getTestJsonForPosition(i, "id")).value(dto.getId()));
action.andExpect(MockMvcResultMatchers.jsonPath(getTestJsonForPosition(i, "libelle")).value(dto.getLibelle()));
action.andExpect(MockMvcResultMatchers.jsonPath(getTestJsonForPosition(i, "cleTraduction")).value(dto.getCleTraduction()));
if(dto instanceof ILibelleEtDescriptionDTO des) {
action.andExpect(MockMvcResultMatchers.jsonPath(getTestJsonForPosition(i, "description")).value(des.getDescription()));
}
if(dto instanceof IDTOparEnumeration<?> en) {
action.andExpect(MockMvcResultMatchers.jsonPath(getTestJsonForPosition(i, "enumeration")).value(en.getEnumeration().toString()));
}
}
} |
Partager