<commit_before>package fi.otavanopisto.kuntaapi.test;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.removeStub;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.github.tomakehurst.wiremock.client.MappingBuilder;
import com.github.tomakehurst.wiremock.matching.StringValuePattern;
import com.github.tomakehurst.wiremock.matching.UrlPattern;

@SuppressWarnings ("squid:S1166")
public class ResourceMocker<I, R> {

  private static final String APPLICATION_JSON = "application/json";
  private static final String CONTENT_TYPE = "Content-Type";

  private EnumMap<MockedResourceStatus, MappingBuilder> statusLists = new EnumMap<>(MockedResourceStatus.class);
  private boolean started = false;
  private Map<I, MockedResource<R>> resources = new LinkedHashMap<>();
  private Map<I, List<ResourceMocker<?, ?>>> subMockers = new LinkedHashMap<>();
  
  public void start() {
    started = true;
    
    for (MockedResource<R> resource : resources.values()) {
      stubFor(resource.getCurrentMapping());
    }
    
    for (Entry<MockedResourceStatus, MappingBuilder> statusListEntry : statusLists.entrySet()) {
      MappingBuilder mapping = statusListEntry.getValue();  
      MockedResourceStatus status = statusListEntry.getKey();
      mapping.willReturn(aResponse()
        .withHeader(CONTENT_TYPE, APPLICATION_JSON)
        .withBody(toJSON(getResources(status))));
      stubFor(mapping);
    }
    
    for (List<ResourceMocker<?, ?>> subMockers : subMockers.values()) {
      for (ResourceMocker<?, ?> subMocker : subMockers) {
        subMocker.start();
      }
    }
  }
  
  public void stop() {
    if (!started) {
      return;
    }
    
    started = false;
    
    for (List<ResourceMocker<?, ?>> subMockers : subMockers.values()) {
      for (ResourceMocker<?, ?> subMocker : subMockers) {
        subMocker.stop();
      }
    }

    for (MockedResource<R> resource : resources.values()) {
      for (MappingBuilder mapping : resource.getMappings()) {
        removeStub(mapping);
      }<commit_msg>Sonar issues<commit_after>package fi.otavanopisto.kuntaapi.test;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.removeStub;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.github.tomakehurst.wiremock.client.MappingBuilder;
import com.github.tomakehurst.wiremock.matching.StringValuePattern;
import com.github.tomakehurst.wiremock.matching.UrlPattern;

@SuppressWarnings ("squid:S1166")
public class ResourceMocker<I, R> {

  private static final String APPLICATION_JSON = "application/json";
  private static final String CONTENT_TYPE = "Content-Type";

  private EnumMap<MockedResourceStatus, MappingBuilder> statusLists = new EnumMap<>(MockedResourceStatus.class);
  private boolean started = false;
  private Map<I, MockedResource<R>> resources = new LinkedHashMap<>();
  private Map<I, List<ResourceMocker<?, ?>>> subMockers = new LinkedHashMap<>();
  
  public void start() {
    started = true;
    
    for (MockedResource<R> resource : resources.values()) {
      stubFor(resource.getCurrentMapping());
    }
    
    for (Entry<MockedResourceStatus, MappingBuilder> statusListEntry : statusLists.entrySet()) {
      MappingBuilder mapping = statusListEntry.getValue();  
      MockedResourceStatus status = statusListEntry.getKey();
      mapping.willReturn(aResponse()
        .withHeader(CONTENT_TYPE, APPLICATION_JSON)
        .withBody(toJSON(getResources(status))));
      stubFor(mapping);
    }
    
    for (List<ResourceMocker<?, ?>> subMockerList : subMockers.values()) {
      for (ResourceMocker<?, ?> subMocker : subMockerList) {
        subMocker.start();
      }
    }
  }
  
  public void stop() {
    if (!started) {
      return;
    }
    
    started = false;
    
    for (List<ResourceMocker<?, ?>> subMockerList : subMockers.values()) {
      for (ResourceMocker<?, ?> subMocker : subMockerList) {
        subMocker.stop();
      }
    }

    for (MockedResource<R> resource : resources.values()) {
      for (MappingBuilder mapping : resource.getMappings()) {
        removeStub(mapping);
      }
{'fraction_non_alphanumeric': 0.11712918660287082, 'fraction_numerical': 0.002679425837320574, 'mean_word_length': 4.226, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4229519', 'n_tokens_mistral': 1624, 'n_tokens_neox': 1569, 'n_words': 289}