Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rstief committed May 20, 2024
1 parent a1a34b5 commit 7b60ba9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
import org.springframework.web.bind.annotation.RestController;

import de.tum.in.www1.artemis.domain.competency.KnowledgeArea;
import de.tum.in.www1.artemis.domain.competency.Source;
import de.tum.in.www1.artemis.domain.competency.StandardizedCompetency;
import de.tum.in.www1.artemis.repository.SourceRepository;
import de.tum.in.www1.artemis.repository.competency.KnowledgeAreaRepository;
import de.tum.in.www1.artemis.repository.competency.StandardizedCompetencyRepository;
import de.tum.in.www1.artemis.security.annotations.EnforceAtLeastInstructor;
import de.tum.in.www1.artemis.service.competency.StandardizedCompetencyService;
import de.tum.in.www1.artemis.web.rest.dto.standardizedCompetency.KnowledgeAreaResultDTO;
import de.tum.in.www1.artemis.web.rest.dto.standardizedCompetency.SourceDTO;

/**
* REST controller for managing {@link StandardizedCompetency} entities.
Expand Down Expand Up @@ -103,10 +103,11 @@ public ResponseEntity<KnowledgeArea> getKnowledgeArea(@PathVariable long knowled
*/
@GetMapping("sources")
@EnforceAtLeastInstructor
public ResponseEntity<List<Source>> getSources() {
// TODO: replace with sourceDTO.
public ResponseEntity<List<SourceDTO>> getSources() {
log.debug("REST request to get all sources");

return ResponseEntity.ok().body(sourceRepository.findAll());
var sourceDTOs = sourceRepository.findAll().stream().map(SourceDTO::of).toList();

return ResponseEntity.ok().body(sourceDTOs);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package de.tum.in.www1.artemis.web.rest.dto.standardizedCompetency;

import de.tum.in.www1.artemis.domain.competency.Source;

/**
* DTO containing source information
*/
public record SourceDTO(long id, String title, String author, String uri) {

/**
* Creates a SourceDTO from the given Source
*
* @param source the Source
* @return the created SourceDTO
*/
public static SourceDTO of(Source source) {
return new SourceDTO(source.getId(), source.getTitle(), source.getAuthor(), source.getUri());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import de.tum.in.www1.artemis.web.rest.dto.standardizedCompetency.KnowledgeAreaRequestDTO;
import de.tum.in.www1.artemis.web.rest.dto.standardizedCompetency.KnowledgeAreaResultDTO;
import de.tum.in.www1.artemis.web.rest.dto.standardizedCompetency.KnowledgeAreasForImportDTO;
import de.tum.in.www1.artemis.web.rest.dto.standardizedCompetency.SourceDTO;
import de.tum.in.www1.artemis.web.rest.dto.standardizedCompetency.StandardizedCompetencyRequestDTO;
import de.tum.in.www1.artemis.web.rest.dto.standardizedCompetency.StandardizedCompetencyResultDTO;

Expand Down Expand Up @@ -468,12 +469,31 @@ void shouldReturnKnowledgeArea() throws Exception {

assertThat(actualKnowledgeArea).isEqualTo(expectedKnowledgeArea);
}

@Test
@WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR")
void shouldReturn404() throws Exception {
request.get("/api/standardized-competencies/knowledge-areas/" + ID_NOT_EXISTS, HttpStatus.NOT_FOUND, KnowledgeArea.class);
}
}

@Test
@WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR")
void shouldReturn404() throws Exception {
request.get("/api/standardized-competencies/knowledge-areas/" + ID_NOT_EXISTS, HttpStatus.NOT_FOUND, KnowledgeArea.class);
@Nested
class GetSources {

@Test
@WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR")
void shouldReturnSources() throws Exception {
var source1 = new Source("title", "author", "http://localhost:1");
source1.setId(1L);
var source2 = new Source("title2", "author2", "http://localhost:2");
source2.setId(2L);
var sources = sourceRepository.saveAll(List.of(source1, source2));
var expectedSources = sources.stream().map(SourceDTO::of).toList();

var actualSources = request.getList("/api/standardized-competencies/sources", HttpStatus.OK, SourceDTO.class);

assertThat(actualSources).containsAll(expectedSources);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('StandardizedCompetencyEditComponent', () => {
description: 'new description',
taxonomy: CompetencyTaxonomy.APPLY,
knowledgeAreaId: 2,
sourceId: 2,
};

const defaultKnowledgeAreas: KnowledgeAreaDTO[] = [
Expand Down Expand Up @@ -73,21 +74,21 @@ describe('StandardizedCompetencyEditComponent', () => {

it('should set form values to competency', () => {
componentFixture.detectChanges();
compareFormValues(component.form.getRawValue(), defaultCompetency);
compareFormValues(component['form'].getRawValue(), defaultCompetency);
});

it('should disable/enable when setting edit mode', () => {
component.isEditing = false;
componentFixture.detectChanges();
expect(component.form.disabled).toBeTrue();
expect(component['form'].disabled).toBeTrue();

component.edit();
expect(component.form.disabled).toBeFalse();
expect(component['form'].disabled).toBeFalse();
});

it('should save', () => {
component.isEditing = true;
component.form.setValue(newValues);
component['form'].setValue(newValues);
const saveSpy = jest.spyOn(component.onSave, 'emit');

component.save();
Expand All @@ -102,12 +103,12 @@ describe('StandardizedCompetencyEditComponent', () => {
])('should cancel and close', (competency, shouldClose) => {
component.competency = competency;
component.isEditing = true;
component.form.setValue(newValues);
component['form'].setValue(newValues);
const closeSpy = jest.spyOn(component.onClose, 'emit');

component.cancel();

compareFormValues(component.form.getRawValue(), competency);
compareFormValues(component['form'].getRawValue(), competency);
expect(component.isEditing).toBeFalse();
shouldClose ? expect(closeSpy).toHaveBeenCalled() : expect(closeSpy).not.toHaveBeenCalled();
});
Expand All @@ -129,7 +130,7 @@ describe('StandardizedCompetencyEditComponent', () => {
it('should update description', () => {
component.updateDescriptionControl('new description');

expect(component.form.controls.description.getRawValue()).toBe('new description');
expect(component['form'].controls.description.getRawValue()).toBe('new description');
});

function compareFormValues(formValues: any, competency: StandardizedCompetencyDTO) {
Expand Down

0 comments on commit 7b60ba9

Please sign in to comment.