Skip to content

Commit

Permalink
ANGOLASUP-859: Provide enhancement for Embedded Reports (#51)
Browse files Browse the repository at this point in the history
* ANGOLASUP-859: Renamed deleteEmbeddedReport method

* ANGOLASUP-859: Added EmbeddedReportCategory entity with CRUD

* ANGOLASUP-859: Change type for category field

* ANGOLASUP-859: Fixed integration tests

* ANGOLASUP-859: Removed constraint from categoryId column
  • Loading branch information
sradziszewski authored May 14, 2024
1 parent 2449152 commit df34cea
Show file tree
Hide file tree
Showing 28 changed files with 984 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.openlmis.ao.reports.repository;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

import org.junit.Test;
import org.openlmis.ao.reports.domain.EmbeddedReportCategory;
import org.openlmis.ao.testutils.EmbeddedReportCategoryDataBuilder;
import org.springframework.beans.factory.annotation.Autowired;

public class EmbeddedReportCategoryRepositoryIntegrationTest extends
BaseCrudRepositoryIntegrationTest<EmbeddedReportCategory> {

private static final String NAME = "EmbeddedReportCategoryIntegrationTest";

@Autowired
private EmbeddedReportCategoryRepository embeddedReportCategoryRepository;

@Override
EmbeddedReportCategoryRepository getRepository() {
return this.embeddedReportCategoryRepository;
}

@Override
protected EmbeddedReportCategory generateInstance() {
return new EmbeddedReportCategoryDataBuilder().withName(NAME).buildAsNew();
}

@Test
public void shouldFindEmbeddedReportCategoryByName() {
embeddedReportCategoryRepository.save(generateInstance());

EmbeddedReportCategory found = embeddedReportCategoryRepository.findByName(NAME);

assertThat(found.getName(), is(NAME));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.List;
import org.junit.Test;
import org.openlmis.ao.reports.domain.EmbeddedReport;
import org.openlmis.ao.reports.domain.EmbeddedReportCategory;
import org.openlmis.ao.testutils.EmbeddedReportCategoryDataBuilder;
import org.openlmis.ao.testutils.EmbeddedReportDataBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
Expand All @@ -21,14 +23,23 @@ public class EmbeddedReportRepositoryIntegrationTest extends
@Autowired
private EmbeddedReportRepository embeddedReportRepository;

@Autowired
private EmbeddedReportCategoryRepository embeddedReportCategoryRepository;

@Override
EmbeddedReportRepository getRepository() {
return this.embeddedReportRepository;
}

@Override
protected EmbeddedReport generateInstance() {
return new EmbeddedReportDataBuilder().withName(NAME).buildAsNew();
EmbeddedReportCategory category = embeddedReportCategoryRepository
.save(new EmbeddedReportCategoryDataBuilder().buildAsNew());

return new EmbeddedReportDataBuilder()
.withName(NAME)
.withCategory(category)
.buildAsNew();
}

@Test
Expand All @@ -43,19 +54,28 @@ public void shouldFindEmbeddedReportByName() {
@Test
public void shouldFindAllEmbeddedReportsByCategory() {
Pageable pageable = new PageRequest(0, 2);
EmbeddedReportCategory defaultCategory = embeddedReportCategoryRepository.save(
new EmbeddedReportCategoryDataBuilder().withName(DEFAULT_CATEGORY).build()
);
EmbeddedReportCategory uniqueCategory = embeddedReportCategoryRepository.save(
new EmbeddedReportCategoryDataBuilder().withName(UNIQUE_CATEGORY).build()
);
EmbeddedReport matchingReport1 = new EmbeddedReportDataBuilder()
.withCategory(DEFAULT_CATEGORY).build();
.withCategory(defaultCategory)
.build();
EmbeddedReport matchingReport2 = new EmbeddedReportDataBuilder()
.withCategory(DEFAULT_CATEGORY).build();
.withCategory(defaultCategory)
.build();
EmbeddedReport notMatchingReport = new EmbeddedReportDataBuilder()
.withCategory(UNIQUE_CATEGORY).build();
.withCategory(uniqueCategory)
.build();

embeddedReportRepository.save(matchingReport1);
embeddedReportRepository.save(matchingReport2);
embeddedReportRepository.save(notMatchingReport);

List<EmbeddedReport> found =
embeddedReportRepository.findAllByCategory(DEFAULT_CATEGORY, pageable).getContent();
embeddedReportRepository.findAllByCategoryName(DEFAULT_CATEGORY, pageable).getContent();

assertThat(found.size(), is(2));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package org.openlmis.ao.reports.web;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.verify;

import guru.nidi.ramltester.junit.RamlMatchers;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import org.junit.Before;
import org.junit.Test;
import org.openlmis.ao.reports.domain.EmbeddedReportCategory;
import org.openlmis.ao.reports.dto.EmbeddedReportCategoryDto;
import org.openlmis.ao.reports.repository.EmbeddedReportCategoryRepository;
import org.openlmis.ao.testutils.EmbeddedReportCategoryDataBuilder;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;

@SuppressWarnings("PMD.TooManyMethods")
public class EmbeddedReportCategoryControllerIntegrationTest extends BaseWebIntegrationTest {

private static final String RESOURCE_URL = "/api/reports/embeddedReportCategories";
private static final String ID_URL = RESOURCE_URL + "/{id}";
public static final String ID = "id";

@MockBean
private EmbeddedReportCategoryRepository embeddedReportCategoryRepository;

@Before
public void setUp() {
mockUserAuthenticated();
doNothing().when(permissionService).canViewEmbeddedReportsCategory();
}

@Test
public void shouldGetExistentEmbeddedReportCategory() {
// given
EmbeddedReportCategory embeddedReportCategory =
new EmbeddedReportCategoryDataBuilder().build();
given(embeddedReportCategoryRepository.findOne(embeddedReportCategory.getId()))
.willReturn(embeddedReportCategory);

// when
EmbeddedReportCategoryDto result = restAssured.given()
.queryParam(ACCESS_TOKEN, getToken())
.contentType(MediaType.APPLICATION_JSON_VALUE)
.pathParam(ID, embeddedReportCategory.getId())
.when()
.get(ID_URL)
.then()
.statusCode(200)
.extract().as(EmbeddedReportCategoryDto.class);

// then
assertEquals(embeddedReportCategory.getId(), result.getId());
assertThat(RAML_ASSERT_MESSAGE, restAssured.getLastReport(), RamlMatchers.hasNoViolations());
}

@Test
public void shouldNotGetNonExistentEmbeddedReportCategory() {
// given
given(embeddedReportCategoryRepository.findOne(anyUuid())).willReturn(null);

// when
restAssured.given()
.queryParam(ACCESS_TOKEN, getToken())
.contentType(MediaType.APPLICATION_JSON_VALUE)
.pathParam(ID, UUID.randomUUID())
.when()
.get(ID_URL)
.then()
.statusCode(404);

// then
assertThat(RAML_ASSERT_MESSAGE, restAssured.getLastReport(), RamlMatchers.hasNoViolations());
}

@Test
public void shouldGetAllEmbeddedReportCategories() {
// given
EmbeddedReportCategory category1 = new EmbeddedReportCategoryDataBuilder().build();
EmbeddedReportCategory category2 = new EmbeddedReportCategoryDataBuilder().build();
List<EmbeddedReportCategory> categories = Arrays.asList(category1, category2);

Pageable pageable = new PageRequest(0, categories.size());
Page<EmbeddedReportCategory> categoryPage = new PageImpl<>(categories, pageable,
categories.size());

given(embeddedReportCategoryRepository.findAll(pageable)).willReturn(categoryPage);

// when
restAssured.given()
.queryParam(ACCESS_TOKEN, getToken())
.queryParam("page", pageable.getPageNumber())
.queryParam("size", pageable.getPageSize())
.when()
.get(RESOURCE_URL)
.then()
.statusCode(200);

// then
assertThat(RAML_ASSERT_MESSAGE, restAssured.getLastReport(), RamlMatchers.hasNoViolations());
}

@Test
public void shouldDeleteExistentEmbeddedReportCategory() {
// given
EmbeddedReportCategory embeddedReportCategory =
new EmbeddedReportCategoryDataBuilder().build();
given(embeddedReportCategoryRepository.findOne(embeddedReportCategory.getId()))
.willReturn(embeddedReportCategory);

// when
restAssured.given()
.queryParam(ACCESS_TOKEN, getToken())
.contentType(MediaType.APPLICATION_JSON_VALUE)
.pathParam(ID, embeddedReportCategory.getId())
.when()
.delete(ID_URL)
.then()
.statusCode(204);

// then
verify(embeddedReportCategoryRepository, atLeastOnce()).delete(eq(embeddedReportCategory));
assertThat(RAML_ASSERT_MESSAGE, restAssured.getLastReport(), RamlMatchers.hasNoViolations());
}

@Test
public void shouldNotDeleteNonExistentEmbeddedReportCategory() {
// given
given(embeddedReportCategoryRepository.findOne(anyUuid())).willReturn(null);

// when
restAssured.given()
.queryParam(ACCESS_TOKEN, getToken())
.contentType(MediaType.APPLICATION_JSON_VALUE)
.pathParam(ID, UUID.randomUUID())
.when()
.delete(ID_URL)
.then()
.statusCode(404);

// then
assertThat(RAML_ASSERT_MESSAGE, restAssured.getLastReport(), RamlMatchers.hasNoViolations());
}

}
Loading

0 comments on commit df34cea

Please sign in to comment.