<commit_before>package com.gbr.nyan.feature;

import com.gbr.nyan.domain.Account;
import com.gbr.nyan.domain.AccountRepository;
import com.gbr.nyan.domain.User;
import com.gbr.nyan.domain.UserRepository;
import com.gbr.nyan.support.FakeAppDirect;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.web.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

import static com.gbr.nyan.domain.Account.Edition.BASIC;
import static com.gbr.nyan.domain.Account.Edition.PREMIUM;
import static com.gbr.nyan.support.HttpClientHelper.anAppDirectHttpClient;
import static com.gbr.nyan.support.HttpClientHelper.get;
import static com.gbr.nyan.support.Iterables.toList;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
public class CanChangeSubscription {
    @LocalServerPort
    private int serverPort;
    private FakeAppDirect fakeAppDirect;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private AccountRepository accountRepository;

    @Before
    public void setup() throws Exception {
        accountRepository.deleteAll();
        userRepository.deleteAll();

        fakeAppDirect = FakeAppDirect.create(42534).start();
    }

    @After
    public void stopFakeServer() {
        fakeAppDirect.stop();
    }

    @Test
    public void respondsToStatelessEventsWithWellFormedErrorResponse() throws Exception {
        HttpResponse response = anAppDirectHttpClient().execute(get(changeSubscription(), "eventUrl", "http://localhost:42534/v1/events/dummyChange"));

        assertThat(fakeAppDirect.lastRequestPath(), is("/v1/events/dummyChange"));
        assertThat(response.getStatusLine().getStatusCode(), is(200));
        assertThat(EntityUtils.toString(response.getEntity()), is("{\"errorCode\":\"UNKNOWN_ERROR\",\"success\":\"false\"}"));
    }

    @Test
    @Ignore("until I get a sample subscription change!")
    public void updatesUserEditionAccordingToChangeEvent() throws Exception {
        Account existingBasicAccount = saveAnAccount(BASIC);
        saveAUserWith("<EMAIL>", existingBasicAccount);

        HttpResponse response = anAppDirectHttpClient().execute(get(changeSubscription(), "eventUrl", "http://localhost:42534/v1/events/dev-change"));
        assertThat(fakeAppDirect.lastRequestPath(), is("/v1/events/dev-change"));
        assertThat(response.getStatusLine().getStatusCode(), is(200));<commit_msg>fix to prevent FKs from exploding.<commit_after>package com.gbr.nyan.feature;

import com.gbr.nyan.domain.Account;
import com.gbr.nyan.domain.AccountRepository;
import com.gbr.nyan.domain.User;
import com.gbr.nyan.domain.UserRepository;
import com.gbr.nyan.support.FakeAppDirect;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.web.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

import static com.gbr.nyan.domain.Account.Edition.BASIC;
import static com.gbr.nyan.domain.Account.Edition.PREMIUM;
import static com.gbr.nyan.support.HttpClientHelper.anAppDirectHttpClient;
import static com.gbr.nyan.support.HttpClientHelper.get;
import static com.gbr.nyan.support.Iterables.toList;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
public class CanChangeSubscription {
    @LocalServerPort
    private int serverPort;
    private FakeAppDirect fakeAppDirect;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private AccountRepository accountRepository;

    @Before
    public void setup() throws Exception {
        userRepository.deleteAll();
        accountRepository.deleteAll();

        fakeAppDirect = FakeAppDirect.create(42534).start();
    }

    @After
    public void stopFakeServer() {
        fakeAppDirect.stop();
    }

    @Test
    public void respondsToStatelessEventsWithWellFormedErrorResponse() throws Exception {
        HttpResponse response = anAppDirectHttpClient().execute(get(changeSubscription(), "eventUrl", "http://localhost:42534/v1/events/dummyChange"));

        assertThat(fakeAppDirect.lastRequestPath(), is("/v1/events/dummyChange"));
        assertThat(response.getStatusLine().getStatusCode(), is(200));
        assertThat(EntityUtils.toString(response.getEntity()), is("{\"errorCode\":\"UNKNOWN_ERROR\",\"success\":\"false\"}"));
    }

    @Test
    @Ignore("until I get a sample subscription change!")
    public void updatesUserEditionAccordingToChangeEvent() throws Exception {
        Account existingBasicAccount = saveAnAccount(BASIC);
        saveAUserWith("<EMAIL>", existingBasicAccount);

        HttpResponse response = anAppDirectHttpClient().execute(get(changeSubscription(), "eventUrl", "http://localhost:42534/v1/events/dev-change"));
        assertThat(fakeAppDirect.lastRequestPath(), is("/v1/events/dev-change"));
        assertThat(response.getStatusLine().getStatusCode(), is(200));
{'fraction_non_alphanumeric': 0.1163185600271693, 'fraction_numerical': 0.008830022075055188, 'mean_word_length': 4.96757852077001, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '2013372', 'n_tokens_mistral': 1707, 'n_tokens_neox': 1689, 'n_words': 284}