Skip to main content
summaryrefslogtreecommitdiffstats
blob: ab72b35ee6dcd78ffd80facb7d7b21b196806969 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*******************************************************************************
 * Copyright (c) 2015 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.orcs.db.intergration;

import static org.eclipse.osee.framework.core.enums.CoreBranches.COMMON;
import static org.eclipse.osee.orcs.db.intergration.IntegrationUtil.integrationRule;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.osee.framework.core.data.ArtifactId;
import org.eclipse.osee.framework.core.enums.CoreArtifactTokens;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.jdk.core.type.OseeStateException;
import org.eclipse.osee.jdbc.JdbcClient;
import org.eclipse.osee.jdbc.JdbcService;
import org.eclipse.osee.jdbc.JdbcStatement;
import org.eclipse.osee.orcs.OrcsApi;
import org.eclipse.osee.orcs.data.ArtifactReadable;
import org.eclipse.osee.orcs.db.internal.callable.PurgeAttributesDatabaseTxCallable;
import org.eclipse.osee.orcs.db.internal.sql.join.SqlJoinFactory;
import org.eclipse.osee.orcs.db.mock.OsgiService;
import org.eclipse.osee.orcs.transaction.TransactionBuilder;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;

/**
 * @author John Misinco
 */
public class PurgeAttributeTest {

   private static final ArtifactId Joe_Smith = ArtifactId.valueOf(61106791L);
   private static final String UNIQUE_ATTR_VALUE = "AEqvcQoyRV8zNDZSBVAA";

   @Rule
   public TestRule db = integrationRule(this);

   @OsgiService
   public OrcsApi orcsApi;
   @OsgiService
   public JdbcService jdbcService;
   @OsgiService
   public SqlJoinFactory sqlJoinFactory;

   @After
   @Before
   public void setupAndTeardown() {
      TransactionBuilder tx = null;
      for (ArtifactReadable art : orcsApi.getQueryFactory().fromBranch(COMMON).and(CoreAttributeTypes.Annotation,
         UNIQUE_ATTR_VALUE).getResults()) {
         if (tx == null) {
            tx = orcsApi.getTransactionFactory().createTransaction(COMMON, Joe_Smith, getClass().getSimpleName());
         }
         tx.deleteArtifact(art);
      }
      if (tx != null) {
         tx.commit();
      }
   }

   @Test
   public void testPurgeAttribute() throws Exception {
      JdbcClient jdbcClient = jdbcService.getClient();

      if (jdbcClient.getConfig().isProduction()) {
         throw new OseeStateException("Test should not be run against a Production Database");
      }

      // Setup test
      TransactionBuilder tx =
         orcsApi.getTransactionFactory().createTransaction(COMMON, Joe_Smith, getClass().getSimpleName());
      tx.createAttribute(CoreArtifactTokens.UserGroups, CoreAttributeTypes.Annotation, UNIQUE_ATTR_VALUE);
      tx.createAttribute(CoreArtifactTokens.Everyone, CoreAttributeTypes.Annotation, UNIQUE_ATTR_VALUE);
      tx.createAttribute(CoreArtifactTokens.OseeAdmin, CoreAttributeTypes.Annotation, UNIQUE_ATTR_VALUE);
      tx.commit();

      JdbcStatement stmt = jdbcClient.getStatement();

      int prePurgeAttributeCount = getCount(jdbcClient, "osee_attribute");
      int preAttributeRows =
         getCount(jdbcClient, String.format("osee_attribute where value = '%s'", UNIQUE_ATTR_VALUE));

      Assert.assertTrue(preAttributeRows == 3);

      int prePurgeTxsCount = getCount(jdbcClient, "osee_txs");

      stmt.runPreparedQuery(String.format("select attr_id from osee_attribute where value = '%s'", UNIQUE_ATTR_VALUE));
      List<Long> toPurge = new LinkedList<>();
      while (stmt.next()) {
         toPurge.add(stmt.getLong("attr_id"));
      }

      PurgeAttributesDatabaseTxCallable callable =
         new PurgeAttributesDatabaseTxCallable(null, null, jdbcClient, sqlJoinFactory, toPurge, null);

      callable.call();

      int postPurgeAttributeCount = getCount(jdbcClient, "osee_attribute");
      int postAttributeRows =
         getCount(jdbcClient, String.format("osee_attribute where value = '%s'", UNIQUE_ATTR_VALUE));
      int postPurgeTxsCount = getCount(jdbcClient, "osee_txs");

      Assert.assertEquals(0, postAttributeRows);
      Assert.assertEquals(prePurgeAttributeCount - preAttributeRows, postPurgeAttributeCount);
      Assert.assertTrue(postPurgeAttributeCount < prePurgeAttributeCount);
      Assert.assertTrue(postPurgeTxsCount < prePurgeTxsCount);
   }

   private int getCount(JdbcClient jdbcClient, String table) {
      JdbcStatement stmt = jdbcClient.getStatement();
      int toReturn = -1;
      stmt.runPreparedQuery("select count(1) from " + table);
      while (stmt.next()) {
         toReturn = stmt.getInt(1);
      }
      return toReturn;
   }
}

Back to the top