Add catalogId field to catalog search selector
[federation.git] / gateway / src / test / java / org / acumos / federation / gateway / test / CatalogServiceTest.java
1 /*-
2  * ===============LICENSE_START=======================================================
3  * Acumos
4  * ===================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property & Tech Mahindra. All rights reserved.
6  * ===================================================================================
7  * This Acumos software file is distributed by AT&T and Tech Mahindra
8  * under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * This file is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ===============LICENSE_END=========================================================
19  */
20 package org.acumos.federation.gateway.test;
21
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24
25 import java.util.concurrent.CopyOnWriteArrayList;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.Map;
29 import java.util.List;
30
31 import org.acumos.cds.domain.MLPArtifact;
32 import org.acumos.cds.domain.MLPPeer;
33 import org.acumos.cds.domain.MLPSolution;
34 import org.acumos.cds.domain.MLPSolutionRevision;
35 import org.acumos.federation.gateway.cds.Solution;
36 import org.acumos.federation.gateway.security.Peer;
37 import org.acumos.federation.gateway.security.Role;
38 import org.acumos.federation.gateway.service.CatalogService;
39 import org.acumos.federation.gateway.service.ServiceContext;
40 import org.acumos.federation.gateway.service.ServiceException;
41 import org.junit.FixMethodOrder;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.junit.runners.MethodSorters;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.boot.test.context.SpringBootTest;
47 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
48 import org.springframework.test.context.junit4.SpringRunner;
49
50
51 /**
52  */
53
54 //@RunWith(SpringJUnit4ClassRunner.class)
55 @RunWith(SpringRunner.class)
56 @SpringBootTest(classes = org.acumos.federation.gateway.Application.class,
57                                                                 webEnvironment = WebEnvironment.RANDOM_PORT,
58                                                                 properties = {
59                                                                         "spring.main.allow-bean-definition-overriding=true",
60                                                                         "federation.instance=gateway",
61                                                                         "federation.instance.name=test",
62                                                                         "federation.operator=admin",
63                                                                         "federation.ssl.key-store=classpath:acumosa.pkcs12",
64                                                                         "federation.ssl.key-store-password=acumosa",
65                                                                         "federation.ssl.key-store-type=PKCS12",
66                                                                         "federation.ssl.key-password = acumosa",
67                                                                         "federation.ssl.trust-store=classpath:acumosTrustStore.jks",
68                                                                         "federation.ssl.trust-store-password=acumos",
69                                                                         "federation.ssl.client-auth=need",
70                                                                         "local.addr=127.0.0.1",
71                                                                         "local.server.port=9011",
72                                                                         "local.ssl.key-store=classpath:acumosa.pkcs12",
73                                                                         "local.ssl.key-store-password=acumosa",
74                                                                         "local.ssl.key-store-type=PKCS12",
75                                                                         "local.ssl.key-password=acumosa",
76                                                                         "cdms.client.url=http://localhost:8000/ccds",
77                                                                         "cdms.client.username=username",
78                                                                         "cdms.client.password=password"
79                                                                         })
80 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
81 public class CatalogServiceTest extends ServiceTest {
82
83         
84         @Autowired
85         private CatalogService catalog;
86
87         private static Map<String, Object> selector(Object... flds) {
88                 Map<String, Object> ret = new HashMap<String, Object>();
89                 for (int i = 0; i < flds.length; i += 2) {
90                         ret.put((String)flds[i], flds[i + 1]);
91                 }
92                 return(ret);
93         }
94
95         protected void initMockResponses() {
96
97                 registerMockResponse("GET /ccds/catalog/mycatalog/solution?page=0&size=100", MockResponse.success("mockCDSPortalSolutionsResponse.json"));
98                 registerMockResponse("GET /ccds/solution/search/date?atc=PB&inst=1000&active=true&page=0&size=100", MockResponse.success("mockCDSPortalSolutionsResponse.json"));
99                 registerMockResponse("GET /ccds/solution/search/date?atc=PB&inst=1531747662000&active=true&page=0&size=100", MockResponse.success("mockCDSDateSolutionsResponsePage0.json"));
100                 registerMockResponse("GET /ccds/solution/search/date?atc=PB&inst=1531747662000&active=true&page=1&size=100", MockResponse.success("mockCDSDateSolutionsResponsePage1.json"));
101                 registerMockResponse("GET /ccds/solution/10101010-1010-1010-1010-101010101010", MockResponse.success("mockCDSSolutionResponse.json"));
102                 registerMockResponse("GET /ccds/solution/10101010-1010-1010-1010-101010101010/revision", MockResponse.success("mockCDSSolutionRevisionsResponse.json"));
103                 registerMockResponse("GET /ccds/revision/a0a0a0a0-a0a0-a0a0-a0a0-a0a0a0a0a0a0/artifact", MockResponse.success("mockCDSSolutionRevisionArtifactsResponse.json"));
104                 registerMockResponse("GET /ccds/solution/f0f0f0f0-f0f0-f0f0-f0f0-f0f0f0f0f0f0", new MockResponse(400, "Error", "mockCDSNoEntryWithIDResponse.json"));
105                 registerMockResponse("GET /ccds/solution/f0f0f0f0-f0f0-f0f0-f0f0-f0f0f0f0f0f0/revision", new MockResponse(400, "Error", "mockCDSNoEntryWithIDResponse.json"));
106                 registerMockResponse("GET /ccds/revision/f0f0f0f0-f0f0-f0f0-f0f0-f0f0f0f0f0f0/artifact", new MockResponse(400, "Error", "mockCDSNoEntryWithIDResponse.json"));
107                 registerMockResponse("GET /ccds/solution/f1f1f1f1-f1f1-f1f1-f1f1-f1f1f1f1f1f1", new MockResponse(412, "Error", "mockCDSErrorResponse.json"));
108                 registerMockResponse("GET /ccds/solution/f1f1f1f1-f1f1-f1f1-f1f1-f1f1f1f1f1f1/revision", new MockResponse(412, "Error", "mockCDSErrorResponse.json"));
109                 registerMockResponse("GET /ccds/revision/f1f1f1f1-f1f1-f1f1-f1f1-f1f1f1f1f1f1/artifact", new MockResponse(412, "Error", "mockCDSErrorResponse.json"));
110                 registerMockResponse("GET /ccds/peer/search?self=true&_j=a&page=0&size=100", MockResponse.success("mockCDSPeerSearchSelfResponse.json"));
111         }
112
113         /**
114          * The gateway behaviour is triggered by the availability of other solutions
115          * in a peer, as provided by the federation client.  
116          */
117         @Test
118         public void testCatalogService() {
119
120                 try {
121                         ServiceContext selfService = 
122                                 ServiceContext.forPeer(new Peer(new MLPPeer("acumosa", "gateway.acumosa.org", "https://gateway.acumosa.org:9084", false, false, "admin@acumosa.org", "AC"), Role.SELF));
123
124                         List<MLPSolution> solutions = catalog.getSolutions(selector("catalogId", "mycatalog"), selfService);
125                         assertTrue("Unexpected solutions count: " + solutions.size(), solutions.size() == 5);
126                         solutions = catalog.getSolutions(selector("catalogId", "mycatalog", "modelTypeCode", "RG"), selfService);
127                         assertTrue("Unexpected solutions count: " + solutions.size(), solutions.size() == 2);
128                         solutions = catalog.getSolutions(selector("catalogId", "mycatalog", "toolkitTypeCode", new CopyOnWriteArrayList(new String[] {"CP", "TF" })), selfService);
129                         assertTrue("Unexpected solutions count: " + solutions.size(), solutions.size() == 3);
130                         solutions = catalog.getSolutions(selector("catalogId", "mycatalog", "tags", "subtract"), selfService);
131                         assertTrue("Unexpected solutions count: " + solutions.size(), solutions.size() == 1);
132                         solutions = catalog.getSolutions(selector("catalogId", "mycatalog", "tags", new CopyOnWriteArrayList(new String[] { "subtract", "poutput"})), selfService);
133                         assertTrue("Unexpected solutions count: " + solutions.size(), solutions.size() == 2);
134                         solutions = catalog.getSolutions(selector("catalogId", "mycatalog", "solutionId", "38efeef1-e4f4-4298-9f68-6f0052d6ade9"), selfService);
135                         assertTrue("Unexpected solutions count: " + solutions.size(), solutions.size() == 1);
136                         try {
137                                 catalog.getSolutions(selector("catalogId", "mycatalog", "name", new CopyOnWriteArrayList(new String[] { "A", "B" })), selfService);
138                                 assertTrue("Expected service exception, got none", 1 == 0);
139                         }
140                         catch (ServiceException sx) {
141                         }
142                         try {
143                                 catalog.getSolutions(selector("catalogId", "mycatalog", "name", true), selfService);
144                                 assertTrue("Expected service exception, got none", 1 == 0);
145                         }
146                         catch (ServiceException sx) {
147                         }
148                         try {
149                                 catalog.getSolutions(selector("catalogId", "mycatalog", "tags", true), selfService);
150                                 assertTrue("Expected service exception, got none", 1 == 0);
151                         }
152                         catch (ServiceException sx) {
153                         }
154                         solutions = catalog.getSolutions(Collections.EMPTY_MAP, selfService);
155                         assertTrue("Unexpected solutions count: " + solutions.size(), solutions.size() == 5);
156                 
157                         Solution solution = catalog.getSolution("10101010-1010-1010-1010-101010101010", selfService);
158                         assertTrue("Unexpected solution info", solution.getName().equals("test"));
159
160                         List<? extends MLPSolutionRevision> revisions = solution.getRevisions();
161                         if (revisions != null && !revisions.isEmpty()) {
162                                 assertTrue("Unexpected revisions count: " + revisions.size(), revisions.size() == 1);
163                                 for (MLPSolutionRevision revision: revisions) {
164                                         assertTrue("Unexpected revision info", revision.getVersion().startsWith("1"));
165                                         List<MLPArtifact> artifacts = catalog.getSolutionRevisionArtifacts(solution.getSolutionId(), revision.getRevisionId());
166                                         assertTrue("Unexpected artifacts count: " + artifacts.size(), artifacts.size() == 1);
167                                                 //catalog.getSolutionRevisionArtifact();
168                                 }
169                         }
170
171                         //no such entry
172                         Solution nsSolution = catalog.getSolution("f0f0f0f0-f0f0-f0f0-f0f0-f0f0f0f0f0f0", selfService);
173                         assertTrue("Unexpected no such solution outcome", nsSolution == null);
174
175                         List<MLPSolutionRevision> nsRevisions = catalog.getSolutionRevisions("f0f0f0f0-f0f0-f0f0-f0f0-f0f0f0f0f0f0", selfService);
176                         assertTrue("Unexpected no such solution (revisions) outcome", nsRevisions == null);
177
178                         List<MLPArtifact> nsArtifacts = catalog.getSolutionRevisionArtifacts("f0f0f0f0-f0f0-f0f0-f0f0-f0f0f0f0f0f0", "f0f0f0f0-f0f0-f0f0-f0f0-f0f0f0f0f0f0", selfService);
179                         assertTrue("Unexpected no such solution (revision artifacts) outcome", nsArtifacts == null);
180
181                         //other errors
182                         try {
183                                 catalog.getSolution("f1f1f1f1-f1f1-f1f1-f1f1-f1f1f1f1f1f1", selfService);
184                                 assertTrue("Expected service exception, got none", 1 == 0);
185                         }
186                         catch (ServiceException sx) {
187                         }
188                 
189                         try {
190                                 catalog.getSolutionRevisions("f1f1f1f1-f1f1-f1f1-f1f1-f1f1f1f1f1f1", selfService);
191                                 assertTrue("Expected service exception, got none", 1 == 0);
192                         }
193                         catch (ServiceException sx) {
194                         }
195
196                         try {
197                                 catalog.getSolutionRevisionArtifacts("f1f1f1f1-f1f1-f1f1-f1f1-f1f1f1f1f1f1", "f1f1f1f1-f1f1-f1f1-f1f1-f1f1f1f1f1f1", selfService);
198                                 assertTrue("Expected service exception, got none", 1 == 0);
199                         }
200                         catch (ServiceException sx) {
201                         }
202                 }
203                 catch (Exception x) {
204                         fail("Unexpected catalog test outcome: " + org.apache.commons.lang.exception.ExceptionUtils.getStackTrace(x));
205                 }
206         }
207
208 }