<commit_before>package org.openstack.atlas.service.domain.service;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.openstack.atlas.core.api.v1.Nodes;
import org.openstack.atlas.datamodel.AtlasTypeHelper;
import org.openstack.atlas.datamodel.CoreLoadBalancerStatus;
import org.openstack.atlas.datamodel.CoreProtocolType;
import org.openstack.atlas.service.domain.entity.LoadBalancer;
import org.openstack.atlas.service.domain.entity.Node;
import org.openstack.atlas.service.domain.exception.BadRequestException;
import org.openstack.atlas.service.domain.exception.EntityNotFoundException;
import org.openstack.atlas.service.domain.exception.UnprocessableEntityException;
import org.openstack.atlas.service.domain.repository.LoadBalancerRepository;
import org.openstack.atlas.service.domain.repository.NodeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;
import java.util.HashSet;
import java.util.Set;

@RunWith(Enclosed.class)
public class NodeServiceImplIntegrationTest {

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {"classpath:db-services-test.xml"})
    @Transactional
    @Service
    public static class WhenCreatingNodes {

        @Autowired
        private LoadBalancerService loadBalancerService;

        @Autowired
        private NodeService nodeService;

        @Autowired
        private LoadBalancerRepository loadBalancerRepository;

        @Autowired
        private NodeRepository nodeRepository;

        @Autowired
        private AtlasTypeHelper atlasTypeHelper;

        @PersistenceContext(unitName = "loadbalancing")
        private EntityManager entityManager;

        private LoadBalancer loadBalancer;
        private Node node;
        private Nodes nodes;

        @Before
        public void setUp() {
            loadBalancer = new LoadBalancer();
            loadBalancer.setAccountId(1000);
            loadBalancer.setName("integration testing");
            loadBalancer.setPort(80);
            loadBalancer.setProtocol(CoreProtocolType.HTTP);

            Set<Node> nodes = new HashSet<Node>();
            Node node = new Node();
            node.setAddress("1.2.2.1");
            node.setPort(80);
            node.setEnabled(true);
            nodes.add(node);
            loadBalancer.setNodes(nodes);
        }

        @After
        public void tearDown() {

        }

        @Test(expected = PersistenceException.class)
        public void shouldThrowExceptionWhenNodeIsNull() throws Exception {
            LoadBalancer dbLoadBalancer = loadBalancerService.create(loadBalancer);
            dbLoadBalancer.setStatus("ACTIVE");
            node = new Node();
            dbLoadBalancer.getNodes().add(node);
            nodeService.createNodes(dbLoadBalancer);
        }

        @Test
        public void shouldAssignIdNodeWhenCreateSucceeds() throws Exception {
            LoadBalancer dbLoadBalancer = loadBalancerService.create(loadBalancer);
            dbLoadBalancer.setStatus("ACTIVE");
            node = new Node();
            node.setAddress("2.2.4.4");
            node.setPort(80);
            node.setEnabled(true);

            LoadBalancer pLb = new LoadBalancer();
            pLb.setAccountId(dbLoadBalancer.getAccountId());
            pLb.setId(dbLoadBalancer.getId());
            pLb.getNodes().add(node);

            Set<Node> rNodes = nodeService.createNodes(pLb);
            for (Node node : rNodes) {
                Assert.assertNotNull(node.getId());
            }
        }

        @Test(expected = UnprocessableEntityException.class)
        public void shouldRejectDuplicateNodes() throws Exception {
            LoadBalancer dbLoadBalancer = loadBalancerService.create(loadBalancer);
            dbLoadBalancer.setStatus("ACTIVE");
            node = new Node();
            node.setAddress("1.2.2.1");
            node.setPort(80);
            node.setEnabled(true);

            LoadBalancer pLb = new LoadBalancer();
            pLb.setAccountId(dbLoadBalancer.getAccountId());
            pLb.setId(dbLoadBalancer.getId());
            pLb.getNodes().add(node);

            Set<Node> rNodes = nodeService.createNodes(pLb);
            for (Node node : rNodes) {
                Assert.assertNotNull(node.getId());
            }
        }
    }

//    @RunWith(SpringJUnit4ClassRunner.class)
//    @ContextConfiguration(locations = {"classpath:db-services-test.xml"})
//    @Transactional
//    @Service
//    public static class WhenUpdatingNodes {
//
//        @Autowired
//        private LoadBalancerService loadBalancerService;
//
//        @Autowired
//        private NodeService nodeService;
//
//        @Autowired
//        private LoadBalancerRepository loadBalancerRepository;
//
//        @Autowired
//        private NodeRepository nodeRepository;
//
//        @Autowired
//        private AtlasTypeHelper atlasTypeHelper;
//
//        @PersistenceContext(unitName = "loadbalancing")
//        private EntityManager entityManager;
//
//        private LoadBalancer loadBalancer;
//        private Node node;
//        private Nodes nodes;
//
//        @Before
//        public void setUp() {
//            loadBalancer = new LoadBalancer();
//            loadBalancer.setAccountId(1000);
//            loadBalancer.setName("integration testing");
//            loadBalancer.setPort(80);
//            loadBalancer.setProtocol(CoreProtocolType.HTTP);
//
//            Set<Node> nodes = new HashSet<Node>();
//            Node node = new Node();
//            node.setAddress("1.2.2.1");
//            node.setPort(80);
//            node.setEnabled(true);
//            nodes.add(node);
//            loadBalancer.setNodes(nodes);
//        }
//
//        @After
//        public void tearDown() {
//
//        }
//
//        @Test
//        public void shouldThrowExceptionWhenNodeIsNull() throws Exception {
//            loadBalancer.setStatus("ACTIVE");
//            for (Node node1 : loadBalancer.getNodes()) {
//                node1.setEnabled(false);
//            }
//
//            nodeService.updateNode(loadBalancer);
//        }
//        //TODO: more tests...
//    }
}

<commit_msg>tests for updatenode<commit_after>package org.openstack.atlas.service.domain.service;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.openstack.atlas.core.api.v1.Nodes;
import org.openstack.atlas.datamodel.AtlasTypeHelper;
import org.openstack.atlas.datamodel.CoreLoadBalancerStatus;
import org.openstack.atlas.datamodel.CoreProtocolType;
import org.openstack.atlas.service.domain.entity.LoadBalancer;
import org.openstack.atlas.service.domain.entity.Node;
import org.openstack.atlas.service.domain.exception.BadRequestException;
import org.openstack.atlas.service.domain.exception.EntityNotFoundException;
import org.openstack.atlas.service.domain.exception.UnprocessableEntityException;
import org.openstack.atlas.service.domain.repository.LoadBalancerRepository;
import org.openstack.atlas.service.domain.repository.NodeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;
import java.util.HashSet;
import java.util.Set;

@RunWith(Enclosed.class)
public class NodeServiceImplIntegrationTest {

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {"classpath:db-services-test.xml"})
    @Transactional
    @Service
    public static class WhenCreatingNodes {

        @Autowired
        private LoadBalancerService loadBalancerService;

        @Autowired
        private NodeService nodeService;

        @Autowired
        private LoadBalancerRepository loadBalancerRepository;

        @Autowired
        private NodeRepository nodeRepository;

        @Autowired
        private AtlasTypeHelper atlasTypeHelper;

        @PersistenceContext(unitName = "loadbalancing")
        private EntityManager entityManager;

        private LoadBalancer loadBalancer;
        private Node node;
        private Nodes nodes;

        @Before
        public void setUp() {
            loadBalancer = new LoadBalancer();
            loadBalancer.setAccountId(1000);
            loadBalancer.setName("integration testing");
            loadBalancer.setPort(80);
            loadBalancer.setProtocol(CoreProtocolType.HTTP);

            Set<Node> nodes = new HashSet<Node>();
            Node node = new Node();
            node.setAddress("1.2.2.1");
            node.setPort(80);
            node.setEnabled(true);
            nodes.add(node);
            loadBalancer.setNodes(nodes);
        }

        @After
        public void tearDown() {

        }

        @Test(expected = PersistenceException.class)
        public void shouldThrowExceptionWhenNodeIsNull() throws Exception {
            LoadBalancer dbLoadBalancer = loadBalancerService.create(loadBalancer);
            dbLoadBalancer.setStatus("ACTIVE");
            node = new Node();
            dbLoadBalancer.getNodes().add(node);
            nodeService.createNodes(dbLoadBalancer);
        }

        @Test
        public void shouldAssignIdNodeWhenCreateSucceeds() throws Exception {
            LoadBalancer dbLoadBalancer = loadBalancerService.create(loadBalancer);
            dbLoadBalancer.setStatus("ACTIVE");
            node = new Node();
            node.setAddress("2.2.4.4");
            node.setPort(80);
            node.setEnabled(true);

            LoadBalancer pLb = new LoadBalancer();
            pLb.setAccountId(dbLoadBalancer.getAccountId());
            pLb.setId(dbLoadBalancer.getId());
            pLb.getNodes().add(node);

            Set<Node> rNodes = nodeService.createNodes(pLb);
            for (Node node : rNodes) {
                Assert.assertNotNull(node.getId());
            }
        }

        @Test(expected = UnprocessableEntityException.class)
        public void shouldRejectDuplicateNodes() throws Exception {
            LoadBalancer dbLoadBalancer = loadBalancerService.create(loadBalancer);
            dbLoadBalancer.setStatus("ACTIVE");
            node = new Node();
            node.setAddress("1.2.2.1");
            node.setPort(80);
            node.setEnabled(true);

            LoadBalancer pLb = new LoadBalancer();
            pLb.setAccountId(dbLoadBalancer.getAccountId());
            pLb.setId(dbLoadBalancer.getId());
            pLb.getNodes().add(node);

            Set<Node> rNodes = nodeService.createNodes(pLb);
            for (Node node : rNodes) {
                Assert.assertNotNull(node.getId());
            }
        }
    }

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {"classpath:db-services-test.xml"})
    @Transactional
    @Service
    public static class WhenUpdatingNodes {

        @Autowired
        private LoadBalancerService loadBalancerService;

        @Autowired
        private NodeService nodeService;

        @Autowired
        private LoadBalancerRepository loadBalancerRepository;

        @Autowired
        private NodeRepository nodeRepository;

        @Autowired
        private AtlasTypeHelper atlasTypeHelper;

        @PersistenceContext(unitName = "loadbalancing")
        private EntityManager entityManager;

        private LoadBalancer loadBalancer;
        private Node node;
        private Nodes nodes;

        @Before
        public void setUp() {
            loadBalancer = new LoadBalancer();
            loadBalancer.setAccountId(1000);
            loadBalancer.setName("integration testing");
            loadBalancer.setPort(80);
            loadBalancer.setProtocol(CoreProtocolType.HTTP);

            Set<Node> nodes = new HashSet<Node>();
            Node node = new Node();
            node.setAddress("1.2.2.1");
            node.setPort(80);
            node.setEnabled(true);
            nodes.add(node);
            loadBalancer.setNodes(nodes);
        }

        @After
        public void tearDown() {

        }

        @Test(expected = UnprocessableEntityException.class)
        public void shouldThrowExceptionWhenDisablingLastNode() throws Exception {
            LoadBalancer dbLoadBalancer = loadBalancerService.create(loadBalancer);
            dbLoadBalancer.setStatus("ACTIVE");
            for (Node node1 : dbLoadBalancer.getNodes()) {
                node1.setEnabled(false);
            }
            nodeService.updateNode(dbLoadBalancer);
        }

        @Test
        public void shouldAssignIdNodeWhenCreateSucceeds() throws Exception {
            LoadBalancer dbLoadBalancer = loadBalancerService.create(loadBalancer);
            dbLoadBalancer.setStatus("ACTIVE");
            node = new Node();
            node.setAddress("2.2.4.4");
            node.setPort(80);
            node.setEnabled(true);

            LoadBalancer pLb = new LoadBalancer();
            pLb.setAccountId(dbLoadBalancer.getAccountId());
            pLb.setId(dbLoadBalancer.getId());
            pLb.getNodes().add(node);

            Set<Node> nodes1 = nodeService.createNodes(pLb);

            LoadBalancer updatedLb = loadBalancerRepository.getByIdAndAccountId(dbLoadBalancer.getId(), dbLoadBalancer.getAccountId());

            for (Node node : updatedLb.getNodes()) {
                Assert.assertNotNull(node.getId());
            }
        }

        @Test
        public void shouldSuccessfullyUpdateNode() throws Exception {
            LoadBalancer dbLoadBalancer = loadBalancerService.create(loadBalancer);
            dbLoadBalancer.setStatus("ACTIVE");

            node = new Node();
            node.setAddress("2.2.4.4");
            node.setPort(80);
            node.setEnabled(true);

            LoadBalancer pLb = new LoadBalancer();
            pLb.setAccountId(dbLoadBalancer.getAccountId());
            pLb.setId(dbLoadBalancer.getId());
            pLb.getNodes().add(node);

            nodeService.createNodes(pLb);
            dbLoadBalancer.setStatus("ACTIVE");

            LoadBalancer updatedLb = loadBalancerRepository.getByIdAndAccountId(dbLoadBalancer.getId(), dbLoadBalancer.getAccountId());

            for (Node node1 : updatedLb.getNodes()) {
                node1.setEnabled(false);
                break;
            }
            nodeService.updateNode(updatedLb);

             LoadBalancer updatedLb1 = loadBalancerRepository.getByIdAndAccountId(dbLoadBalancer.getId(), dbLoadBalancer.getAccountId());
            for (Node node : updatedLb.getNodes()) {
                Assert.assertEquals(false, node.isEnabled());
            }
        }
        //TODO: more tests...
    }
}


{'fraction_non_alphanumeric': 0.10043780582024209, 'fraction_numerical': 0.006567087303631213, 'mean_word_length': 2.470285969615728, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 10, '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': '4675717', 'n_tokens_mistral': 4239, 'n_tokens_neox': 4189, 'n_words': 685}