Skip to content

Commit

Permalink
Fix MetadataApiTest and add test to check metadata not available in s…
Browse files Browse the repository at this point in the history
…ubportal
  • Loading branch information
josegar74 authored and david-blasby committed Jan 27, 2025
1 parent 803d51d commit d6d6e4a
Showing 1 changed file with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2001-2016 Food and Agriculture Organization of the
* Copyright (C) 2001-2024 Food and Agriculture Organization of the
* United Nations (FAO-UN), United Nations World Food Programme (WFP)
* and United Nations Environment Programme (UNEP)
*
Expand Down Expand Up @@ -27,8 +27,13 @@
import org.fao.geonet.NodeInfo;
import org.fao.geonet.api.ApiParams;
import org.fao.geonet.domain.AbstractMetadata;
import org.fao.geonet.domain.Source;
import org.fao.geonet.domain.SourceType;
import org.fao.geonet.kernel.SpringLocalServiceInvoker;
import org.fao.geonet.kernel.datamanager.IMetadataIndexer;
import org.fao.geonet.kernel.search.IndexingMode;
import org.fao.geonet.repository.MetadataRepository;
import org.fao.geonet.repository.SourceRepository;
import org.fao.geonet.services.AbstractServiceIntegrationTest;
import org.jdom.Element;
import org.junit.Assert;
Expand Down Expand Up @@ -67,6 +72,12 @@ public class MetadataApiTest extends AbstractServiceIntegrationTest {
private EntityManager _entityManager;
@Autowired
private MetadataRepository metadataRepository;
@Autowired
private IMetadataIndexer metadataIndexer;
@Autowired
private SourceRepository sourceRepository;
@Autowired
private NodeInfo nodeInfo;

private int id;
private String uuid;
Expand All @@ -83,6 +94,15 @@ private void createTestData() throws Exception {
AbstractMetadata metadata = injectMetadataInDb(getSampleMetadataXml(), context, true);
id = metadata.getId();
uuid = metadata.getUuid();

metadataIndexer.indexMetadata(String.valueOf(id), true, IndexingMode.full);

Source subportal = new Source();
subportal.setName("external");
subportal.setType(SourceType.subportal);
subportal.setFilter("-uuid:" + uuid);
subportal.setUuid(UUID.randomUUID().toString());
sourceRepository.save(subportal);
}

@Test
Expand Down Expand Up @@ -330,6 +350,35 @@ public void getNonAllowedRecordAsXml() throws Exception {
.andExpect(xpath("/apiError/message").string(equalTo(ApiParams.API_RESPONSE_NOT_ALLOWED_CAN_VIEW)));
}

@Test
public void getNonAvailableRecordInSubportalAsXml() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
MockHttpSession mockHttpSession = loginAsAdmin();

// Set the current node to the subportal
Source subportal = sourceRepository.findOneByName("external");
Assert.assertNotNull(subportal);
nodeInfo.setId(subportal.getUuid());
nodeInfo.setDefaultNode(false);

mockMvc.perform(get("/external/api/records/" + this.uuid + "/formatters/json")
.session(mockHttpSession)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound())
.andExpect(content().contentType(API_JSON_EXPECTED_ENCODING))
.andExpect(jsonPath("$.code").value(equalTo("resource_not_found")))
.andExpect(jsonPath("$.message").value(equalTo("Metadata not found")));

mockMvc.perform(get("/external/api/records/" + this.uuid + "/formatters/xml")
.session(mockHttpSession)
.accept(MediaType.APPLICATION_XML))
.andExpect(status().isNotFound())
.andExpect(content().contentType(MediaType.APPLICATION_XML))
.andExpect(xpath("/apiError/code").string(equalTo("resource_not_found")))
.andExpect(xpath("/apiError/message").string(equalTo("Metadata not found")));
}


@Test
public void getNonExistentRecordAsXml() throws Exception {
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
Expand Down

0 comments on commit d6d6e4a

Please sign in to comment.