|
| 1 | +package org.folio.linked.data.e2e.mappings.work.lightwork; |
| 2 | + |
| 3 | +import static org.folio.ld.dictionary.PredicateDictionary.IS_PART_OF; |
| 4 | +import static org.folio.ld.dictionary.PredicateDictionary.OTHER_EDITION; |
| 5 | +import static org.folio.ld.dictionary.PredicateDictionary.OTHER_VERSION; |
| 6 | +import static org.folio.ld.dictionary.PredicateDictionary.RELATED_WORK; |
| 7 | +import static org.folio.ld.dictionary.PropertyDictionary.LABEL; |
| 8 | +import static org.folio.ld.dictionary.ResourceTypeDictionary.LIGHT_RESOURCE; |
| 9 | +import static org.folio.ld.dictionary.ResourceTypeDictionary.SERIES; |
| 10 | +import static org.folio.ld.dictionary.ResourceTypeDictionary.WORK; |
| 11 | +import static org.folio.linked.data.test.TestUtil.STANDALONE_TEST_PROFILE; |
| 12 | +import static org.folio.linked.data.test.TestUtil.TEST_JSON_MAPPER; |
| 13 | +import static org.folio.linked.data.test.TestUtil.defaultHeaders; |
| 14 | +import static org.folio.linked.data.util.Constants.STANDALONE_PROFILE; |
| 15 | +import static org.hamcrest.Matchers.containsInAnyOrder; |
| 16 | +import static org.hamcrest.Matchers.hasSize; |
| 17 | +import static org.springframework.http.MediaType.APPLICATION_JSON; |
| 18 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 19 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 20 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 21 | + |
| 22 | +import lombok.SneakyThrows; |
| 23 | +import org.folio.ld.dictionary.PredicateDictionary; |
| 24 | +import org.folio.linked.data.e2e.base.ITBase; |
| 25 | +import org.folio.linked.data.e2e.base.IntegrationTest; |
| 26 | +import org.folio.linked.data.model.entity.Resource; |
| 27 | +import org.folio.linked.data.model.entity.ResourceEdge; |
| 28 | +import org.folio.linked.data.test.MonographTestUtil; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.springframework.test.context.ActiveProfiles; |
| 31 | + |
| 32 | +@IntegrationTest |
| 33 | +@ActiveProfiles({STANDALONE_PROFILE, STANDALONE_TEST_PROFILE}) |
| 34 | +class LightWorkIT extends ITBase { |
| 35 | + |
| 36 | + private static final String RESOURCE_URL = "/linked-data/resource"; |
| 37 | + private static final Long IS_PART_OF_ID = 200L; |
| 38 | + private static final Long OTHER_EDITION_ID = 201L; |
| 39 | + private static final Long OTHER_VERSION_ID = 202L; |
| 40 | + private static final Long RELATED_WORK_ID = 203L; |
| 41 | + |
| 42 | + @Test |
| 43 | + @SneakyThrows |
| 44 | + void getWork_withLightWorkEdges_shouldReturnAnalyticalEntryForEachRelation() { |
| 45 | + // given |
| 46 | + var work = buildWorkWithLightWorkEdges(); |
| 47 | + resourceTestService.saveGraph(work); |
| 48 | + var getRequest = get(RESOURCE_URL + "/" + work.getId()) |
| 49 | + .contentType(APPLICATION_JSON) |
| 50 | + .headers(defaultHeaders(env)); |
| 51 | + |
| 52 | + // when |
| 53 | + var response = mockMvc.perform(getRequest); |
| 54 | + |
| 55 | + // then |
| 56 | + var analyticalEntryPath = "$.resource['http://bibfra.me/vocab/lite/Work']['_analyticalEntry ']"; |
| 57 | + response |
| 58 | + .andExpect(status().isOk()) |
| 59 | + .andExpect(jsonPath(analyticalEntryPath, hasSize(4))) |
| 60 | + .andExpect(jsonPath(analyticalEntryPath + "[*]['id']", containsInAnyOrder( |
| 61 | + IS_PART_OF_ID.toString(), |
| 62 | + OTHER_EDITION_ID.toString(), |
| 63 | + OTHER_VERSION_ID.toString(), |
| 64 | + RELATED_WORK_ID.toString() |
| 65 | + ))) |
| 66 | + .andExpect(jsonPath(analyticalEntryPath + "[*]['_relation']", containsInAnyOrder( |
| 67 | + IS_PART_OF.getUri(), |
| 68 | + OTHER_EDITION.getUri(), |
| 69 | + OTHER_VERSION.getUri(), |
| 70 | + RELATED_WORK.getUri() |
| 71 | + ))); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + @SneakyThrows |
| 76 | + void getWork_withLightWorkEdgesAndPartOfSeries_shouldReturnCorrectAnalyticalEntries() { |
| 77 | + // given |
| 78 | + var work = buildWorkWithLightWorkEdges(); |
| 79 | + var series = new Resource().setLabel("Series").addTypes(WORK, SERIES, LIGHT_RESOURCE).setIdAndRefreshEdges(333L); |
| 80 | + work.addOutgoingEdge(new ResourceEdge(work, series, IS_PART_OF)); |
| 81 | + resourceTestService.saveGraph(work); |
| 82 | + var getRequest = get(RESOURCE_URL + "/" + work.getId()) |
| 83 | + .contentType(APPLICATION_JSON) |
| 84 | + .headers(defaultHeaders(env)); |
| 85 | + |
| 86 | + // when |
| 87 | + var response = mockMvc.perform(getRequest); |
| 88 | + |
| 89 | + // then |
| 90 | + var analyticalEntryPath = "$.resource['http://bibfra.me/vocab/lite/Work']['_analyticalEntry ']"; |
| 91 | + response |
| 92 | + .andExpect(status().isOk()) |
| 93 | + .andExpect(jsonPath(analyticalEntryPath, hasSize(4))) |
| 94 | + .andExpect(jsonPath(analyticalEntryPath + "[*]['id']", containsInAnyOrder( |
| 95 | + IS_PART_OF_ID.toString(), |
| 96 | + OTHER_EDITION_ID.toString(), |
| 97 | + OTHER_VERSION_ID.toString(), |
| 98 | + RELATED_WORK_ID.toString() |
| 99 | + ))) |
| 100 | + .andExpect(jsonPath(analyticalEntryPath + "[*]['_relation']", containsInAnyOrder( |
| 101 | + IS_PART_OF.getUri(), |
| 102 | + OTHER_EDITION.getUri(), |
| 103 | + OTHER_VERSION.getUri(), |
| 104 | + RELATED_WORK.getUri() |
| 105 | + ))); |
| 106 | + } |
| 107 | + |
| 108 | + @SneakyThrows |
| 109 | + private Resource buildWorkWithLightWorkEdges() { |
| 110 | + var work = MonographTestUtil.getWork("work", hashService); |
| 111 | + addLightWorkEdge(work, IS_PART_OF_ID, IS_PART_OF); |
| 112 | + addLightWorkEdge(work, OTHER_EDITION_ID, OTHER_EDITION); |
| 113 | + addLightWorkEdge(work, OTHER_VERSION_ID, OTHER_VERSION); |
| 114 | + addLightWorkEdge(work, RELATED_WORK_ID, RELATED_WORK); |
| 115 | + return work; |
| 116 | + } |
| 117 | + |
| 118 | + @SneakyThrows |
| 119 | + private void addLightWorkEdge(Resource work, Long lightWorkId, PredicateDictionary predicate) { |
| 120 | + var doc = TEST_JSON_MAPPER.readTree(""" |
| 121 | + {"%s": ["%s"]}""".formatted(LABEL.getValue(), labelFor(lightWorkId))); |
| 122 | + var lightWork = new Resource() |
| 123 | + .addTypes(LIGHT_RESOURCE, WORK) |
| 124 | + .setDoc(doc) |
| 125 | + .setLabel(labelFor(lightWorkId)) |
| 126 | + .setIdAndRefreshEdges(lightWorkId); |
| 127 | + work.addOutgoingEdge(new ResourceEdge(work, lightWork, predicate)); |
| 128 | + } |
| 129 | + |
| 130 | + private String labelFor(Long id) { |
| 131 | + return "light work label " + id; |
| 132 | + } |
| 133 | +} |
| 134 | + |
0 commit comments