1137419f053a8ca7ef2655b89f21c4f8933be2f6
[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.Collections;
26 import java.util.List;
27
28 import org.acumos.cds.domain.MLPArtifact;
29 import org.acumos.cds.domain.MLPPeer;
30 import org.acumos.cds.domain.MLPSolution;
31 import org.acumos.cds.domain.MLPSolutionRevision;
32 import org.acumos.federation.gateway.cds.Solution;
33 import org.acumos.federation.gateway.security.Peer;
34 import org.acumos.federation.gateway.security.Role;
35 import org.acumos.federation.gateway.service.CatalogService;
36 import org.acumos.federation.gateway.service.ServiceContext;
37 import org.acumos.federation.gateway.service.ServiceException;
38 import org.junit.FixMethodOrder;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.junit.runners.MethodSorters;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.boot.test.context.SpringBootTest;
44 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
45 import org.springframework.test.context.junit4.SpringRunner;
46
47
48 /**
49  */
50
51 //@RunWith(SpringJUnit4ClassRunner.class)
52 @RunWith(SpringRunner.class)
53 @SpringBootTest(classes = org.acumos.federation.gateway.Application.class,
54                                                                 webEnvironment = WebEnvironment.RANDOM_PORT,
55                                                                 properties = {
56                                                                         "spring.main.allow-bean-definition-overriding=true",
57                                                                         "federation.instance=gateway",
58                                                                         "federation.instance.name=test",
59                                                                         "federation.operator=admin",
60                                                                         "federation.ssl.key-store=classpath:acumosa.pkcs12",
61                                                                         "federation.ssl.key-store-password=acumosa",
62                                                                         "federation.ssl.key-store-type=PKCS12",
63                                                                         "federation.ssl.key-password = acumosa",
64                                                                         "federation.ssl.trust-store=classpath:acumosTrustStore.jks",
65                                                                         "federation.ssl.trust-store-password=acumos",
66                                                                         "federation.ssl.client-auth=need",
67                                                                         "local.addr=127.0.0.1",
68                                                                         "local.server.port=9011",
69                                                                         "local.ssl.key-store=classpath:acumosa.pkcs12",
70                                                                         "local.ssl.key-store-password=acumosa",
71                                                                         "local.ssl.key-store-type=PKCS12",
72                                                                         "local.ssl.key-password=acumosa",
73                                                                         "cdms.client.url=http://localhost:8000/ccds",
74                                                                         "cdms.client.username=username",
75                                                                         "cdms.client.password=password"
76                                                                         })
77 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
78 public class CatalogServiceTest extends ServiceTest {
79
80         
81         @Autowired
82         private CatalogService catalog;
83
84         protected void initMockResponses() {
85
86                 registerMockResponse("GET /ccds/solution/search/date?atc=PB&inst=1000&active=true&page=0&size=100", MockResponse.success("mockCDSPortalSolutionsResponse.json"));
87                 registerMockResponse("GET /ccds/solution/search/date?atc=PB&datems=1531747662&active=true&page=0&size=100", MockResponse.success("mockCDSDateSolutionsResponsePage0.json"));
88                 registerMockResponse("GET /ccds/solution/search/date?atc=PB&datems=1531747662&active=true&page=1&size=100", MockResponse.success("mockCDSDateSolutionsResponsePage1.json"));
89                 registerMockResponse("GET /ccds/solution/10101010-1010-1010-1010-101010101010", MockResponse.success("mockCDSSolutionResponse.json"));
90                 registerMockResponse("GET /ccds/solution/10101010-1010-1010-1010-101010101010/revision", MockResponse.success("mockCDSSolutionRevisionsResponse.json"));
91                 registerMockResponse("GET /ccds/revision/a0a0a0a0-a0a0-a0a0-a0a0-a0a0a0a0a0a0/artifact", MockResponse.success("mockCDSSolutionRevisionArtifactsResponse.json"));
92                 registerMockResponse("GET /ccds/solution/f0f0f0f0-f0f0-f0f0-f0f0-f0f0f0f0f0f0", new MockResponse(400, "Error", "mockCDSNoEntryWithIDResponse.json"));
93                 registerMockResponse("GET /ccds/solution/f0f0f0f0-f0f0-f0f0-f0f0-f0f0f0f0f0f0/revision", new MockResponse(400, "Error", "mockCDSNoEntryWithIDResponse.json"));
94                 registerMockResponse("GET /ccds/revision/f0f0f0f0-f0f0-f0f0-f0f0-f0f0f0f0f0f0/artifact", new MockResponse(400, "Error", "mockCDSNoEntryWithIDResponse.json"));
95                 registerMockResponse("GET /ccds/solution/f1f1f1f1-f1f1-f1f1-f1f1-f1f1f1f1f1f1", new MockResponse(412, "Error", "mockCDSErrorResponse.json"));
96                 registerMockResponse("GET /ccds/solution/f1f1f1f1-f1f1-f1f1-f1f1-f1f1f1f1f1f1/revision", new MockResponse(412, "Error", "mockCDSErrorResponse.json"));
97                 registerMockResponse("GET /ccds/revision/f1f1f1f1-f1f1-f1f1-f1f1-f1f1f1f1f1f1/artifact", new MockResponse(412, "Error", "mockCDSErrorResponse.json"));
98                 registerMockResponse("GET /ccds/peer/search?self=true&_j=a&page=0&size=100", MockResponse.success("mockCDSPeerSearchSelfResponse.json"));
99         }
100
101         /**
102          * The gateway behaviour is triggered by the availability of other solutions
103          * in a peer, as provided by the federation client.  
104          */
105         @Test
106         public void testCatalogService() {
107
108                 try {
109                         ServiceContext selfService = 
110                                 ServiceContext.forPeer(new Peer(new MLPPeer("acumosa", "gateway.acumosa.org", "https://gateway.acumosa.org:9084", false, false, "admin@acumosa.org", "AC"), Role.SELF));
111
112                         List<MLPSolution> solutions = catalog.getSolutions(Collections.EMPTY_MAP, selfService);
113                         assertTrue("Unexpected solutions count: " + solutions.size(), solutions.size() == 5);
114                 
115                         Solution solution = catalog.getSolution("10101010-1010-1010-1010-101010101010", selfService);
116                         assertTrue("Unexpected solution info", solution.getName().equals("test"));
117
118                         List<? extends MLPSolutionRevision> revisions = solution.getRevisions();
119                         if (revisions != null && !revisions.isEmpty()) {
120                                 assertTrue("Unexpected revisions count: " + revisions.size(), revisions.size() == 1);
121                                 for (MLPSolutionRevision revision: revisions) {
122                                         assertTrue("Unexpected revision info", revision.getVersion().startsWith("1"));
123                                         List<MLPArtifact> artifacts = catalog.getSolutionRevisionArtifacts(solution.getSolutionId(), revision.getRevisionId());
124                                         assertTrue("Unexpected artifacts count: " + artifacts.size(), artifacts.size() == 1);
125                                                 //catalog.getSolutionRevisionArtifact();
126                                 }
127                         }
128
129                         //no such entry
130                         Solution nsSolution = catalog.getSolution("f0f0f0f0-f0f0-f0f0-f0f0-f0f0f0f0f0f0", selfService);
131                         assertTrue("Unexpected no such solution outcome", nsSolution == null);
132
133                         List<MLPSolutionRevision> nsRevisions = catalog.getSolutionRevisions("f0f0f0f0-f0f0-f0f0-f0f0-f0f0f0f0f0f0", selfService);
134                         assertTrue("Unexpected no such solution (revisions) outcome", nsRevisions == null);
135
136                         List<MLPArtifact> nsArtifacts = catalog.getSolutionRevisionArtifacts("f0f0f0f0-f0f0-f0f0-f0f0-f0f0f0f0f0f0", "f0f0f0f0-f0f0-f0f0-f0f0-f0f0f0f0f0f0", selfService);
137                         assertTrue("Unexpected no such solution (revision artifacts) outcome", nsArtifacts == null);
138
139                         //other errors
140                         try {
141                                 catalog.getSolution("f1f1f1f1-f1f1-f1f1-f1f1-f1f1f1f1f1f1", selfService);
142                                 assertTrue("Expected service exception, got none", 1 == 0);
143                         }
144                         catch (ServiceException sx) {
145                         }
146                 
147                         try {
148                                 catalog.getSolutionRevisions("f1f1f1f1-f1f1-f1f1-f1f1-f1f1f1f1f1f1", selfService);
149                                 assertTrue("Expected service exception, got none", 1 == 0);
150                         }
151                         catch (ServiceException sx) {
152                         }
153
154                         try {
155                                 catalog.getSolutionRevisionArtifacts("f1f1f1f1-f1f1-f1f1-f1f1-f1f1f1f1f1f1", "f1f1f1f1-f1f1-f1f1-f1f1-f1f1f1f1f1f1", selfService);
156                                 assertTrue("Expected service exception, got none", 1 == 0);
157                         }
158                         catch (ServiceException sx) {
159                         }
160                 }
161                 catch (Exception x) {
162                         fail("Unexpected catalog test outcome: " + org.apache.commons.lang.exception.ExceptionUtils.getStackTrace(x));
163                 }
164         }
165
166 }