<commit_before>package com.thinkbiganalytics.metadata.modeshape.sla;

import com.thinkbiganalytics.metadata.api.Command;
import com.thinkbiganalytics.metadata.api.category.Category;
import com.thinkbiganalytics.metadata.api.category.CategoryProvider;
import com.thinkbiganalytics.metadata.api.extension.ExtensibleEntity;
import com.thinkbiganalytics.metadata.api.extension.ExtensibleEntityProvider;
import com.thinkbiganalytics.metadata.api.extension.ExtensibleTypeProvider;
import com.thinkbiganalytics.metadata.api.feed.Feed;
import com.thinkbiganalytics.metadata.api.feed.FeedProvider;
import com.thinkbiganalytics.metadata.api.sla.FeedServiceLevelAgreement;
import com.thinkbiganalytics.metadata.api.sla.FeedServiceLevelAgreementProvider;
import com.thinkbiganalytics.metadata.modeshape.JcrMetadataAccess;
import com.thinkbiganalytics.metadata.modeshape.ModeShapeEngineConfig;
import com.thinkbiganalytics.metadata.modeshape.auth.AdminCredentials;
import com.thinkbiganalytics.metadata.modeshape.category.JcrCategory;
import com.thinkbiganalytics.metadata.modeshape.feed.JcrFeed;
import com.thinkbiganalytics.metadata.modeshape.generic.JcrExtensibleProvidersTestConfig;
import com.thinkbiganalytics.metadata.sla.api.ServiceLevelAgreement;
import com.thinkbiganalytics.metadata.sla.spi.ServiceLevelAgreementProvider;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.testng.Assert;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.inject.Inject;
import javax.jcr.Node;
import javax.jcr.RepositoryException;

/**
 * Created by sr186054 on 8/4/16.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {ModeShapeEngineConfig.class, JcrExtensibleProvidersTestConfig.class})
@ComponentScan(basePackages = {"com.thinkbiganalytics.metadata.modeshape.op"})
public class JcrFeedSlaTest {

    @Inject
    private ExtensibleTypeProvider typeProvider;

    @Inject
    private ExtensibleEntityProvider entityProvider;

    @Inject
    CategoryProvider categoryProvider;

    @Inject
    FeedProvider feedProvider;

    @Inject
    ServiceLevelAgreementProvider slaProvider;

    @Inject
    FeedServiceLevelAgreementProvider feedSlaProvider;

    @Inject
    private JcrMetadataAccess metadata;

    private static String FEED_SLA = "feedSla";


    public Set<Feed.ID> createFeeds(int number) {
        Set<Feed.ID> feedIds = new HashSet<>();
        String categorySystemName = "my_category";
        for (int i = 0; i < number; i++) {
            JcrFeed.FeedId feedId = createFeed(categorySystemName, "my_feed" + i);
            feedIds.add(feedId);
        }
        return feedIds;

    }


    public JcrFeed.FeedId createFeed(final String categorySystemName, final String feedSystemName) {

        Category category = metadata.commit(new AdminCredentials(), new Command<Category>() {
            @Override
            public Category execute() {
                JcrCategory category = (JcrCategory) categoryProvider.ensureCategory(categorySystemName);
                category.setDescription(categorySystemName + " desc");
                category.setTitle(categorySystemName);
                categoryProvider.update(category);
                return category;
            }
        });

        return metadata.commit(new AdminCredentials(), new Command<JcrFeed.FeedId>() {
            @Override
            public JcrFeed.FeedId execute() {

                JcrCategory category = (JcrCategory) categoryProvider.ensureCategory(categorySystemName);

                JcrFeed feed = (JcrFeed) feedProvider.ensureFeed(categorySystemName, feedSystemName, feedSystemName + " desc");
                feed.setTitle(feedSystemName);
                return feed.getId();

            }
        });
    }


    @Before
    public void setUp() throws Exception {
        JcrFeedServiceLevelAgreementProvider jcrFeedSlaProvider = (JcrFeedServiceLevelAgreementProvider) feedSlaProvider;
        jcrFeedSlaProvider.createType();
    }

    @Test
    public void testCreateFeedSLAEntity() {
        //create 2 feeds
        final int numberOfFeeds = 2;
        Set<Feed.ID> feedIds = createFeeds(numberOfFeeds);
        final String feedSlaTitle = "My New SLA";
        final String nonFeedSlaTitle = "No Feed SLA";
        ExtensibleEntity.ID feedSlaEntityId = createFeedSLAEntity(feedIds, feedSlaTitle);
        ServiceLevelAgreement.ID nonFeedSla = createGenericSla(nonFeedSlaTitle);

        ServiceLevelAgreement.ID slaId = metadata.read(new AdminCredentials(), () -> {

            JcrFeedServiceLevelAgreementProvider jcrFeedSlaProvider = (JcrFeedServiceLevelAgreementProvider) feedSlaProvider;

            //ASSERT everything is good

            //Assert query returns the correct result
            List<ExtensibleEntity> entities = jcrFeedSlaProvider.findAllRelationships();
            Assert.assertEquals(entities.size(), 1);

            //Assert relationships are correct
            JcrFeedServiceLevelAgreementRelationship entity = (JcrFeedServiceLevelAgreementRelationship) jcrFeedSlaProvider.getRelationship(feedSlaEntityId);
            ServiceLevelAgreement feedSla = entity.getAgreement();
            Assert.assertNotNull(feedSla);

            List<ServiceLevelAgreement> agreements = slaProvider.getAgreements();
            //assert both agreements are there
            Assert.assertEquals(agreements.size(), 2);
<commit_msg>fix testcase<commit_after>package com.thinkbiganalytics.metadata.modeshape.sla;

import com.thinkbiganalytics.metadata.api.Command;
import com.thinkbiganalytics.metadata.api.category.Category;
import com.thinkbiganalytics.metadata.api.category.CategoryProvider;
import com.thinkbiganalytics.metadata.api.extension.ExtensibleEntity;
import com.thinkbiganalytics.metadata.api.extension.ExtensibleEntityProvider;
import com.thinkbiganalytics.metadata.api.extension.ExtensibleTypeProvider;
import com.thinkbiganalytics.metadata.api.feed.Feed;
import com.thinkbiganalytics.metadata.api.feed.FeedProvider;
import com.thinkbiganalytics.metadata.api.sla.FeedServiceLevelAgreement;
import com.thinkbiganalytics.metadata.api.sla.FeedServiceLevelAgreementProvider;
import com.thinkbiganalytics.metadata.modeshape.JcrMetadataAccess;
import com.thinkbiganalytics.metadata.modeshape.ModeShapeEngineConfig;
import com.thinkbiganalytics.metadata.modeshape.auth.AdminCredentials;
import com.thinkbiganalytics.metadata.modeshape.category.JcrCategory;
import com.thinkbiganalytics.metadata.modeshape.feed.JcrFeed;
import com.thinkbiganalytics.metadata.modeshape.generic.JcrExtensibleProvidersTestConfig;
import com.thinkbiganalytics.metadata.sla.api.ServiceLevelAgreement;
import com.thinkbiganalytics.metadata.sla.spi.ServiceLevelAgreementProvider;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.testng.Assert;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.inject.Inject;
import javax.jcr.Node;
import javax.jcr.RepositoryException;

/**
 * Created by sr186054 on 8/4/16.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {ModeShapeEngineConfig.class, JcrExtensibleProvidersTestConfig.class})
@ComponentScan(basePackages = {"com.thinkbiganalytics.metadata.modeshape.op"})
public class JcrFeedSlaTest {

    @Inject
    private ExtensibleTypeProvider typeProvider;

    @Inject
    private ExtensibleEntityProvider entityProvider;

    @Inject
    CategoryProvider categoryProvider;

    @Inject
    FeedProvider feedProvider;

    @Inject
    ServiceLevelAgreementProvider slaProvider;

    @Inject
    FeedServiceLevelAgreementProvider feedSlaProvider;

    @Inject
    private JcrMetadataAccess metadata;

    private static String FEED_SLA = "feedSla";


    public Set<Feed.ID> createFeeds(int number) {
        Set<Feed.ID> feedIds = new HashSet<>();
        String categorySystemName = "my_category";
        for (int i = 0; i < number; i++) {
            JcrFeed.FeedId feedId = createFeed(categorySystemName, "my_feed" + i);
            feedIds.add(feedId);
        }
        return feedIds;

    }


    public JcrFeed.FeedId createFeed(final String categorySystemName, final String feedSystemName) {

        Category category = metadata.commit(new AdminCredentials(), new Command<Category>() {
            @Override
            public Category execute() {
                JcrCategory category = (JcrCategory) categoryProvider.ensureCategory(categorySystemName);
                category.setDescription(categorySystemName + " desc");
                category.setTitle(categorySystemName);
                categoryProvider.update(category);
                return category;
            }
        });

        return metadata.commit(new AdminCredentials(), new Command<JcrFeed.FeedId>() {
            @Override
            public JcrFeed.FeedId execute() {

                JcrCategory category = (JcrCategory) categoryProvider.ensureCategory(categorySystemName);

                JcrFeed feed = (JcrFeed) feedProvider.ensureFeed(categorySystemName, feedSystemName, feedSystemName + " desc");
                feed.setTitle(feedSystemName);
                return feed.getId();

            }
        });
    }


    @Before
    public void setUp() throws Exception {
        JcrFeedServiceLevelAgreementProvider jcrFeedSlaProvider = (JcrFeedServiceLevelAgreementProvider) feedSlaProvider;
        // jcrFeedSlaProvider.createType();
    }

    @Test
    public void testCreateFeedSLAEntity() {
        //create 2 feeds
        final int numberOfFeeds = 2;
        Set<Feed.ID> feedIds = createFeeds(numberOfFeeds);
        final String feedSlaTitle = "My New SLA";
        final String nonFeedSlaTitle = "No Feed SLA";
        ExtensibleEntity.ID feedSlaEntityId = createFeedSLAEntity(feedIds, feedSlaTitle);
        ServiceLevelAgreement.ID nonFeedSla = createGenericSla(nonFeedSlaTitle);

        ServiceLevelAgreement.ID slaId = metadata.read(new AdminCredentials(), () -> {

            JcrFeedServiceLevelAgreementProvider jcrFeedSlaProvider = (JcrFeedServiceLevelAgreementProvider) feedSlaProvider;

            //ASSERT everything is good

            //Assert query returns the correct result
            List<ExtensibleEntity> entities = jcrFeedSlaProvider.findAllRelationships();
            Assert.assertEquals(entities.size(), 1);

            //Assert relationships are correct
            JcrFeedServiceLevelAgreementRelationship entity = (JcrFeedServiceLevelAgreementRelationship) jcrFeedSlaProvider.getRelationship(feedSlaEntityId);
            ServiceLevelAgreement feedSla = entity.getAgreement();
            Assert.assertNotNull(feedSla);

            List<ServiceLevelAgreement> agreements = slaProvider.getAgreements();
            //assert both agreements are there
            Assert.assertEquals(agreements.size(), 2);

{'fraction_non_alphanumeric': 0.08333333333333333, 'fraction_numerical': 0.0031914893617021275, 'mean_word_length': 3.798383666524883, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '3234422', 'n_tokens_mistral': 3152, 'n_tokens_neox': 3023, 'n_words': 547}