Skip to main content
summaryrefslogtreecommitdiffstats
blob: 35054d4a75296066e4d87e47ce9e989aec944076 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*******************************************************************************
 * Copyright (c) 2004, 2007 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.framework.skynet.core.test.integration;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;
import org.eclipse.osee.framework.core.client.ClientSessionManager;
import org.eclipse.osee.framework.core.enums.SystemUser;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.skynet.core.User;
import org.eclipse.osee.framework.skynet.core.UserManager;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.linking.LinkType;
import org.eclipse.osee.framework.skynet.core.linking.WordMlLinkHandler;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;

/**
 * @author Roberto E. Escobar
 */
public class WordMlLinkHandlerTest {

   /**
    * Data driven test to check document link manager link/unlink methods
    */
   @org.junit.Test
   public void testLinkUnLink() throws Exception {
      User user = UserManager.getUser(SystemUser.OseeSystem);
      String guid = user.getGuid();
      String sessionId = ClientSessionManager.getSessionId();
      Map<String, TestData> testMap = getTestData();
      for (String key : testMap.keySet()) {
         TestData testData = testMap.get(key);

         InputStream dataStream = null;
         InputStream expectedStream = null;
         try {
            dataStream = new BufferedInputStream(testData.data.openStream());
            expectedStream = new BufferedInputStream(testData.expected.openStream());

            LinkType docType = testData.docType;
            boolean isLinkTest = testData.isLink;

            String input = Lib.inputStreamToString(dataStream);
            input = input.replaceAll("#GUID#", guid);
            input = input.replaceAll("#SESSION#", sessionId);
            Artifact source = user;

            // TODO this test will fail - add live artifact -- need to change test to use artifact instead of
            // input files.
            if (source != null) {
               if (isLinkTest) {
                  WordMlLinkHandler.link(docType, source, input);
               } else {
                  WordMlLinkHandler.unlink(docType, source, input);
               }
               String expected = Lib.inputStreamToString(expectedStream);
               expected = expected.replaceAll("#GUID#", guid);
               expected = expected.replaceAll("#SESSION#", sessionId);
            }
         } finally {
            if (dataStream != null) {
               try {
                  dataStream.close();
               } catch (IOException ex) {
                  // do nothing
               }
            }
            if (expectedStream != null) {
               try {
                  expectedStream.close();
               } catch (IOException ex) {
                  // do nothing
               }
            }
         }
      }
   }

   private String getFileName(String name) {
      int index = name.lastIndexOf("/");
      if (index > -1) {
         name = name.substring(index + 1, name.length());
      }
      if (name.endsWith(".data.xml")) {
         name = name.substring(0, name.length() - 9);
      }
      if (name.endsWith(".expected.xml")) {
         name = name.substring(0, name.length() - 13);
      }
      return name;
   }

   private boolean isDataFile(String name) {
      return name != null && name.endsWith(".data.xml");
   }

   private boolean isExpectedFile(String name) {
      return name != null && name.endsWith(".expected.xml");
   }

   private LinkType getDocType(String name) {
      return LinkType.OSEE_SERVER_LINK;
   }

   private boolean isLinkTest(String name) {
      return !name.contains("unlink");
   }

   private Map<String, TestData> getTestData() {
      Map<String, TestData> toReturn = new LinkedHashMap<String, TestData>();

      Bundle bundle = FrameworkUtil.getBundle(WordMlLinkHandlerTest.class).getBundleContext().getBundle();
      Enumeration<?> urls = bundle.findEntries("support/WordMlLinkData", "*.*", true);
      while (urls.hasMoreElements()) {
         URL url = (URL) urls.nextElement();
         String name = getFileName(url.getPath());
         if (Strings.isValid(name) && (url.getPath().endsWith(".data.xml") || url.getPath().endsWith(".expected.xml"))) {
            String key = name;
            int index = name.indexOf('.');
            if (index > 0) {
               key = name.substring(0, index);
            }
            TestData pair = toReturn.get(key);
            if (pair == null) {
               pair = new TestData();
               toReturn.put(key, pair);
            }
            if (isDataFile(url.getPath())) {
               pair.data = url;
               pair.docType = getDocType(name);
               pair.isLink = isLinkTest(name);
            } else if (isExpectedFile(url.getPath())) {
               pair.expected = url;
            } else if (pair.data == null || pair.expected == null) {
               toReturn.remove(pair);
            }
         }
      }
      return toReturn;
   }
   private class TestData {
      public boolean isLink;
      private URL data;
      private URL expected;
      private LinkType docType;
   }
}

Back to the top